blob: a4e0963bf4573ea5bc0bfec40ce51502172e96f3 [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
Jeff Mahoney143bede2012-03-01 14:56:26 +010075void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040076{
77 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040078
Yan Zheng2b820322008-11-17 21:11:30 -050079 while (!list_empty(&fs_uuids)) {
80 fs_devices = list_entry(fs_uuids.next,
81 struct btrfs_fs_devices, list);
82 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050083 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040084 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040085}
86
Chris Masona1b32a52008-09-05 16:09:51 -040087static noinline struct btrfs_device *__find_device(struct list_head *head,
88 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040089{
90 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040091
Qinghuang Fengc6e30872009-01-21 10:59:08 -050092 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040093 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040094 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040095 return dev;
Chris Masona4437552008-04-18 10:29:38 -040096 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040097 }
98 return NULL;
99}
100
Chris Masona1b32a52008-09-05 16:09:51 -0400101static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400103 struct btrfs_fs_devices *fs_devices;
104
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500105 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400106 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
107 return fs_devices;
108 }
109 return NULL;
110}
111
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100112static int
113btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
114 int flush, struct block_device **bdev,
115 struct buffer_head **bh)
116{
117 int ret;
118
119 *bdev = blkdev_get_by_path(device_path, flags, holder);
120
121 if (IS_ERR(*bdev)) {
122 ret = PTR_ERR(*bdev);
123 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
124 goto error;
125 }
126
127 if (flush)
128 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
129 ret = set_blocksize(*bdev, 4096);
130 if (ret) {
131 blkdev_put(*bdev, flags);
132 goto error;
133 }
134 invalidate_bdev(*bdev);
135 *bh = btrfs_read_dev_super(*bdev);
136 if (!*bh) {
137 ret = -EINVAL;
138 blkdev_put(*bdev, flags);
139 goto error;
140 }
141
142 return 0;
143
144error:
145 *bdev = NULL;
146 *bh = NULL;
147 return ret;
148}
149
Chris Masonffbd5172009-04-20 15:50:09 -0400150static void requeue_list(struct btrfs_pending_bios *pending_bios,
151 struct bio *head, struct bio *tail)
152{
153
154 struct bio *old_head;
155
156 old_head = pending_bios->head;
157 pending_bios->head = head;
158 if (pending_bios->tail)
159 tail->bi_next = old_head;
160 else
161 pending_bios->tail = tail;
162}
163
Chris Mason8b712842008-06-11 16:50:36 -0400164/*
165 * we try to collect pending bios for a device so we don't get a large
166 * number of procs sending bios down to the same device. This greatly
167 * improves the schedulers ability to collect and merge the bios.
168 *
169 * But, it also turns into a long list of bios to process and that is sure
170 * to eventually make the worker thread block. The solution here is to
171 * make some progress and then put this work struct back at the end of
172 * the list if the block device is congested. This way, multiple devices
173 * can make progress from a single worker thread.
174 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100175static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400176{
177 struct bio *pending;
178 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400179 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400180 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400181 struct bio *tail;
182 struct bio *cur;
183 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400184 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400185 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400186 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400187 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400188 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000189 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400190 struct blk_plug plug;
191
192 /*
193 * this function runs all the bios we've collected for
194 * a particular device. We don't want to wander off to
195 * another device without first sending all of these down.
196 * So, setup a plug here and finish it off before we return
197 */
198 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400199
Chris Masonbedf7622009-04-03 10:32:58 -0400200 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400201 fs_info = device->dev_root->fs_info;
202 limit = btrfs_async_submit_limit(fs_info);
203 limit = limit * 2 / 3;
204
Chris Mason8b712842008-06-11 16:50:36 -0400205loop:
206 spin_lock(&device->io_lock);
207
Chris Masona6837052009-02-04 09:19:41 -0500208loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400209 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400210
Chris Mason8b712842008-06-11 16:50:36 -0400211 /* take all the bios off the list at once and process them
212 * later on (without the lock held). But, remember the
213 * tail and other pointers so the bios can be properly reinserted
214 * into the list if we hit congestion
215 */
Chris Masond84275c2009-06-09 15:39:08 -0400216 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400217 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400218 force_reg = 1;
219 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400220 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400221 force_reg = 0;
222 }
Chris Masonffbd5172009-04-20 15:50:09 -0400223
224 pending = pending_bios->head;
225 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400226 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400227
228 /*
229 * if pending was null this time around, no bios need processing
230 * at all and we can stop. Otherwise it'll loop back up again
231 * and do an additional check so no bios are missed.
232 *
233 * device->running_pending is used to synchronize with the
234 * schedule_bio code.
235 */
Chris Masonffbd5172009-04-20 15:50:09 -0400236 if (device->pending_sync_bios.head == NULL &&
237 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400238 again = 0;
239 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400240 } else {
241 again = 1;
242 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400243 }
Chris Masonffbd5172009-04-20 15:50:09 -0400244
245 pending_bios->head = NULL;
246 pending_bios->tail = NULL;
247
Chris Mason8b712842008-06-11 16:50:36 -0400248 spin_unlock(&device->io_lock);
249
Chris Masond3977122009-01-05 21:25:51 -0500250 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400251
252 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400253 /* we want to work on both lists, but do more bios on the
254 * sync list than the regular list
255 */
256 if ((num_run > 32 &&
257 pending_bios != &device->pending_sync_bios &&
258 device->pending_sync_bios.head) ||
259 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
260 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400261 spin_lock(&device->io_lock);
262 requeue_list(pending_bios, pending, tail);
263 goto loop_lock;
264 }
265
Chris Mason8b712842008-06-11 16:50:36 -0400266 cur = pending;
267 pending = pending->bi_next;
268 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400269
Josef Bacik66657b32012-08-01 15:36:24 -0400270 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400271 waitqueue_active(&fs_info->async_submit_wait))
272 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400273
274 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400275
Chris Mason2ab1ba62011-08-04 14:28:36 -0400276 /*
277 * if we're doing the sync list, record that our
278 * plug has some sync requests on it
279 *
280 * If we're doing the regular list and there are
281 * sync requests sitting around, unplug before
282 * we add more
283 */
284 if (pending_bios == &device->pending_sync_bios) {
285 sync_pending = 1;
286 } else if (sync_pending) {
287 blk_finish_plug(&plug);
288 blk_start_plug(&plug);
289 sync_pending = 0;
290 }
291
Stefan Behrens21adbd52011-11-09 13:44:05 +0100292 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400293 num_run++;
294 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100295 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400296 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400297
298 /*
299 * we made progress, there is more work to do and the bdi
300 * is now congested. Back off and let other work structs
301 * run instead
302 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400303 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500304 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400305 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400306
Chris Masonb765ead2009-04-03 10:27:10 -0400307 ioc = current->io_context;
308
309 /*
310 * the main goal here is that we don't want to
311 * block if we're going to be able to submit
312 * more requests without blocking.
313 *
314 * This code does two great things, it pokes into
315 * the elevator code from a filesystem _and_
316 * it makes assumptions about how batching works.
317 */
318 if (ioc && ioc->nr_batch_requests > 0 &&
319 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
320 (last_waited == 0 ||
321 ioc->last_waited == last_waited)) {
322 /*
323 * we want to go through our batch of
324 * requests and stop. So, we copy out
325 * the ioc->last_waited time and test
326 * against it before looping
327 */
328 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100329 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400330 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400331 continue;
332 }
Chris Mason8b712842008-06-11 16:50:36 -0400333 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400334 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500335 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400336
337 spin_unlock(&device->io_lock);
338 btrfs_requeue_work(&device->work);
339 goto done;
340 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500341 /* unplug every 64 requests just for good measure */
342 if (batch_run % 64 == 0) {
343 blk_finish_plug(&plug);
344 blk_start_plug(&plug);
345 sync_pending = 0;
346 }
Chris Mason8b712842008-06-11 16:50:36 -0400347 }
Chris Masonffbd5172009-04-20 15:50:09 -0400348
Chris Mason51684082010-03-10 15:33:32 -0500349 cond_resched();
350 if (again)
351 goto loop;
352
353 spin_lock(&device->io_lock);
354 if (device->pending_bios.head || device->pending_sync_bios.head)
355 goto loop_lock;
356 spin_unlock(&device->io_lock);
357
Chris Mason8b712842008-06-11 16:50:36 -0400358done:
Chris Mason211588a2011-04-19 20:12:40 -0400359 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400360}
361
Christoph Hellwigb2950862008-12-02 09:54:17 -0500362static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400363{
364 struct btrfs_device *device;
365
366 device = container_of(work, struct btrfs_device, work);
367 run_scheduled_bios(device);
368}
369
Chris Masona1b32a52008-09-05 16:09:51 -0400370static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400371 struct btrfs_super_block *disk_super,
372 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
373{
374 struct btrfs_device *device;
375 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400376 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400377 u64 found_transid = btrfs_super_generation(disk_super);
378
379 fs_devices = find_fsid(disk_super->fsid);
380 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400381 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400382 if (!fs_devices)
383 return -ENOMEM;
384 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400385 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400386 list_add(&fs_devices->list, &fs_uuids);
387 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
388 fs_devices->latest_devid = devid;
389 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400390 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400391 device = NULL;
392 } else {
Chris Masona4437552008-04-18 10:29:38 -0400393 device = __find_device(&fs_devices->devices, devid,
394 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400395 }
396 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500397 if (fs_devices->opened)
398 return -EBUSY;
399
Chris Mason8a4b83c2008-03-24 15:02:07 -0400400 device = kzalloc(sizeof(*device), GFP_NOFS);
401 if (!device) {
402 /* we can safely leave the fs_devices entry around */
403 return -ENOMEM;
404 }
405 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200406 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400407 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400408 memcpy(device->uuid, disk_super->dev_item.uuid,
409 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400410 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400411
412 name = rcu_string_strdup(path, GFP_NOFS);
413 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400414 kfree(device);
415 return -ENOMEM;
416 }
Josef Bacik606686e2012-06-04 14:03:51 -0400417 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500418 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400419
Arne Jansen90519d62011-05-23 14:30:00 +0200420 /* init readahead state */
421 spin_lock_init(&device->reada_lock);
422 device->reada_curr_zone = NULL;
423 atomic_set(&device->reada_in_flight, 0);
424 device->reada_next = 0;
425 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
426 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
427
Chris Masone5e9a522009-06-10 15:17:02 -0400428 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000429 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400430 mutex_unlock(&fs_devices->device_list_mutex);
431
Yan Zheng2b820322008-11-17 21:11:30 -0500432 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400433 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400434 } else if (!device->name || strcmp(device->name->str, path)) {
435 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000436 if (!name)
437 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400438 rcu_string_free(device->name);
439 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500440 if (device->missing) {
441 fs_devices->missing_devices--;
442 device->missing = 0;
443 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400444 }
445
446 if (found_transid > fs_devices->latest_trans) {
447 fs_devices->latest_devid = devid;
448 fs_devices->latest_trans = found_transid;
449 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400450 *fs_devices_ret = fs_devices;
451 return 0;
452}
453
Yan Zhenge4404d62008-12-12 10:03:26 -0500454static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
455{
456 struct btrfs_fs_devices *fs_devices;
457 struct btrfs_device *device;
458 struct btrfs_device *orig_dev;
459
460 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
461 if (!fs_devices)
462 return ERR_PTR(-ENOMEM);
463
464 INIT_LIST_HEAD(&fs_devices->devices);
465 INIT_LIST_HEAD(&fs_devices->alloc_list);
466 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400467 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500468 fs_devices->latest_devid = orig->latest_devid;
469 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400470 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500471 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
472
Xiao Guangrong46224702011-04-20 10:08:47 +0000473 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500474 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400475 struct rcu_string *name;
476
Yan Zhenge4404d62008-12-12 10:03:26 -0500477 device = kzalloc(sizeof(*device), GFP_NOFS);
478 if (!device)
479 goto error;
480
Josef Bacik606686e2012-06-04 14:03:51 -0400481 /*
482 * This is ok to do without rcu read locked because we hold the
483 * uuid mutex so nothing we touch in here is going to disappear.
484 */
485 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
486 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400487 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500488 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400489 }
Josef Bacik606686e2012-06-04 14:03:51 -0400490 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500491
492 device->devid = orig_dev->devid;
493 device->work.func = pending_bios_fn;
494 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500495 spin_lock_init(&device->io_lock);
496 INIT_LIST_HEAD(&device->dev_list);
497 INIT_LIST_HEAD(&device->dev_alloc_list);
498
499 list_add(&device->dev_list, &fs_devices->devices);
500 device->fs_devices = fs_devices;
501 fs_devices->num_devices++;
502 }
503 return fs_devices;
504error:
505 free_fs_devices(fs_devices);
506 return ERR_PTR(-ENOMEM);
507}
508
Stefan Behrens8dabb742012-11-06 13:15:27 +0100509void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
510 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400511{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500512 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400513
Chris Masona6b0d5c2012-02-20 20:53:43 -0500514 struct block_device *latest_bdev = NULL;
515 u64 latest_devid = 0;
516 u64 latest_transid = 0;
517
Chris Masondfe25022008-05-13 13:46:40 -0400518 mutex_lock(&uuid_mutex);
519again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000520 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500521 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500522 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100523 if (!device->is_tgtdev_for_dev_replace &&
524 (!latest_transid ||
525 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500526 latest_devid = device->devid;
527 latest_transid = device->generation;
528 latest_bdev = device->bdev;
529 }
Yan Zheng2b820322008-11-17 21:11:30 -0500530 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500531 }
Yan Zheng2b820322008-11-17 21:11:30 -0500532
Stefan Behrens8dabb742012-11-06 13:15:27 +0100533 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
534 /*
535 * In the first step, keep the device which has
536 * the correct fsid and the devid that is used
537 * for the dev_replace procedure.
538 * In the second step, the dev_replace state is
539 * read from the device tree and it is known
540 * whether the procedure is really active or
541 * not, which means whether this device is
542 * used or whether it should be removed.
543 */
544 if (step == 0 || device->is_tgtdev_for_dev_replace) {
545 continue;
546 }
547 }
Yan Zheng2b820322008-11-17 21:11:30 -0500548 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100549 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500550 device->bdev = NULL;
551 fs_devices->open_devices--;
552 }
553 if (device->writeable) {
554 list_del_init(&device->dev_alloc_list);
555 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100556 if (!device->is_tgtdev_for_dev_replace)
557 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500558 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500559 list_del_init(&device->dev_list);
560 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400561 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500562 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400563 }
Yan Zheng2b820322008-11-17 21:11:30 -0500564
565 if (fs_devices->seed) {
566 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500567 goto again;
568 }
569
Chris Masona6b0d5c2012-02-20 20:53:43 -0500570 fs_devices->latest_bdev = latest_bdev;
571 fs_devices->latest_devid = latest_devid;
572 fs_devices->latest_trans = latest_transid;
573
Chris Masondfe25022008-05-13 13:46:40 -0400574 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400575}
Chris Masona0af4692008-05-13 16:03:06 -0400576
Xiao Guangrong1f781602011-04-20 10:09:16 +0000577static void __free_device(struct work_struct *work)
578{
579 struct btrfs_device *device;
580
581 device = container_of(work, struct btrfs_device, rcu_work);
582
583 if (device->bdev)
584 blkdev_put(device->bdev, device->mode);
585
Josef Bacik606686e2012-06-04 14:03:51 -0400586 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000587 kfree(device);
588}
589
590static void free_device(struct rcu_head *head)
591{
592 struct btrfs_device *device;
593
594 device = container_of(head, struct btrfs_device, rcu);
595
596 INIT_WORK(&device->rcu_work, __free_device);
597 schedule_work(&device->rcu_work);
598}
599
Yan Zheng2b820322008-11-17 21:11:30 -0500600static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400601{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400602 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500603
Yan Zheng2b820322008-11-17 21:11:30 -0500604 if (--fs_devices->opened > 0)
605 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400606
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000607 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500608 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000609 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400610 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000611
612 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400613 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000614
Stefan Behrens8dabb742012-11-06 13:15:27 +0100615 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500616 list_del_init(&device->dev_alloc_list);
617 fs_devices->rw_devices--;
618 }
619
Josef Bacikd5e20032011-08-04 14:52:27 +0000620 if (device->can_discard)
621 fs_devices->num_can_discard--;
622
Xiao Guangrong1f781602011-04-20 10:09:16 +0000623 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100624 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000625 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400626
627 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400628 if (device->name) {
629 name = rcu_string_strdup(device->name->str, GFP_NOFS);
630 BUG_ON(device->name && !name); /* -ENOMEM */
631 rcu_assign_pointer(new_device->name, name);
632 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000633 new_device->bdev = NULL;
634 new_device->writeable = 0;
635 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000636 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000637 list_replace_rcu(&device->dev_list, &new_device->dev_list);
638
639 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400640 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000641 mutex_unlock(&fs_devices->device_list_mutex);
642
Yan Zhenge4404d62008-12-12 10:03:26 -0500643 WARN_ON(fs_devices->open_devices);
644 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500645 fs_devices->opened = 0;
646 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500647
Chris Mason8a4b83c2008-03-24 15:02:07 -0400648 return 0;
649}
650
Yan Zheng2b820322008-11-17 21:11:30 -0500651int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
652{
Yan Zhenge4404d62008-12-12 10:03:26 -0500653 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500654 int ret;
655
656 mutex_lock(&uuid_mutex);
657 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500658 if (!fs_devices->opened) {
659 seed_devices = fs_devices->seed;
660 fs_devices->seed = NULL;
661 }
Yan Zheng2b820322008-11-17 21:11:30 -0500662 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500663
664 while (seed_devices) {
665 fs_devices = seed_devices;
666 seed_devices = fs_devices->seed;
667 __btrfs_close_devices(fs_devices);
668 free_fs_devices(fs_devices);
669 }
Yan Zheng2b820322008-11-17 21:11:30 -0500670 return ret;
671}
672
Yan Zhenge4404d62008-12-12 10:03:26 -0500673static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
674 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400675{
Josef Bacikd5e20032011-08-04 14:52:27 +0000676 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400677 struct block_device *bdev;
678 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400679 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400680 struct block_device *latest_bdev = NULL;
681 struct buffer_head *bh;
682 struct btrfs_super_block *disk_super;
683 u64 latest_devid = 0;
684 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400685 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500686 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400687 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400688
Tejun Heod4d77622010-11-13 11:55:18 +0100689 flags |= FMODE_EXCL;
690
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500691 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400692 if (device->bdev)
693 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400694 if (!device->name)
695 continue;
696
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100697 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
698 &bdev, &bh);
699 if (ret)
700 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400701
702 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000703 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400704 if (devid != device->devid)
705 goto error_brelse;
706
Yan Zheng2b820322008-11-17 21:11:30 -0500707 if (memcmp(device->uuid, disk_super->dev_item.uuid,
708 BTRFS_UUID_SIZE))
709 goto error_brelse;
710
711 device->generation = btrfs_super_generation(disk_super);
712 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400713 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500714 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400715 latest_bdev = bdev;
716 }
717
Yan Zheng2b820322008-11-17 21:11:30 -0500718 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
719 device->writeable = 0;
720 } else {
721 device->writeable = !bdev_read_only(bdev);
722 seeding = 0;
723 }
724
Josef Bacikd5e20032011-08-04 14:52:27 +0000725 q = bdev_get_queue(bdev);
726 if (blk_queue_discard(q)) {
727 device->can_discard = 1;
728 fs_devices->num_can_discard++;
729 }
730
Chris Mason8a4b83c2008-03-24 15:02:07 -0400731 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400732 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500733 device->mode = flags;
734
Chris Masonc2898112009-06-10 09:51:32 -0400735 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
736 fs_devices->rotating = 1;
737
Chris Masona0af4692008-05-13 16:03:06 -0400738 fs_devices->open_devices++;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100739 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500740 fs_devices->rw_devices++;
741 list_add(&device->dev_alloc_list,
742 &fs_devices->alloc_list);
743 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000744 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400745 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400746
Chris Masona0af4692008-05-13 16:03:06 -0400747error_brelse:
748 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100749 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400750 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 }
Chris Masona0af4692008-05-13 16:03:06 -0400752 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300753 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400754 goto out;
755 }
Yan Zheng2b820322008-11-17 21:11:30 -0500756 fs_devices->seeding = seeding;
757 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400758 fs_devices->latest_bdev = latest_bdev;
759 fs_devices->latest_devid = latest_devid;
760 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500761 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400762out:
Yan Zheng2b820322008-11-17 21:11:30 -0500763 return ret;
764}
765
766int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500767 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500768{
769 int ret;
770
771 mutex_lock(&uuid_mutex);
772 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500773 fs_devices->opened++;
774 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500775 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500776 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500777 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400778 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400779 return ret;
780}
781
Christoph Hellwig97288f22008-12-02 06:36:09 -0500782int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400783 struct btrfs_fs_devices **fs_devices_ret)
784{
785 struct btrfs_super_block *disk_super;
786 struct block_device *bdev;
787 struct buffer_head *bh;
788 int ret;
789 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400790 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400791 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400792
Tejun Heod4d77622010-11-13 11:55:18 +0100793 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500794 mutex_lock(&uuid_mutex);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100795 ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400796 if (ret)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100797 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400798 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000799 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400800 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400801 total_devices = btrfs_super_num_devices(disk_super);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000802 if (disk_super->label[0]) {
803 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
804 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500805 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000806 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200807 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000808 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500809 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500810 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400811 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400812 if (!ret && fs_devices_ret)
813 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400814 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100815 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400816error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100817 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400818 return ret;
819}
Chris Mason0b86a832008-03-24 15:01:56 -0400820
Miao Xie6d07bce2011-01-05 10:07:31 +0000821/* helper to account the used device space in the range */
822int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
823 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400824{
825 struct btrfs_key key;
826 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000827 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500828 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000829 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400830 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000831 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400832 struct extent_buffer *l;
833
Miao Xie6d07bce2011-01-05 10:07:31 +0000834 *length = 0;
835
Stefan Behrens63a212a2012-11-05 18:29:28 +0100836 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000837 return 0;
838
Yan Zheng2b820322008-11-17 21:11:30 -0500839 path = btrfs_alloc_path();
840 if (!path)
841 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400842 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400843
Chris Mason0b86a832008-03-24 15:01:56 -0400844 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000845 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400846 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000847
848 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400849 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000850 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400851 if (ret > 0) {
852 ret = btrfs_previous_item(root, path, key.objectid, key.type);
853 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000854 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400855 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000856
Chris Mason0b86a832008-03-24 15:01:56 -0400857 while (1) {
858 l = path->nodes[0];
859 slot = path->slots[0];
860 if (slot >= btrfs_header_nritems(l)) {
861 ret = btrfs_next_leaf(root, path);
862 if (ret == 0)
863 continue;
864 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000865 goto out;
866
867 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400868 }
869 btrfs_item_key_to_cpu(l, &key, slot);
870
871 if (key.objectid < device->devid)
872 goto next;
873
874 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000875 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400876
Chris Masond3977122009-01-05 21:25:51 -0500877 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400878 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400879
Chris Mason0b86a832008-03-24 15:01:56 -0400880 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000881 extent_end = key.offset + btrfs_dev_extent_length(l,
882 dev_extent);
883 if (key.offset <= start && extent_end > end) {
884 *length = end - start + 1;
885 break;
886 } else if (key.offset <= start && extent_end > start)
887 *length += extent_end - start;
888 else if (key.offset > start && extent_end <= end)
889 *length += extent_end - key.offset;
890 else if (key.offset > start && key.offset <= end) {
891 *length += end - key.offset + 1;
892 break;
893 } else if (key.offset > end)
894 break;
895
896next:
897 path->slots[0]++;
898 }
899 ret = 0;
900out:
901 btrfs_free_path(path);
902 return ret;
903}
904
Chris Mason0b86a832008-03-24 15:01:56 -0400905/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000906 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000907 * @device: the device which we search the free space in
908 * @num_bytes: the size of the free space that we need
909 * @start: store the start of the free space.
910 * @len: the size of the free space. that we find, or the size of the max
911 * free space if we don't find suitable free space
912 *
Chris Mason0b86a832008-03-24 15:01:56 -0400913 * this uses a pretty simple search, the expectation is that it is
914 * called very infrequently and that a given device has a small number
915 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000916 *
917 * @start is used to store the start of the free space if we find. But if we
918 * don't find suitable free space, it will be used to store the start position
919 * of the max free space.
920 *
921 * @len is used to store the size of the free space that we find.
922 * But if we don't find suitable free space, it is used to store the size of
923 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400924 */
Li Zefan125ccb02011-12-08 15:07:24 +0800925int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000926 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400927{
928 struct btrfs_key key;
929 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000930 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400931 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000932 u64 hole_size;
933 u64 max_hole_start;
934 u64 max_hole_size;
935 u64 extent_end;
936 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400937 u64 search_end = device->total_bytes;
938 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000939 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400940 struct extent_buffer *l;
941
Chris Mason0b86a832008-03-24 15:01:56 -0400942 /* FIXME use last free of some kind */
943
944 /* we don't want to overwrite the superblock on the drive,
945 * so we make sure to start at an offset of at least 1MB
946 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200947 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400948
Miao Xie7bfc8372011-01-05 10:07:26 +0000949 max_hole_start = search_start;
950 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000951 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000952
Stefan Behrens63a212a2012-11-05 18:29:28 +0100953 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +0000954 ret = -ENOSPC;
955 goto error;
956 }
957
958 path = btrfs_alloc_path();
959 if (!path) {
960 ret = -ENOMEM;
961 goto error;
962 }
963 path->reada = 2;
964
Chris Mason0b86a832008-03-24 15:01:56 -0400965 key.objectid = device->devid;
966 key.offset = search_start;
967 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000968
Li Zefan125ccb02011-12-08 15:07:24 +0800969 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400970 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000971 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400972 if (ret > 0) {
973 ret = btrfs_previous_item(root, path, key.objectid, key.type);
974 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000975 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400976 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000977
Chris Mason0b86a832008-03-24 15:01:56 -0400978 while (1) {
979 l = path->nodes[0];
980 slot = path->slots[0];
981 if (slot >= btrfs_header_nritems(l)) {
982 ret = btrfs_next_leaf(root, path);
983 if (ret == 0)
984 continue;
985 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000986 goto out;
987
988 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400989 }
990 btrfs_item_key_to_cpu(l, &key, slot);
991
992 if (key.objectid < device->devid)
993 goto next;
994
995 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000996 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400997
Chris Mason0b86a832008-03-24 15:01:56 -0400998 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
999 goto next;
1000
Miao Xie7bfc8372011-01-05 10:07:26 +00001001 if (key.offset > search_start) {
1002 hole_size = key.offset - search_start;
1003
1004 if (hole_size > max_hole_size) {
1005 max_hole_start = search_start;
1006 max_hole_size = hole_size;
1007 }
1008
1009 /*
1010 * If this free space is greater than which we need,
1011 * it must be the max free space that we have found
1012 * until now, so max_hole_start must point to the start
1013 * of this free space and the length of this free space
1014 * is stored in max_hole_size. Thus, we return
1015 * max_hole_start and max_hole_size and go back to the
1016 * caller.
1017 */
1018 if (hole_size >= num_bytes) {
1019 ret = 0;
1020 goto out;
1021 }
1022 }
1023
Chris Mason0b86a832008-03-24 15:01:56 -04001024 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001025 extent_end = key.offset + btrfs_dev_extent_length(l,
1026 dev_extent);
1027 if (extent_end > search_start)
1028 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001029next:
1030 path->slots[0]++;
1031 cond_resched();
1032 }
Chris Mason0b86a832008-03-24 15:01:56 -04001033
liubo38c01b92011-08-02 02:39:03 +00001034 /*
1035 * At this point, search_start should be the end of
1036 * allocated dev extents, and when shrinking the device,
1037 * search_end may be smaller than search_start.
1038 */
1039 if (search_end > search_start)
1040 hole_size = search_end - search_start;
1041
Miao Xie7bfc8372011-01-05 10:07:26 +00001042 if (hole_size > max_hole_size) {
1043 max_hole_start = search_start;
1044 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001045 }
Chris Mason0b86a832008-03-24 15:01:56 -04001046
Miao Xie7bfc8372011-01-05 10:07:26 +00001047 /* See above. */
1048 if (hole_size < num_bytes)
1049 ret = -ENOSPC;
1050 else
1051 ret = 0;
1052
1053out:
Yan Zheng2b820322008-11-17 21:11:30 -05001054 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001055error:
1056 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001057 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001058 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001059 return ret;
1060}
1061
Christoph Hellwigb2950862008-12-02 09:54:17 -05001062static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001063 struct btrfs_device *device,
1064 u64 start)
1065{
1066 int ret;
1067 struct btrfs_path *path;
1068 struct btrfs_root *root = device->dev_root;
1069 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001070 struct btrfs_key found_key;
1071 struct extent_buffer *leaf = NULL;
1072 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001073
1074 path = btrfs_alloc_path();
1075 if (!path)
1076 return -ENOMEM;
1077
1078 key.objectid = device->devid;
1079 key.offset = start;
1080 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001081again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001082 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001083 if (ret > 0) {
1084 ret = btrfs_previous_item(root, path, key.objectid,
1085 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001086 if (ret)
1087 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001088 leaf = path->nodes[0];
1089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1090 extent = btrfs_item_ptr(leaf, path->slots[0],
1091 struct btrfs_dev_extent);
1092 BUG_ON(found_key.offset > start || found_key.offset +
1093 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001094 key = found_key;
1095 btrfs_release_path(path);
1096 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001097 } else if (ret == 0) {
1098 leaf = path->nodes[0];
1099 extent = btrfs_item_ptr(leaf, path->slots[0],
1100 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001101 } else {
1102 btrfs_error(root->fs_info, ret, "Slot search failed");
1103 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001104 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001105
Josef Bacik2bf64752011-09-26 17:12:22 -04001106 if (device->bytes_used > 0) {
1107 u64 len = btrfs_dev_extent_length(leaf, extent);
1108 device->bytes_used -= len;
1109 spin_lock(&root->fs_info->free_chunk_lock);
1110 root->fs_info->free_chunk_space += len;
1111 spin_unlock(&root->fs_info->free_chunk_lock);
1112 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001113 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001114 if (ret) {
1115 btrfs_error(root->fs_info, ret,
1116 "Failed to remove dev extent item");
1117 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001118out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001119 btrfs_free_path(path);
1120 return ret;
1121}
1122
Yan Zheng2b820322008-11-17 21:11:30 -05001123int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001124 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001125 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001126 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001127{
1128 int ret;
1129 struct btrfs_path *path;
1130 struct btrfs_root *root = device->dev_root;
1131 struct btrfs_dev_extent *extent;
1132 struct extent_buffer *leaf;
1133 struct btrfs_key key;
1134
Chris Masondfe25022008-05-13 13:46:40 -04001135 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001136 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001137 path = btrfs_alloc_path();
1138 if (!path)
1139 return -ENOMEM;
1140
Chris Mason0b86a832008-03-24 15:01:56 -04001141 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001142 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001143 key.type = BTRFS_DEV_EXTENT_KEY;
1144 ret = btrfs_insert_empty_item(trans, root, path, &key,
1145 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001146 if (ret)
1147 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001148
1149 leaf = path->nodes[0];
1150 extent = btrfs_item_ptr(leaf, path->slots[0],
1151 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001152 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1153 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1154 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1155
1156 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1157 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1158 BTRFS_UUID_SIZE);
1159
Chris Mason0b86a832008-03-24 15:01:56 -04001160 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1161 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001162out:
Chris Mason0b86a832008-03-24 15:01:56 -04001163 btrfs_free_path(path);
1164 return ret;
1165}
1166
Chris Masona1b32a52008-09-05 16:09:51 -04001167static noinline int find_next_chunk(struct btrfs_root *root,
1168 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001169{
1170 struct btrfs_path *path;
1171 int ret;
1172 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001173 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001174 struct btrfs_key found_key;
1175
1176 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001177 if (!path)
1178 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001179
Chris Masone17cade2008-04-15 15:41:47 -04001180 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001181 key.offset = (u64)-1;
1182 key.type = BTRFS_CHUNK_ITEM_KEY;
1183
1184 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1185 if (ret < 0)
1186 goto error;
1187
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001188 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001189
1190 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1191 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001192 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001193 } else {
1194 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1195 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001196 if (found_key.objectid != objectid)
1197 *offset = 0;
1198 else {
1199 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1200 struct btrfs_chunk);
1201 *offset = found_key.offset +
1202 btrfs_chunk_length(path->nodes[0], chunk);
1203 }
Chris Mason0b86a832008-03-24 15:01:56 -04001204 }
1205 ret = 0;
1206error:
1207 btrfs_free_path(path);
1208 return ret;
1209}
1210
Yan Zheng2b820322008-11-17 21:11:30 -05001211static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001212{
1213 int ret;
1214 struct btrfs_key key;
1215 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001216 struct btrfs_path *path;
1217
1218 root = root->fs_info->chunk_root;
1219
1220 path = btrfs_alloc_path();
1221 if (!path)
1222 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001223
1224 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1225 key.type = BTRFS_DEV_ITEM_KEY;
1226 key.offset = (u64)-1;
1227
1228 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1229 if (ret < 0)
1230 goto error;
1231
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001232 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001233
1234 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1235 BTRFS_DEV_ITEM_KEY);
1236 if (ret) {
1237 *objectid = 1;
1238 } else {
1239 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1240 path->slots[0]);
1241 *objectid = found_key.offset + 1;
1242 }
1243 ret = 0;
1244error:
Yan Zheng2b820322008-11-17 21:11:30 -05001245 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001246 return ret;
1247}
1248
1249/*
1250 * the device information is stored in the chunk root
1251 * the btrfs_device struct should be fully filled in
1252 */
1253int btrfs_add_device(struct btrfs_trans_handle *trans,
1254 struct btrfs_root *root,
1255 struct btrfs_device *device)
1256{
1257 int ret;
1258 struct btrfs_path *path;
1259 struct btrfs_dev_item *dev_item;
1260 struct extent_buffer *leaf;
1261 struct btrfs_key key;
1262 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001263
1264 root = root->fs_info->chunk_root;
1265
1266 path = btrfs_alloc_path();
1267 if (!path)
1268 return -ENOMEM;
1269
Chris Mason0b86a832008-03-24 15:01:56 -04001270 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1271 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001272 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001273
1274 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001275 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001276 if (ret)
1277 goto out;
1278
1279 leaf = path->nodes[0];
1280 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1281
1282 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001283 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001284 btrfs_set_device_type(leaf, dev_item, device->type);
1285 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1286 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1287 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001288 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1289 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001290 btrfs_set_device_group(leaf, dev_item, 0);
1291 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1292 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001293 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001294
Chris Mason0b86a832008-03-24 15:01:56 -04001295 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001296 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001297 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1298 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001299 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001300
Yan Zheng2b820322008-11-17 21:11:30 -05001301 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001302out:
1303 btrfs_free_path(path);
1304 return ret;
1305}
Chris Mason8f18cf12008-04-25 16:53:30 -04001306
Chris Masona061fc82008-05-07 11:43:44 -04001307static int btrfs_rm_dev_item(struct btrfs_root *root,
1308 struct btrfs_device *device)
1309{
1310 int ret;
1311 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001312 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001313 struct btrfs_trans_handle *trans;
1314
1315 root = root->fs_info->chunk_root;
1316
1317 path = btrfs_alloc_path();
1318 if (!path)
1319 return -ENOMEM;
1320
Yan, Zhenga22285a2010-05-16 10:48:46 -04001321 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001322 if (IS_ERR(trans)) {
1323 btrfs_free_path(path);
1324 return PTR_ERR(trans);
1325 }
Chris Masona061fc82008-05-07 11:43:44 -04001326 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1327 key.type = BTRFS_DEV_ITEM_KEY;
1328 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001329 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001330
1331 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1332 if (ret < 0)
1333 goto out;
1334
1335 if (ret > 0) {
1336 ret = -ENOENT;
1337 goto out;
1338 }
1339
1340 ret = btrfs_del_item(trans, root, path);
1341 if (ret)
1342 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001343out:
1344 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001345 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001346 btrfs_commit_transaction(trans, root);
1347 return ret;
1348}
1349
1350int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1351{
1352 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001353 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001354 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001355 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001356 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001357 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001358 u64 all_avail;
1359 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001360 u64 num_devices;
1361 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001362 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001363 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001364
Chris Masona061fc82008-05-07 11:43:44 -04001365 mutex_lock(&uuid_mutex);
1366
1367 all_avail = root->fs_info->avail_data_alloc_bits |
1368 root->fs_info->avail_system_alloc_bits |
1369 root->fs_info->avail_metadata_alloc_bits;
1370
Stefan Behrens8dabb742012-11-06 13:15:27 +01001371 num_devices = root->fs_info->fs_devices->num_devices;
1372 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1373 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1374 WARN_ON(num_devices < 1);
1375 num_devices--;
1376 }
1377 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1378
1379 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001380 printk(KERN_ERR "btrfs: unable to go below four devices "
1381 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001382 ret = -EINVAL;
1383 goto out;
1384 }
1385
Stefan Behrens8dabb742012-11-06 13:15:27 +01001386 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001387 printk(KERN_ERR "btrfs: unable to go below two "
1388 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001389 ret = -EINVAL;
1390 goto out;
1391 }
1392
Chris Masondfe25022008-05-13 13:46:40 -04001393 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001394 struct list_head *devices;
1395 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001396
Chris Masondfe25022008-05-13 13:46:40 -04001397 device = NULL;
1398 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001399 /*
1400 * It is safe to read the devices since the volume_mutex
1401 * is held.
1402 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001403 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001404 if (tmp->in_fs_metadata &&
1405 !tmp->is_tgtdev_for_dev_replace &&
1406 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001407 device = tmp;
1408 break;
1409 }
1410 }
1411 bdev = NULL;
1412 bh = NULL;
1413 disk_super = NULL;
1414 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001415 printk(KERN_ERR "btrfs: no missing devices found to "
1416 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001417 goto out;
1418 }
Chris Masondfe25022008-05-13 13:46:40 -04001419 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001420 ret = btrfs_get_bdev_and_sb(device_path,
1421 FMODE_READ | FMODE_EXCL,
1422 root->fs_info->bdev_holder, 0,
1423 &bdev, &bh);
1424 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001425 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001426 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001427 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001428 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001429 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001430 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001431 if (!device) {
1432 ret = -ENOENT;
1433 goto error_brelse;
1434 }
Chris Masondfe25022008-05-13 13:46:40 -04001435 }
Yan Zheng2b820322008-11-17 21:11:30 -05001436
Stefan Behrens63a212a2012-11-05 18:29:28 +01001437 if (device->is_tgtdev_for_dev_replace) {
1438 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1439 ret = -EINVAL;
1440 goto error_brelse;
1441 }
1442
Yan Zheng2b820322008-11-17 21:11:30 -05001443 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001444 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1445 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001446 ret = -EINVAL;
1447 goto error_brelse;
1448 }
1449
1450 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001451 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001452 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001453 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001454 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001455 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001456 }
Chris Masona061fc82008-05-07 11:43:44 -04001457
1458 ret = btrfs_shrink_device(device, 0);
1459 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001460 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001461
Stefan Behrens63a212a2012-11-05 18:29:28 +01001462 /*
1463 * TODO: the superblock still includes this device in its num_devices
1464 * counter although write_all_supers() is not locked out. This
1465 * could give a filesystem state which requires a degraded mount.
1466 */
Chris Masona061fc82008-05-07 11:43:44 -04001467 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1468 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001469 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001470
Josef Bacik2bf64752011-09-26 17:12:22 -04001471 spin_lock(&root->fs_info->free_chunk_lock);
1472 root->fs_info->free_chunk_space = device->total_bytes -
1473 device->bytes_used;
1474 spin_unlock(&root->fs_info->free_chunk_lock);
1475
Yan Zheng2b820322008-11-17 21:11:30 -05001476 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001477 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001478
1479 /*
1480 * the device list mutex makes sure that we don't change
1481 * the device list while someone else is writing out all
1482 * the device supers.
1483 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001484
1485 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001486 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001487 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001488
Yan Zhenge4404d62008-12-12 10:03:26 -05001489 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001490 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001491
Chris Masoncd02dca2010-12-13 14:56:23 -05001492 if (device->missing)
1493 root->fs_info->fs_devices->missing_devices--;
1494
Yan Zheng2b820322008-11-17 21:11:30 -05001495 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1496 struct btrfs_device, dev_list);
1497 if (device->bdev == root->fs_info->sb->s_bdev)
1498 root->fs_info->sb->s_bdev = next_device->bdev;
1499 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1500 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1501
Xiao Guangrong1f781602011-04-20 10:09:16 +00001502 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001503 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001504
1505 call_rcu(&device->rcu, free_device);
1506 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001507
David Sterba6c417612011-04-13 15:41:04 +02001508 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1509 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001510
Xiao Guangrong1f781602011-04-20 10:09:16 +00001511 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001512 struct btrfs_fs_devices *fs_devices;
1513 fs_devices = root->fs_info->fs_devices;
1514 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001515 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001516 break;
1517 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001518 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001519 fs_devices->seed = cur_devices->seed;
1520 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001521 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001522 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001523 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001524 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001525 }
1526
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001527 root->fs_info->num_tolerated_disk_barrier_failures =
1528 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1529
Yan Zheng2b820322008-11-17 21:11:30 -05001530 /*
1531 * at this point, the device is zero sized. We want to
1532 * remove it from the devices list and zero out the old super
1533 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001534 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001535 /* make sure this device isn't detected as part of
1536 * the FS anymore
1537 */
1538 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1539 set_buffer_dirty(bh);
1540 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001541 }
Chris Masona061fc82008-05-07 11:43:44 -04001542
Chris Masona061fc82008-05-07 11:43:44 -04001543 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001544
1545error_brelse:
1546 brelse(bh);
1547error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001548 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001549 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001550out:
1551 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001552 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001553error_undo:
1554 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001555 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001556 list_add(&device->dev_alloc_list,
1557 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001558 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001559 root->fs_info->fs_devices->rw_devices++;
1560 }
1561 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001562}
1563
Stefan Behrense93c89c2012-11-05 17:33:06 +01001564void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1565 struct btrfs_device *srcdev)
1566{
1567 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1568 list_del_rcu(&srcdev->dev_list);
1569 list_del_rcu(&srcdev->dev_alloc_list);
1570 fs_info->fs_devices->num_devices--;
1571 if (srcdev->missing) {
1572 fs_info->fs_devices->missing_devices--;
1573 fs_info->fs_devices->rw_devices++;
1574 }
1575 if (srcdev->can_discard)
1576 fs_info->fs_devices->num_can_discard--;
1577 if (srcdev->bdev)
1578 fs_info->fs_devices->open_devices--;
1579
1580 call_rcu(&srcdev->rcu, free_device);
1581}
1582
1583void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1584 struct btrfs_device *tgtdev)
1585{
1586 struct btrfs_device *next_device;
1587
1588 WARN_ON(!tgtdev);
1589 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1590 if (tgtdev->bdev) {
1591 btrfs_scratch_superblock(tgtdev);
1592 fs_info->fs_devices->open_devices--;
1593 }
1594 fs_info->fs_devices->num_devices--;
1595 if (tgtdev->can_discard)
1596 fs_info->fs_devices->num_can_discard++;
1597
1598 next_device = list_entry(fs_info->fs_devices->devices.next,
1599 struct btrfs_device, dev_list);
1600 if (tgtdev->bdev == fs_info->sb->s_bdev)
1601 fs_info->sb->s_bdev = next_device->bdev;
1602 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1603 fs_info->fs_devices->latest_bdev = next_device->bdev;
1604 list_del_rcu(&tgtdev->dev_list);
1605
1606 call_rcu(&tgtdev->rcu, free_device);
1607
1608 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1609}
1610
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001611int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1612 struct btrfs_device **device)
1613{
1614 int ret = 0;
1615 struct btrfs_super_block *disk_super;
1616 u64 devid;
1617 u8 *dev_uuid;
1618 struct block_device *bdev;
1619 struct buffer_head *bh;
1620
1621 *device = NULL;
1622 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1623 root->fs_info->bdev_holder, 0, &bdev, &bh);
1624 if (ret)
1625 return ret;
1626 disk_super = (struct btrfs_super_block *)bh->b_data;
1627 devid = btrfs_stack_device_id(&disk_super->dev_item);
1628 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001629 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001630 disk_super->fsid);
1631 brelse(bh);
1632 if (!*device)
1633 ret = -ENOENT;
1634 blkdev_put(bdev, FMODE_READ);
1635 return ret;
1636}
1637
1638int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1639 char *device_path,
1640 struct btrfs_device **device)
1641{
1642 *device = NULL;
1643 if (strcmp(device_path, "missing") == 0) {
1644 struct list_head *devices;
1645 struct btrfs_device *tmp;
1646
1647 devices = &root->fs_info->fs_devices->devices;
1648 /*
1649 * It is safe to read the devices since the volume_mutex
1650 * is held by the caller.
1651 */
1652 list_for_each_entry(tmp, devices, dev_list) {
1653 if (tmp->in_fs_metadata && !tmp->bdev) {
1654 *device = tmp;
1655 break;
1656 }
1657 }
1658
1659 if (!*device) {
1660 pr_err("btrfs: no missing device found\n");
1661 return -ENOENT;
1662 }
1663
1664 return 0;
1665 } else {
1666 return btrfs_find_device_by_path(root, device_path, device);
1667 }
1668}
1669
Yan Zheng2b820322008-11-17 21:11:30 -05001670/*
1671 * does all the dirty work required for changing file system's UUID.
1672 */
Li Zefan125ccb02011-12-08 15:07:24 +08001673static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001674{
1675 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1676 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001677 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001678 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001679 struct btrfs_device *device;
1680 u64 super_flags;
1681
1682 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001683 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001684 return -EINVAL;
1685
Yan Zhenge4404d62008-12-12 10:03:26 -05001686 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1687 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001688 return -ENOMEM;
1689
Yan Zhenge4404d62008-12-12 10:03:26 -05001690 old_devices = clone_fs_devices(fs_devices);
1691 if (IS_ERR(old_devices)) {
1692 kfree(seed_devices);
1693 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001694 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001695
Yan Zheng2b820322008-11-17 21:11:30 -05001696 list_add(&old_devices->list, &fs_uuids);
1697
Yan Zhenge4404d62008-12-12 10:03:26 -05001698 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1699 seed_devices->opened = 1;
1700 INIT_LIST_HEAD(&seed_devices->devices);
1701 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001702 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001703
1704 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001705 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1706 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001707 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1708
Yan Zhenge4404d62008-12-12 10:03:26 -05001709 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1710 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1711 device->fs_devices = seed_devices;
1712 }
1713
Yan Zheng2b820322008-11-17 21:11:30 -05001714 fs_devices->seeding = 0;
1715 fs_devices->num_devices = 0;
1716 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001717 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001718 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001719
1720 generate_random_uuid(fs_devices->fsid);
1721 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1722 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1723 super_flags = btrfs_super_flags(disk_super) &
1724 ~BTRFS_SUPER_FLAG_SEEDING;
1725 btrfs_set_super_flags(disk_super, super_flags);
1726
1727 return 0;
1728}
1729
1730/*
1731 * strore the expected generation for seed devices in device items.
1732 */
1733static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1734 struct btrfs_root *root)
1735{
1736 struct btrfs_path *path;
1737 struct extent_buffer *leaf;
1738 struct btrfs_dev_item *dev_item;
1739 struct btrfs_device *device;
1740 struct btrfs_key key;
1741 u8 fs_uuid[BTRFS_UUID_SIZE];
1742 u8 dev_uuid[BTRFS_UUID_SIZE];
1743 u64 devid;
1744 int ret;
1745
1746 path = btrfs_alloc_path();
1747 if (!path)
1748 return -ENOMEM;
1749
1750 root = root->fs_info->chunk_root;
1751 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1752 key.offset = 0;
1753 key.type = BTRFS_DEV_ITEM_KEY;
1754
1755 while (1) {
1756 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1757 if (ret < 0)
1758 goto error;
1759
1760 leaf = path->nodes[0];
1761next_slot:
1762 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1763 ret = btrfs_next_leaf(root, path);
1764 if (ret > 0)
1765 break;
1766 if (ret < 0)
1767 goto error;
1768 leaf = path->nodes[0];
1769 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001770 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001771 continue;
1772 }
1773
1774 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1775 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1776 key.type != BTRFS_DEV_ITEM_KEY)
1777 break;
1778
1779 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1780 struct btrfs_dev_item);
1781 devid = btrfs_device_id(leaf, dev_item);
1782 read_extent_buffer(leaf, dev_uuid,
1783 (unsigned long)btrfs_device_uuid(dev_item),
1784 BTRFS_UUID_SIZE);
1785 read_extent_buffer(leaf, fs_uuid,
1786 (unsigned long)btrfs_device_fsid(dev_item),
1787 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001788 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1789 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001790 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001791
1792 if (device->fs_devices->seeding) {
1793 btrfs_set_device_generation(leaf, dev_item,
1794 device->generation);
1795 btrfs_mark_buffer_dirty(leaf);
1796 }
1797
1798 path->slots[0]++;
1799 goto next_slot;
1800 }
1801 ret = 0;
1802error:
1803 btrfs_free_path(path);
1804 return ret;
1805}
1806
Chris Mason788f20e2008-04-28 15:29:42 -04001807int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1808{
Josef Bacikd5e20032011-08-04 14:52:27 +00001809 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001810 struct btrfs_trans_handle *trans;
1811 struct btrfs_device *device;
1812 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001813 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001814 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001815 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001816 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001817 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001818 int ret = 0;
1819
Yan Zheng2b820322008-11-17 21:11:30 -05001820 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001821 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001822
Li Zefana5d163332011-12-07 20:08:40 -05001823 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001824 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001825 if (IS_ERR(bdev))
1826 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001827
Yan Zheng2b820322008-11-17 21:11:30 -05001828 if (root->fs_info->fs_devices->seeding) {
1829 seeding_dev = 1;
1830 down_write(&sb->s_umount);
1831 mutex_lock(&uuid_mutex);
1832 }
1833
Chris Mason8c8bee12008-09-29 11:19:10 -04001834 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001835
Chris Mason788f20e2008-04-28 15:29:42 -04001836 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001837
1838 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001839 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001840 if (device->bdev == bdev) {
1841 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001842 mutex_unlock(
1843 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001844 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001845 }
1846 }
Liu Bod25628b2012-11-14 14:35:30 +00001847 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001848
1849 device = kzalloc(sizeof(*device), GFP_NOFS);
1850 if (!device) {
1851 /* we can safely leave the fs_devices entry around */
1852 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001853 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001854 }
1855
Josef Bacik606686e2012-06-04 14:03:51 -04001856 name = rcu_string_strdup(device_path, GFP_NOFS);
1857 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001858 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001859 ret = -ENOMEM;
1860 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001861 }
Josef Bacik606686e2012-06-04 14:03:51 -04001862 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001863
1864 ret = find_next_devid(root, &device->devid);
1865 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001866 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001867 kfree(device);
1868 goto error;
1869 }
1870
Yan, Zhenga22285a2010-05-16 10:48:46 -04001871 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001872 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001873 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001874 kfree(device);
1875 ret = PTR_ERR(trans);
1876 goto error;
1877 }
1878
Yan Zheng2b820322008-11-17 21:11:30 -05001879 lock_chunks(root);
1880
Josef Bacikd5e20032011-08-04 14:52:27 +00001881 q = bdev_get_queue(bdev);
1882 if (blk_queue_discard(q))
1883 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001884 device->writeable = 1;
1885 device->work.func = pending_bios_fn;
1886 generate_random_uuid(device->uuid);
1887 spin_lock_init(&device->io_lock);
1888 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001889 device->io_width = root->sectorsize;
1890 device->io_align = root->sectorsize;
1891 device->sector_size = root->sectorsize;
1892 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001893 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001894 device->dev_root = root->fs_info->dev_root;
1895 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001896 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001897 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001898 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001899 set_blocksize(device->bdev, 4096);
1900
Yan Zheng2b820322008-11-17 21:11:30 -05001901 if (seeding_dev) {
1902 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001903 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001904 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001905 }
1906
1907 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001908
Chris Masone5e9a522009-06-10 15:17:02 -04001909 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001910 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001911 list_add(&device->dev_alloc_list,
1912 &root->fs_info->fs_devices->alloc_list);
1913 root->fs_info->fs_devices->num_devices++;
1914 root->fs_info->fs_devices->open_devices++;
1915 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001916 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001917 if (device->can_discard)
1918 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001919 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1920
Josef Bacik2bf64752011-09-26 17:12:22 -04001921 spin_lock(&root->fs_info->free_chunk_lock);
1922 root->fs_info->free_chunk_space += device->total_bytes;
1923 spin_unlock(&root->fs_info->free_chunk_lock);
1924
Chris Masonc2898112009-06-10 09:51:32 -04001925 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1926 root->fs_info->fs_devices->rotating = 1;
1927
David Sterba6c417612011-04-13 15:41:04 +02001928 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1929 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001930 total_bytes + device->total_bytes);
1931
David Sterba6c417612011-04-13 15:41:04 +02001932 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1933 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001934 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001935 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001936
Yan Zheng2b820322008-11-17 21:11:30 -05001937 if (seeding_dev) {
1938 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001939 if (ret) {
1940 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001941 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001942 }
Yan Zheng2b820322008-11-17 21:11:30 -05001943 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06001944 if (ret) {
1945 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001946 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001947 }
Yan Zheng2b820322008-11-17 21:11:30 -05001948 } else {
1949 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001950 if (ret) {
1951 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001952 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001953 }
Yan Zheng2b820322008-11-17 21:11:30 -05001954 }
1955
Chris Mason913d9522009-03-10 13:17:18 -04001956 /*
1957 * we've got more storage, clear any full flags on the space
1958 * infos
1959 */
1960 btrfs_clear_space_info_full(root->fs_info);
1961
Chris Mason7d9eb122008-07-08 14:19:17 -04001962 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001963 root->fs_info->num_tolerated_disk_barrier_failures =
1964 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001965 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001966
1967 if (seeding_dev) {
1968 mutex_unlock(&uuid_mutex);
1969 up_write(&sb->s_umount);
1970
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001971 if (ret) /* transaction commit */
1972 return ret;
1973
Yan Zheng2b820322008-11-17 21:11:30 -05001974 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001975 if (ret < 0)
1976 btrfs_error(root->fs_info, ret,
1977 "Failed to relocate sys chunks after "
1978 "device initialization. This can be fixed "
1979 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00001980 trans = btrfs_attach_transaction(root);
1981 if (IS_ERR(trans)) {
1982 if (PTR_ERR(trans) == -ENOENT)
1983 return 0;
1984 return PTR_ERR(trans);
1985 }
1986 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001987 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001988
Chris Mason788f20e2008-04-28 15:29:42 -04001989 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001990
1991error_trans:
1992 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001993 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001994 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001995 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001996error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001997 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001998 if (seeding_dev) {
1999 mutex_unlock(&uuid_mutex);
2000 up_write(&sb->s_umount);
2001 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002002 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002003}
2004
Stefan Behrense93c89c2012-11-05 17:33:06 +01002005int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2006 struct btrfs_device **device_out)
2007{
2008 struct request_queue *q;
2009 struct btrfs_device *device;
2010 struct block_device *bdev;
2011 struct btrfs_fs_info *fs_info = root->fs_info;
2012 struct list_head *devices;
2013 struct rcu_string *name;
2014 int ret = 0;
2015
2016 *device_out = NULL;
2017 if (fs_info->fs_devices->seeding)
2018 return -EINVAL;
2019
2020 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2021 fs_info->bdev_holder);
2022 if (IS_ERR(bdev))
2023 return PTR_ERR(bdev);
2024
2025 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2026
2027 devices = &fs_info->fs_devices->devices;
2028 list_for_each_entry(device, devices, dev_list) {
2029 if (device->bdev == bdev) {
2030 ret = -EEXIST;
2031 goto error;
2032 }
2033 }
2034
2035 device = kzalloc(sizeof(*device), GFP_NOFS);
2036 if (!device) {
2037 ret = -ENOMEM;
2038 goto error;
2039 }
2040
2041 name = rcu_string_strdup(device_path, GFP_NOFS);
2042 if (!name) {
2043 kfree(device);
2044 ret = -ENOMEM;
2045 goto error;
2046 }
2047 rcu_assign_pointer(device->name, name);
2048
2049 q = bdev_get_queue(bdev);
2050 if (blk_queue_discard(q))
2051 device->can_discard = 1;
2052 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2053 device->writeable = 1;
2054 device->work.func = pending_bios_fn;
2055 generate_random_uuid(device->uuid);
2056 device->devid = BTRFS_DEV_REPLACE_DEVID;
2057 spin_lock_init(&device->io_lock);
2058 device->generation = 0;
2059 device->io_width = root->sectorsize;
2060 device->io_align = root->sectorsize;
2061 device->sector_size = root->sectorsize;
2062 device->total_bytes = i_size_read(bdev->bd_inode);
2063 device->disk_total_bytes = device->total_bytes;
2064 device->dev_root = fs_info->dev_root;
2065 device->bdev = bdev;
2066 device->in_fs_metadata = 1;
2067 device->is_tgtdev_for_dev_replace = 1;
2068 device->mode = FMODE_EXCL;
2069 set_blocksize(device->bdev, 4096);
2070 device->fs_devices = fs_info->fs_devices;
2071 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2072 fs_info->fs_devices->num_devices++;
2073 fs_info->fs_devices->open_devices++;
2074 if (device->can_discard)
2075 fs_info->fs_devices->num_can_discard++;
2076 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2077
2078 *device_out = device;
2079 return ret;
2080
2081error:
2082 blkdev_put(bdev, FMODE_EXCL);
2083 return ret;
2084}
2085
2086void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2087 struct btrfs_device *tgtdev)
2088{
2089 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2090 tgtdev->io_width = fs_info->dev_root->sectorsize;
2091 tgtdev->io_align = fs_info->dev_root->sectorsize;
2092 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2093 tgtdev->dev_root = fs_info->dev_root;
2094 tgtdev->in_fs_metadata = 1;
2095}
2096
Chris Masond3977122009-01-05 21:25:51 -05002097static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2098 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002099{
2100 int ret;
2101 struct btrfs_path *path;
2102 struct btrfs_root *root;
2103 struct btrfs_dev_item *dev_item;
2104 struct extent_buffer *leaf;
2105 struct btrfs_key key;
2106
2107 root = device->dev_root->fs_info->chunk_root;
2108
2109 path = btrfs_alloc_path();
2110 if (!path)
2111 return -ENOMEM;
2112
2113 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2114 key.type = BTRFS_DEV_ITEM_KEY;
2115 key.offset = device->devid;
2116
2117 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2118 if (ret < 0)
2119 goto out;
2120
2121 if (ret > 0) {
2122 ret = -ENOENT;
2123 goto out;
2124 }
2125
2126 leaf = path->nodes[0];
2127 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2128
2129 btrfs_set_device_id(leaf, dev_item, device->devid);
2130 btrfs_set_device_type(leaf, dev_item, device->type);
2131 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2132 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2133 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002134 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002135 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2136 btrfs_mark_buffer_dirty(leaf);
2137
2138out:
2139 btrfs_free_path(path);
2140 return ret;
2141}
2142
Chris Mason7d9eb122008-07-08 14:19:17 -04002143static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002144 struct btrfs_device *device, u64 new_size)
2145{
2146 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002147 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002148 u64 old_total = btrfs_super_total_bytes(super_copy);
2149 u64 diff = new_size - device->total_bytes;
2150
Yan Zheng2b820322008-11-17 21:11:30 -05002151 if (!device->writeable)
2152 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002153 if (new_size <= device->total_bytes ||
2154 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002155 return -EINVAL;
2156
Chris Mason8f18cf12008-04-25 16:53:30 -04002157 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002158 device->fs_devices->total_rw_bytes += diff;
2159
2160 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002161 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002162 btrfs_clear_space_info_full(device->dev_root->fs_info);
2163
Chris Mason8f18cf12008-04-25 16:53:30 -04002164 return btrfs_update_device(trans, device);
2165}
2166
Chris Mason7d9eb122008-07-08 14:19:17 -04002167int btrfs_grow_device(struct btrfs_trans_handle *trans,
2168 struct btrfs_device *device, u64 new_size)
2169{
2170 int ret;
2171 lock_chunks(device->dev_root);
2172 ret = __btrfs_grow_device(trans, device, new_size);
2173 unlock_chunks(device->dev_root);
2174 return ret;
2175}
2176
Chris Mason8f18cf12008-04-25 16:53:30 -04002177static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2178 struct btrfs_root *root,
2179 u64 chunk_tree, u64 chunk_objectid,
2180 u64 chunk_offset)
2181{
2182 int ret;
2183 struct btrfs_path *path;
2184 struct btrfs_key key;
2185
2186 root = root->fs_info->chunk_root;
2187 path = btrfs_alloc_path();
2188 if (!path)
2189 return -ENOMEM;
2190
2191 key.objectid = chunk_objectid;
2192 key.offset = chunk_offset;
2193 key.type = BTRFS_CHUNK_ITEM_KEY;
2194
2195 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002196 if (ret < 0)
2197 goto out;
2198 else if (ret > 0) { /* Logic error or corruption */
2199 btrfs_error(root->fs_info, -ENOENT,
2200 "Failed lookup while freeing chunk.");
2201 ret = -ENOENT;
2202 goto out;
2203 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002204
2205 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002206 if (ret < 0)
2207 btrfs_error(root->fs_info, ret,
2208 "Failed to delete chunk item.");
2209out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002210 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002211 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002212}
2213
Christoph Hellwigb2950862008-12-02 09:54:17 -05002214static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002215 chunk_offset)
2216{
David Sterba6c417612011-04-13 15:41:04 +02002217 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002218 struct btrfs_disk_key *disk_key;
2219 struct btrfs_chunk *chunk;
2220 u8 *ptr;
2221 int ret = 0;
2222 u32 num_stripes;
2223 u32 array_size;
2224 u32 len = 0;
2225 u32 cur;
2226 struct btrfs_key key;
2227
2228 array_size = btrfs_super_sys_array_size(super_copy);
2229
2230 ptr = super_copy->sys_chunk_array;
2231 cur = 0;
2232
2233 while (cur < array_size) {
2234 disk_key = (struct btrfs_disk_key *)ptr;
2235 btrfs_disk_key_to_cpu(&key, disk_key);
2236
2237 len = sizeof(*disk_key);
2238
2239 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2240 chunk = (struct btrfs_chunk *)(ptr + len);
2241 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2242 len += btrfs_chunk_item_size(num_stripes);
2243 } else {
2244 ret = -EIO;
2245 break;
2246 }
2247 if (key.objectid == chunk_objectid &&
2248 key.offset == chunk_offset) {
2249 memmove(ptr, ptr + len, array_size - (cur + len));
2250 array_size -= len;
2251 btrfs_set_super_sys_array_size(super_copy, array_size);
2252 } else {
2253 ptr += len;
2254 cur += len;
2255 }
2256 }
2257 return ret;
2258}
2259
Christoph Hellwigb2950862008-12-02 09:54:17 -05002260static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002261 u64 chunk_tree, u64 chunk_objectid,
2262 u64 chunk_offset)
2263{
2264 struct extent_map_tree *em_tree;
2265 struct btrfs_root *extent_root;
2266 struct btrfs_trans_handle *trans;
2267 struct extent_map *em;
2268 struct map_lookup *map;
2269 int ret;
2270 int i;
2271
2272 root = root->fs_info->chunk_root;
2273 extent_root = root->fs_info->extent_root;
2274 em_tree = &root->fs_info->mapping_tree.map_tree;
2275
Josef Bacikba1bf482009-09-11 16:11:19 -04002276 ret = btrfs_can_relocate(extent_root, chunk_offset);
2277 if (ret)
2278 return -ENOSPC;
2279
Chris Mason8f18cf12008-04-25 16:53:30 -04002280 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002281 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002282 if (ret)
2283 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002284
Yan, Zhenga22285a2010-05-16 10:48:46 -04002285 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002286 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002287
Chris Mason7d9eb122008-07-08 14:19:17 -04002288 lock_chunks(root);
2289
Chris Mason8f18cf12008-04-25 16:53:30 -04002290 /*
2291 * step two, delete the device extents and the
2292 * chunk tree entries
2293 */
Chris Mason890871b2009-09-02 16:24:52 -04002294 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002295 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002296 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002297
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002298 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002299 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002300 map = (struct map_lookup *)em->bdev;
2301
2302 for (i = 0; i < map->num_stripes; i++) {
2303 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2304 map->stripes[i].physical);
2305 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002306
Chris Masondfe25022008-05-13 13:46:40 -04002307 if (map->stripes[i].dev) {
2308 ret = btrfs_update_device(trans, map->stripes[i].dev);
2309 BUG_ON(ret);
2310 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002311 }
2312 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2313 chunk_offset);
2314
2315 BUG_ON(ret);
2316
liubo1abe9b82011-03-24 11:18:59 +00002317 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2318
Chris Mason8f18cf12008-04-25 16:53:30 -04002319 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2320 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2321 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002322 }
2323
Zheng Yan1a40e232008-09-26 10:09:34 -04002324 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2325 BUG_ON(ret);
2326
Chris Mason890871b2009-09-02 16:24:52 -04002327 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002328 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002329 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002330
Chris Mason8f18cf12008-04-25 16:53:30 -04002331 kfree(map);
2332 em->bdev = NULL;
2333
2334 /* once for the tree */
2335 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002336 /* once for us */
2337 free_extent_map(em);
2338
Chris Mason7d9eb122008-07-08 14:19:17 -04002339 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002340 btrfs_end_transaction(trans, root);
2341 return 0;
2342}
2343
Yan Zheng2b820322008-11-17 21:11:30 -05002344static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2345{
2346 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2347 struct btrfs_path *path;
2348 struct extent_buffer *leaf;
2349 struct btrfs_chunk *chunk;
2350 struct btrfs_key key;
2351 struct btrfs_key found_key;
2352 u64 chunk_tree = chunk_root->root_key.objectid;
2353 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002354 bool retried = false;
2355 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002356 int ret;
2357
2358 path = btrfs_alloc_path();
2359 if (!path)
2360 return -ENOMEM;
2361
Josef Bacikba1bf482009-09-11 16:11:19 -04002362again:
Yan Zheng2b820322008-11-17 21:11:30 -05002363 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2364 key.offset = (u64)-1;
2365 key.type = BTRFS_CHUNK_ITEM_KEY;
2366
2367 while (1) {
2368 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2369 if (ret < 0)
2370 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002371 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002372
2373 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2374 key.type);
2375 if (ret < 0)
2376 goto error;
2377 if (ret > 0)
2378 break;
2379
2380 leaf = path->nodes[0];
2381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2382
2383 chunk = btrfs_item_ptr(leaf, path->slots[0],
2384 struct btrfs_chunk);
2385 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002386 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002387
2388 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2389 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2390 found_key.objectid,
2391 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002392 if (ret == -ENOSPC)
2393 failed++;
2394 else if (ret)
2395 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002396 }
2397
2398 if (found_key.offset == 0)
2399 break;
2400 key.offset = found_key.offset - 1;
2401 }
2402 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002403 if (failed && !retried) {
2404 failed = 0;
2405 retried = true;
2406 goto again;
2407 } else if (failed && retried) {
2408 WARN_ON(1);
2409 ret = -ENOSPC;
2410 }
Yan Zheng2b820322008-11-17 21:11:30 -05002411error:
2412 btrfs_free_path(path);
2413 return ret;
2414}
2415
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002416static int insert_balance_item(struct btrfs_root *root,
2417 struct btrfs_balance_control *bctl)
2418{
2419 struct btrfs_trans_handle *trans;
2420 struct btrfs_balance_item *item;
2421 struct btrfs_disk_balance_args disk_bargs;
2422 struct btrfs_path *path;
2423 struct extent_buffer *leaf;
2424 struct btrfs_key key;
2425 int ret, err;
2426
2427 path = btrfs_alloc_path();
2428 if (!path)
2429 return -ENOMEM;
2430
2431 trans = btrfs_start_transaction(root, 0);
2432 if (IS_ERR(trans)) {
2433 btrfs_free_path(path);
2434 return PTR_ERR(trans);
2435 }
2436
2437 key.objectid = BTRFS_BALANCE_OBJECTID;
2438 key.type = BTRFS_BALANCE_ITEM_KEY;
2439 key.offset = 0;
2440
2441 ret = btrfs_insert_empty_item(trans, root, path, &key,
2442 sizeof(*item));
2443 if (ret)
2444 goto out;
2445
2446 leaf = path->nodes[0];
2447 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2448
2449 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2450
2451 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2452 btrfs_set_balance_data(leaf, item, &disk_bargs);
2453 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2454 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2455 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2456 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2457
2458 btrfs_set_balance_flags(leaf, item, bctl->flags);
2459
2460 btrfs_mark_buffer_dirty(leaf);
2461out:
2462 btrfs_free_path(path);
2463 err = btrfs_commit_transaction(trans, root);
2464 if (err && !ret)
2465 ret = err;
2466 return ret;
2467}
2468
2469static int del_balance_item(struct btrfs_root *root)
2470{
2471 struct btrfs_trans_handle *trans;
2472 struct btrfs_path *path;
2473 struct btrfs_key key;
2474 int ret, err;
2475
2476 path = btrfs_alloc_path();
2477 if (!path)
2478 return -ENOMEM;
2479
2480 trans = btrfs_start_transaction(root, 0);
2481 if (IS_ERR(trans)) {
2482 btrfs_free_path(path);
2483 return PTR_ERR(trans);
2484 }
2485
2486 key.objectid = BTRFS_BALANCE_OBJECTID;
2487 key.type = BTRFS_BALANCE_ITEM_KEY;
2488 key.offset = 0;
2489
2490 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2491 if (ret < 0)
2492 goto out;
2493 if (ret > 0) {
2494 ret = -ENOENT;
2495 goto out;
2496 }
2497
2498 ret = btrfs_del_item(trans, root, path);
2499out:
2500 btrfs_free_path(path);
2501 err = btrfs_commit_transaction(trans, root);
2502 if (err && !ret)
2503 ret = err;
2504 return ret;
2505}
2506
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002507/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002508 * This is a heuristic used to reduce the number of chunks balanced on
2509 * resume after balance was interrupted.
2510 */
2511static void update_balance_args(struct btrfs_balance_control *bctl)
2512{
2513 /*
2514 * Turn on soft mode for chunk types that were being converted.
2515 */
2516 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2517 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2518 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2519 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2520 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2521 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2522
2523 /*
2524 * Turn on usage filter if is not already used. The idea is
2525 * that chunks that we have already balanced should be
2526 * reasonably full. Don't do it for chunks that are being
2527 * converted - that will keep us from relocating unconverted
2528 * (albeit full) chunks.
2529 */
2530 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2531 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2532 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2533 bctl->data.usage = 90;
2534 }
2535 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2536 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2537 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2538 bctl->sys.usage = 90;
2539 }
2540 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2541 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2542 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2543 bctl->meta.usage = 90;
2544 }
2545}
2546
2547/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002548 * Should be called with both balance and volume mutexes held to
2549 * serialize other volume operations (add_dev/rm_dev/resize) with
2550 * restriper. Same goes for unset_balance_control.
2551 */
2552static void set_balance_control(struct btrfs_balance_control *bctl)
2553{
2554 struct btrfs_fs_info *fs_info = bctl->fs_info;
2555
2556 BUG_ON(fs_info->balance_ctl);
2557
2558 spin_lock(&fs_info->balance_lock);
2559 fs_info->balance_ctl = bctl;
2560 spin_unlock(&fs_info->balance_lock);
2561}
2562
2563static void unset_balance_control(struct btrfs_fs_info *fs_info)
2564{
2565 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2566
2567 BUG_ON(!fs_info->balance_ctl);
2568
2569 spin_lock(&fs_info->balance_lock);
2570 fs_info->balance_ctl = NULL;
2571 spin_unlock(&fs_info->balance_lock);
2572
2573 kfree(bctl);
2574}
2575
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002576/*
2577 * Balance filters. Return 1 if chunk should be filtered out
2578 * (should not be balanced).
2579 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002580static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002581 struct btrfs_balance_args *bargs)
2582{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002583 chunk_type = chunk_to_extended(chunk_type) &
2584 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002585
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002586 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002587 return 0;
2588
2589 return 1;
2590}
2591
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002592static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2593 struct btrfs_balance_args *bargs)
2594{
2595 struct btrfs_block_group_cache *cache;
2596 u64 chunk_used, user_thresh;
2597 int ret = 1;
2598
2599 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2600 chunk_used = btrfs_block_group_used(&cache->item);
2601
2602 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2603 if (chunk_used < user_thresh)
2604 ret = 0;
2605
2606 btrfs_put_block_group(cache);
2607 return ret;
2608}
2609
Ilya Dryomov409d4042012-01-16 22:04:47 +02002610static int chunk_devid_filter(struct extent_buffer *leaf,
2611 struct btrfs_chunk *chunk,
2612 struct btrfs_balance_args *bargs)
2613{
2614 struct btrfs_stripe *stripe;
2615 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2616 int i;
2617
2618 for (i = 0; i < num_stripes; i++) {
2619 stripe = btrfs_stripe_nr(chunk, i);
2620 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2621 return 0;
2622 }
2623
2624 return 1;
2625}
2626
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002627/* [pstart, pend) */
2628static int chunk_drange_filter(struct extent_buffer *leaf,
2629 struct btrfs_chunk *chunk,
2630 u64 chunk_offset,
2631 struct btrfs_balance_args *bargs)
2632{
2633 struct btrfs_stripe *stripe;
2634 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2635 u64 stripe_offset;
2636 u64 stripe_length;
2637 int factor;
2638 int i;
2639
2640 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2641 return 0;
2642
2643 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2644 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2645 factor = 2;
2646 else
2647 factor = 1;
2648 factor = num_stripes / factor;
2649
2650 for (i = 0; i < num_stripes; i++) {
2651 stripe = btrfs_stripe_nr(chunk, i);
2652 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2653 continue;
2654
2655 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2656 stripe_length = btrfs_chunk_length(leaf, chunk);
2657 do_div(stripe_length, factor);
2658
2659 if (stripe_offset < bargs->pend &&
2660 stripe_offset + stripe_length > bargs->pstart)
2661 return 0;
2662 }
2663
2664 return 1;
2665}
2666
Ilya Dryomovea671762012-01-16 22:04:48 +02002667/* [vstart, vend) */
2668static int chunk_vrange_filter(struct extent_buffer *leaf,
2669 struct btrfs_chunk *chunk,
2670 u64 chunk_offset,
2671 struct btrfs_balance_args *bargs)
2672{
2673 if (chunk_offset < bargs->vend &&
2674 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2675 /* at least part of the chunk is inside this vrange */
2676 return 0;
2677
2678 return 1;
2679}
2680
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002681static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002682 struct btrfs_balance_args *bargs)
2683{
2684 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2685 return 0;
2686
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002687 chunk_type = chunk_to_extended(chunk_type) &
2688 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002689
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002690 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002691 return 1;
2692
2693 return 0;
2694}
2695
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002696static int should_balance_chunk(struct btrfs_root *root,
2697 struct extent_buffer *leaf,
2698 struct btrfs_chunk *chunk, u64 chunk_offset)
2699{
2700 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2701 struct btrfs_balance_args *bargs = NULL;
2702 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2703
2704 /* type filter */
2705 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2706 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2707 return 0;
2708 }
2709
2710 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2711 bargs = &bctl->data;
2712 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2713 bargs = &bctl->sys;
2714 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2715 bargs = &bctl->meta;
2716
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002717 /* profiles filter */
2718 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2719 chunk_profiles_filter(chunk_type, bargs)) {
2720 return 0;
2721 }
2722
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002723 /* usage filter */
2724 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2725 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2726 return 0;
2727 }
2728
Ilya Dryomov409d4042012-01-16 22:04:47 +02002729 /* devid filter */
2730 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2731 chunk_devid_filter(leaf, chunk, bargs)) {
2732 return 0;
2733 }
2734
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002735 /* drange filter, makes sense only with devid filter */
2736 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2737 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2738 return 0;
2739 }
2740
Ilya Dryomovea671762012-01-16 22:04:48 +02002741 /* vrange filter */
2742 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2743 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2744 return 0;
2745 }
2746
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002747 /* soft profile changing mode */
2748 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2749 chunk_soft_convert_filter(chunk_type, bargs)) {
2750 return 0;
2751 }
2752
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002753 return 1;
2754}
2755
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002756static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002757{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002758 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002759 struct btrfs_root *chunk_root = fs_info->chunk_root;
2760 struct btrfs_root *dev_root = fs_info->dev_root;
2761 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002762 struct btrfs_device *device;
2763 u64 old_size;
2764 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002765 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002766 struct btrfs_path *path;
2767 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002768 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002769 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002770 struct extent_buffer *leaf;
2771 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002772 int ret;
2773 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002774 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002775
Chris Masonec44a352008-04-28 15:29:52 -04002776 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002777 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002778 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002779 old_size = device->total_bytes;
2780 size_to_free = div_factor(old_size, 1);
2781 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002782 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002783 device->total_bytes - device->bytes_used > size_to_free ||
2784 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002785 continue;
2786
2787 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002788 if (ret == -ENOSPC)
2789 break;
Chris Masonec44a352008-04-28 15:29:52 -04002790 BUG_ON(ret);
2791
Yan, Zhenga22285a2010-05-16 10:48:46 -04002792 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002793 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002794
2795 ret = btrfs_grow_device(trans, device, old_size);
2796 BUG_ON(ret);
2797
2798 btrfs_end_transaction(trans, dev_root);
2799 }
2800
2801 /* step two, relocate all the chunks */
2802 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002803 if (!path) {
2804 ret = -ENOMEM;
2805 goto error;
2806 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002807
2808 /* zero out stat counters */
2809 spin_lock(&fs_info->balance_lock);
2810 memset(&bctl->stat, 0, sizeof(bctl->stat));
2811 spin_unlock(&fs_info->balance_lock);
2812again:
Chris Masonec44a352008-04-28 15:29:52 -04002813 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2814 key.offset = (u64)-1;
2815 key.type = BTRFS_CHUNK_ITEM_KEY;
2816
Chris Masond3977122009-01-05 21:25:51 -05002817 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002818 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002819 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002820 ret = -ECANCELED;
2821 goto error;
2822 }
2823
Chris Masonec44a352008-04-28 15:29:52 -04002824 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2825 if (ret < 0)
2826 goto error;
2827
2828 /*
2829 * this shouldn't happen, it means the last relocate
2830 * failed
2831 */
2832 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002833 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002834
2835 ret = btrfs_previous_item(chunk_root, path, 0,
2836 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002837 if (ret) {
2838 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002839 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002840 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002841
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002842 leaf = path->nodes[0];
2843 slot = path->slots[0];
2844 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2845
Chris Masonec44a352008-04-28 15:29:52 -04002846 if (found_key.objectid != key.objectid)
2847 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002848
Chris Masonec44a352008-04-28 15:29:52 -04002849 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002850 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002851 break;
2852
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002853 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2854
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002855 if (!counting) {
2856 spin_lock(&fs_info->balance_lock);
2857 bctl->stat.considered++;
2858 spin_unlock(&fs_info->balance_lock);
2859 }
2860
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002861 ret = should_balance_chunk(chunk_root, leaf, chunk,
2862 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002863 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002864 if (!ret)
2865 goto loop;
2866
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002867 if (counting) {
2868 spin_lock(&fs_info->balance_lock);
2869 bctl->stat.expected++;
2870 spin_unlock(&fs_info->balance_lock);
2871 goto loop;
2872 }
2873
Chris Masonec44a352008-04-28 15:29:52 -04002874 ret = btrfs_relocate_chunk(chunk_root,
2875 chunk_root->root_key.objectid,
2876 found_key.objectid,
2877 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002878 if (ret && ret != -ENOSPC)
2879 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002880 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002881 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002882 } else {
2883 spin_lock(&fs_info->balance_lock);
2884 bctl->stat.completed++;
2885 spin_unlock(&fs_info->balance_lock);
2886 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002887loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002888 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002889 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002890
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002891 if (counting) {
2892 btrfs_release_path(path);
2893 counting = false;
2894 goto again;
2895 }
Chris Masonec44a352008-04-28 15:29:52 -04002896error:
2897 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002898 if (enospc_errors) {
2899 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2900 enospc_errors);
2901 if (!ret)
2902 ret = -ENOSPC;
2903 }
2904
Chris Masonec44a352008-04-28 15:29:52 -04002905 return ret;
2906}
2907
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002908/**
2909 * alloc_profile_is_valid - see if a given profile is valid and reduced
2910 * @flags: profile to validate
2911 * @extended: if true @flags is treated as an extended profile
2912 */
2913static int alloc_profile_is_valid(u64 flags, int extended)
2914{
2915 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2916 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2917
2918 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2919
2920 /* 1) check that all other bits are zeroed */
2921 if (flags & ~mask)
2922 return 0;
2923
2924 /* 2) see if profile is reduced */
2925 if (flags == 0)
2926 return !extended; /* "0" is valid for usual profiles */
2927
2928 /* true if exactly one bit set */
2929 return (flags & (flags - 1)) == 0;
2930}
2931
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002932static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2933{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002934 /* cancel requested || normal exit path */
2935 return atomic_read(&fs_info->balance_cancel_req) ||
2936 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2937 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002938}
2939
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002940static void __cancel_balance(struct btrfs_fs_info *fs_info)
2941{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002942 int ret;
2943
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002944 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002945 ret = del_balance_item(fs_info->tree_root);
2946 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002947}
2948
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002949void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002950 struct btrfs_ioctl_balance_args *bargs);
2951
2952/*
2953 * Should be called with both balance and volume mutexes held
2954 */
2955int btrfs_balance(struct btrfs_balance_control *bctl,
2956 struct btrfs_ioctl_balance_args *bargs)
2957{
2958 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002959 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002960 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002961 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01002962 u64 num_devices;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002963
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002964 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002965 atomic_read(&fs_info->balance_pause_req) ||
2966 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002967 ret = -EINVAL;
2968 goto out;
2969 }
2970
Ilya Dryomove4837f82012-03-27 17:09:17 +03002971 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2972 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2973 mixed = 1;
2974
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002975 /*
2976 * In case of mixed groups both data and meta should be picked,
2977 * and identical options should be given for both of them.
2978 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002979 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2980 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002981 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2982 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2983 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2984 printk(KERN_ERR "btrfs: with mixed groups data and "
2985 "metadata balance options must be the same\n");
2986 ret = -EINVAL;
2987 goto out;
2988 }
2989 }
2990
Stefan Behrens8dabb742012-11-06 13:15:27 +01002991 num_devices = fs_info->fs_devices->num_devices;
2992 btrfs_dev_replace_lock(&fs_info->dev_replace);
2993 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2994 BUG_ON(num_devices < 1);
2995 num_devices--;
2996 }
2997 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002998 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01002999 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003000 allowed |= BTRFS_BLOCK_GROUP_DUP;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003001 else if (num_devices < 4)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003002 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3003 else
3004 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
3005 BTRFS_BLOCK_GROUP_RAID10);
3006
Ilya Dryomov6728b192012-03-27 17:09:17 +03003007 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3008 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3009 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003010 printk(KERN_ERR "btrfs: unable to start balance with target "
3011 "data profile %llu\n",
3012 (unsigned long long)bctl->data.target);
3013 ret = -EINVAL;
3014 goto out;
3015 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003016 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3017 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3018 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003019 printk(KERN_ERR "btrfs: unable to start balance with target "
3020 "metadata profile %llu\n",
3021 (unsigned long long)bctl->meta.target);
3022 ret = -EINVAL;
3023 goto out;
3024 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003025 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3026 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3027 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003028 printk(KERN_ERR "btrfs: unable to start balance with target "
3029 "system profile %llu\n",
3030 (unsigned long long)bctl->sys.target);
3031 ret = -EINVAL;
3032 goto out;
3033 }
3034
Ilya Dryomove4837f82012-03-27 17:09:17 +03003035 /* allow dup'ed data chunks only in mixed mode */
3036 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003037 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003038 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3039 ret = -EINVAL;
3040 goto out;
3041 }
3042
3043 /* allow to reduce meta or sys integrity only if force set */
3044 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3045 BTRFS_BLOCK_GROUP_RAID10;
3046 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3047 (fs_info->avail_system_alloc_bits & allowed) &&
3048 !(bctl->sys.target & allowed)) ||
3049 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3050 (fs_info->avail_metadata_alloc_bits & allowed) &&
3051 !(bctl->meta.target & allowed))) {
3052 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3053 printk(KERN_INFO "btrfs: force reducing metadata "
3054 "integrity\n");
3055 } else {
3056 printk(KERN_ERR "btrfs: balance will reduce metadata "
3057 "integrity, use force if you want this\n");
3058 ret = -EINVAL;
3059 goto out;
3060 }
3061 }
3062
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003063 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3064 int num_tolerated_disk_barrier_failures;
3065 u64 target = bctl->sys.target;
3066
3067 num_tolerated_disk_barrier_failures =
3068 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3069 if (num_tolerated_disk_barrier_failures > 0 &&
3070 (target &
3071 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3072 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3073 num_tolerated_disk_barrier_failures = 0;
3074 else if (num_tolerated_disk_barrier_failures > 1 &&
3075 (target &
3076 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3077 num_tolerated_disk_barrier_failures = 1;
3078
3079 fs_info->num_tolerated_disk_barrier_failures =
3080 num_tolerated_disk_barrier_failures;
3081 }
3082
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003083 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003084 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003085 goto out;
3086
Ilya Dryomov59641012012-01-16 22:04:48 +02003087 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3088 BUG_ON(ret == -EEXIST);
3089 set_balance_control(bctl);
3090 } else {
3091 BUG_ON(ret != -EEXIST);
3092 spin_lock(&fs_info->balance_lock);
3093 update_balance_args(bctl);
3094 spin_unlock(&fs_info->balance_lock);
3095 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003096
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003097 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003098 mutex_unlock(&fs_info->balance_mutex);
3099
3100 ret = __btrfs_balance(fs_info);
3101
3102 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003103 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003104
3105 if (bargs) {
3106 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003107 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003108 }
3109
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003110 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3111 balance_need_close(fs_info)) {
3112 __cancel_balance(fs_info);
3113 }
3114
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003115 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3116 fs_info->num_tolerated_disk_barrier_failures =
3117 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3118 }
3119
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003120 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003121
3122 return ret;
3123out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003124 if (bctl->flags & BTRFS_BALANCE_RESUME)
3125 __cancel_balance(fs_info);
3126 else
3127 kfree(bctl);
3128 return ret;
3129}
3130
3131static int balance_kthread(void *data)
3132{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003133 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003134 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003135
3136 mutex_lock(&fs_info->volume_mutex);
3137 mutex_lock(&fs_info->balance_mutex);
3138
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003139 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003140 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003141 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003142 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003143
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003144 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov59641012012-01-16 22:04:48 +02003145 mutex_unlock(&fs_info->balance_mutex);
3146 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003147
Ilya Dryomov59641012012-01-16 22:04:48 +02003148 return ret;
3149}
3150
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003151int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3152{
3153 struct task_struct *tsk;
3154
3155 spin_lock(&fs_info->balance_lock);
3156 if (!fs_info->balance_ctl) {
3157 spin_unlock(&fs_info->balance_lock);
3158 return 0;
3159 }
3160 spin_unlock(&fs_info->balance_lock);
3161
3162 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3163 printk(KERN_INFO "btrfs: force skipping balance\n");
3164 return 0;
3165 }
3166
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003167 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003168 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3169 if (IS_ERR(tsk))
3170 return PTR_ERR(tsk);
3171
3172 return 0;
3173}
3174
Ilya Dryomov68310a52012-06-22 12:24:12 -06003175int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003176{
Ilya Dryomov59641012012-01-16 22:04:48 +02003177 struct btrfs_balance_control *bctl;
3178 struct btrfs_balance_item *item;
3179 struct btrfs_disk_balance_args disk_bargs;
3180 struct btrfs_path *path;
3181 struct extent_buffer *leaf;
3182 struct btrfs_key key;
3183 int ret;
3184
3185 path = btrfs_alloc_path();
3186 if (!path)
3187 return -ENOMEM;
3188
Ilya Dryomov68310a52012-06-22 12:24:12 -06003189 key.objectid = BTRFS_BALANCE_OBJECTID;
3190 key.type = BTRFS_BALANCE_ITEM_KEY;
3191 key.offset = 0;
3192
3193 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3194 if (ret < 0)
3195 goto out;
3196 if (ret > 0) { /* ret = -ENOENT; */
3197 ret = 0;
3198 goto out;
3199 }
3200
Ilya Dryomov59641012012-01-16 22:04:48 +02003201 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3202 if (!bctl) {
3203 ret = -ENOMEM;
3204 goto out;
3205 }
3206
Ilya Dryomov59641012012-01-16 22:04:48 +02003207 leaf = path->nodes[0];
3208 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3209
Ilya Dryomov68310a52012-06-22 12:24:12 -06003210 bctl->fs_info = fs_info;
3211 bctl->flags = btrfs_balance_flags(leaf, item);
3212 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003213
3214 btrfs_balance_data(leaf, item, &disk_bargs);
3215 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3216 btrfs_balance_meta(leaf, item, &disk_bargs);
3217 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3218 btrfs_balance_sys(leaf, item, &disk_bargs);
3219 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3220
Ilya Dryomov68310a52012-06-22 12:24:12 -06003221 mutex_lock(&fs_info->volume_mutex);
3222 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003223
Ilya Dryomov68310a52012-06-22 12:24:12 -06003224 set_balance_control(bctl);
3225
3226 mutex_unlock(&fs_info->balance_mutex);
3227 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003228out:
3229 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003230 return ret;
3231}
3232
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003233int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3234{
3235 int ret = 0;
3236
3237 mutex_lock(&fs_info->balance_mutex);
3238 if (!fs_info->balance_ctl) {
3239 mutex_unlock(&fs_info->balance_mutex);
3240 return -ENOTCONN;
3241 }
3242
3243 if (atomic_read(&fs_info->balance_running)) {
3244 atomic_inc(&fs_info->balance_pause_req);
3245 mutex_unlock(&fs_info->balance_mutex);
3246
3247 wait_event(fs_info->balance_wait_q,
3248 atomic_read(&fs_info->balance_running) == 0);
3249
3250 mutex_lock(&fs_info->balance_mutex);
3251 /* we are good with balance_ctl ripped off from under us */
3252 BUG_ON(atomic_read(&fs_info->balance_running));
3253 atomic_dec(&fs_info->balance_pause_req);
3254 } else {
3255 ret = -ENOTCONN;
3256 }
3257
3258 mutex_unlock(&fs_info->balance_mutex);
3259 return ret;
3260}
3261
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003262int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3263{
3264 mutex_lock(&fs_info->balance_mutex);
3265 if (!fs_info->balance_ctl) {
3266 mutex_unlock(&fs_info->balance_mutex);
3267 return -ENOTCONN;
3268 }
3269
3270 atomic_inc(&fs_info->balance_cancel_req);
3271 /*
3272 * if we are running just wait and return, balance item is
3273 * deleted in btrfs_balance in this case
3274 */
3275 if (atomic_read(&fs_info->balance_running)) {
3276 mutex_unlock(&fs_info->balance_mutex);
3277 wait_event(fs_info->balance_wait_q,
3278 atomic_read(&fs_info->balance_running) == 0);
3279 mutex_lock(&fs_info->balance_mutex);
3280 } else {
3281 /* __cancel_balance needs volume_mutex */
3282 mutex_unlock(&fs_info->balance_mutex);
3283 mutex_lock(&fs_info->volume_mutex);
3284 mutex_lock(&fs_info->balance_mutex);
3285
3286 if (fs_info->balance_ctl)
3287 __cancel_balance(fs_info);
3288
3289 mutex_unlock(&fs_info->volume_mutex);
3290 }
3291
3292 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3293 atomic_dec(&fs_info->balance_cancel_req);
3294 mutex_unlock(&fs_info->balance_mutex);
3295 return 0;
3296}
3297
Chris Mason8f18cf12008-04-25 16:53:30 -04003298/*
3299 * shrinking a device means finding all of the device extents past
3300 * the new size, and then following the back refs to the chunks.
3301 * The chunk relocation code actually frees the device extent
3302 */
3303int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3304{
3305 struct btrfs_trans_handle *trans;
3306 struct btrfs_root *root = device->dev_root;
3307 struct btrfs_dev_extent *dev_extent = NULL;
3308 struct btrfs_path *path;
3309 u64 length;
3310 u64 chunk_tree;
3311 u64 chunk_objectid;
3312 u64 chunk_offset;
3313 int ret;
3314 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003315 int failed = 0;
3316 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003317 struct extent_buffer *l;
3318 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003319 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003320 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003321 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003322 u64 diff = device->total_bytes - new_size;
3323
Stefan Behrens63a212a2012-11-05 18:29:28 +01003324 if (device->is_tgtdev_for_dev_replace)
3325 return -EINVAL;
3326
Chris Mason8f18cf12008-04-25 16:53:30 -04003327 path = btrfs_alloc_path();
3328 if (!path)
3329 return -ENOMEM;
3330
Chris Mason8f18cf12008-04-25 16:53:30 -04003331 path->reada = 2;
3332
Chris Mason7d9eb122008-07-08 14:19:17 -04003333 lock_chunks(root);
3334
Chris Mason8f18cf12008-04-25 16:53:30 -04003335 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003336 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003337 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003338 spin_lock(&root->fs_info->free_chunk_lock);
3339 root->fs_info->free_chunk_space -= diff;
3340 spin_unlock(&root->fs_info->free_chunk_lock);
3341 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003342 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003343
Josef Bacikba1bf482009-09-11 16:11:19 -04003344again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003345 key.objectid = device->devid;
3346 key.offset = (u64)-1;
3347 key.type = BTRFS_DEV_EXTENT_KEY;
3348
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003349 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003350 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3351 if (ret < 0)
3352 goto done;
3353
3354 ret = btrfs_previous_item(root, path, 0, key.type);
3355 if (ret < 0)
3356 goto done;
3357 if (ret) {
3358 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003359 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003360 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003361 }
3362
3363 l = path->nodes[0];
3364 slot = path->slots[0];
3365 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3366
Josef Bacikba1bf482009-09-11 16:11:19 -04003367 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003368 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003369 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003370 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003371
3372 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3373 length = btrfs_dev_extent_length(l, dev_extent);
3374
Josef Bacikba1bf482009-09-11 16:11:19 -04003375 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003376 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003377 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003378 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003379
3380 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3381 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3382 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003383 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003384
3385 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3386 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003387 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003388 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003389 if (ret == -ENOSPC)
3390 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003391 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003392
3393 if (failed && !retried) {
3394 failed = 0;
3395 retried = true;
3396 goto again;
3397 } else if (failed && retried) {
3398 ret = -ENOSPC;
3399 lock_chunks(root);
3400
3401 device->total_bytes = old_size;
3402 if (device->writeable)
3403 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003404 spin_lock(&root->fs_info->free_chunk_lock);
3405 root->fs_info->free_chunk_space += diff;
3406 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003407 unlock_chunks(root);
3408 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003409 }
3410
Chris Balld6397ba2009-04-27 07:29:03 -04003411 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003412 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003413 if (IS_ERR(trans)) {
3414 ret = PTR_ERR(trans);
3415 goto done;
3416 }
3417
Chris Balld6397ba2009-04-27 07:29:03 -04003418 lock_chunks(root);
3419
3420 device->disk_total_bytes = new_size;
3421 /* Now btrfs_update_device() will change the on-disk size. */
3422 ret = btrfs_update_device(trans, device);
3423 if (ret) {
3424 unlock_chunks(root);
3425 btrfs_end_transaction(trans, root);
3426 goto done;
3427 }
3428 WARN_ON(diff > old_total);
3429 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3430 unlock_chunks(root);
3431 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003432done:
3433 btrfs_free_path(path);
3434 return ret;
3435}
3436
Li Zefan125ccb02011-12-08 15:07:24 +08003437static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003438 struct btrfs_key *key,
3439 struct btrfs_chunk *chunk, int item_size)
3440{
David Sterba6c417612011-04-13 15:41:04 +02003441 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003442 struct btrfs_disk_key disk_key;
3443 u32 array_size;
3444 u8 *ptr;
3445
3446 array_size = btrfs_super_sys_array_size(super_copy);
3447 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3448 return -EFBIG;
3449
3450 ptr = super_copy->sys_chunk_array + array_size;
3451 btrfs_cpu_key_to_disk(&disk_key, key);
3452 memcpy(ptr, &disk_key, sizeof(disk_key));
3453 ptr += sizeof(disk_key);
3454 memcpy(ptr, chunk, item_size);
3455 item_size += sizeof(disk_key);
3456 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3457 return 0;
3458}
3459
Miao Xieb2117a32011-01-05 10:07:28 +00003460/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003461 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003462 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003463static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003464{
Arne Jansen73c5de02011-04-12 12:07:57 +02003465 const struct btrfs_device_info *di_a = a;
3466 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003467
Arne Jansen73c5de02011-04-12 12:07:57 +02003468 if (di_a->max_avail > di_b->max_avail)
3469 return -1;
3470 if (di_a->max_avail < di_b->max_avail)
3471 return 1;
3472 if (di_a->total_avail > di_b->total_avail)
3473 return -1;
3474 if (di_a->total_avail < di_b->total_avail)
3475 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003476 return 0;
3477}
3478
3479static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3480 struct btrfs_root *extent_root,
3481 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003482 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003483 u64 start, u64 type)
3484{
3485 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003486 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3487 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003488 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003489 struct extent_map_tree *em_tree;
3490 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003491 struct btrfs_device_info *devices_info = NULL;
3492 u64 total_avail;
3493 int num_stripes; /* total number of stripes to allocate */
3494 int sub_stripes; /* sub_stripes info for map */
3495 int dev_stripes; /* stripes per dev */
3496 int devs_max; /* max devs to use */
3497 int devs_min; /* min devs needed */
3498 int devs_increment; /* ndevs has to be a multiple of this */
3499 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003500 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003501 u64 max_stripe_size;
3502 u64 max_chunk_size;
3503 u64 stripe_size;
3504 u64 num_bytes;
3505 int ndevs;
3506 int i;
3507 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003508
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003509 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003510
Miao Xieb2117a32011-01-05 10:07:28 +00003511 if (list_empty(&fs_devices->alloc_list))
3512 return -ENOSPC;
3513
Arne Jansen73c5de02011-04-12 12:07:57 +02003514 sub_stripes = 1;
3515 dev_stripes = 1;
3516 devs_increment = 1;
3517 ncopies = 1;
3518 devs_max = 0; /* 0 == as many as possible */
3519 devs_min = 1;
3520
3521 /*
3522 * define the properties of each RAID type.
3523 * FIXME: move this to a global table and use it in all RAID
3524 * calculation code
3525 */
3526 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3527 dev_stripes = 2;
3528 ncopies = 2;
3529 devs_max = 1;
3530 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3531 devs_min = 2;
3532 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3533 devs_increment = 2;
3534 ncopies = 2;
3535 devs_max = 2;
3536 devs_min = 2;
3537 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3538 sub_stripes = 2;
3539 devs_increment = 2;
3540 ncopies = 2;
3541 devs_min = 4;
3542 } else {
3543 devs_max = 1;
3544 }
3545
3546 if (type & BTRFS_BLOCK_GROUP_DATA) {
3547 max_stripe_size = 1024 * 1024 * 1024;
3548 max_chunk_size = 10 * max_stripe_size;
3549 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003550 /* for larger filesystems, use larger metadata chunks */
3551 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3552 max_stripe_size = 1024 * 1024 * 1024;
3553 else
3554 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003555 max_chunk_size = max_stripe_size;
3556 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003557 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003558 max_chunk_size = 2 * max_stripe_size;
3559 } else {
3560 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3561 type);
3562 BUG_ON(1);
3563 }
3564
3565 /* we don't want a chunk larger than 10% of writeable space */
3566 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3567 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003568
3569 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3570 GFP_NOFS);
3571 if (!devices_info)
3572 return -ENOMEM;
3573
Arne Jansen73c5de02011-04-12 12:07:57 +02003574 cur = fs_devices->alloc_list.next;
3575
3576 /*
3577 * in the first pass through the devices list, we gather information
3578 * about the available holes on each device.
3579 */
3580 ndevs = 0;
3581 while (cur != &fs_devices->alloc_list) {
3582 struct btrfs_device *device;
3583 u64 max_avail;
3584 u64 dev_offset;
3585
3586 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3587
3588 cur = cur->next;
3589
3590 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003591 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003592 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003593 continue;
3594 }
3595
Stefan Behrens63a212a2012-11-05 18:29:28 +01003596 if (!device->in_fs_metadata ||
3597 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003598 continue;
3599
3600 if (device->total_bytes > device->bytes_used)
3601 total_avail = device->total_bytes - device->bytes_used;
3602 else
3603 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003604
3605 /* If there is no space on this device, skip it. */
3606 if (total_avail == 0)
3607 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003608
Li Zefan125ccb02011-12-08 15:07:24 +08003609 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003610 max_stripe_size * dev_stripes,
3611 &dev_offset, &max_avail);
3612 if (ret && ret != -ENOSPC)
3613 goto error;
3614
3615 if (ret == 0)
3616 max_avail = max_stripe_size * dev_stripes;
3617
3618 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3619 continue;
3620
3621 devices_info[ndevs].dev_offset = dev_offset;
3622 devices_info[ndevs].max_avail = max_avail;
3623 devices_info[ndevs].total_avail = total_avail;
3624 devices_info[ndevs].dev = device;
3625 ++ndevs;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003626 WARN_ON(ndevs > fs_devices->rw_devices);
Arne Jansen73c5de02011-04-12 12:07:57 +02003627 }
3628
3629 /*
3630 * now sort the devices by hole size / available space
3631 */
3632 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3633 btrfs_cmp_device_info, NULL);
3634
3635 /* round down to number of usable stripes */
3636 ndevs -= ndevs % devs_increment;
3637
3638 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3639 ret = -ENOSPC;
3640 goto error;
3641 }
3642
3643 if (devs_max && ndevs > devs_max)
3644 ndevs = devs_max;
3645 /*
3646 * the primary goal is to maximize the number of stripes, so use as many
3647 * devices as possible, even if the stripes are not maximum sized.
3648 */
3649 stripe_size = devices_info[ndevs-1].max_avail;
3650 num_stripes = ndevs * dev_stripes;
3651
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003652 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003653 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003654 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003655 }
3656
3657 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003658
3659 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003660 do_div(stripe_size, BTRFS_STRIPE_LEN);
3661 stripe_size *= BTRFS_STRIPE_LEN;
3662
Miao Xieb2117a32011-01-05 10:07:28 +00003663 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3664 if (!map) {
3665 ret = -ENOMEM;
3666 goto error;
3667 }
3668 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003669
Arne Jansen73c5de02011-04-12 12:07:57 +02003670 for (i = 0; i < ndevs; ++i) {
3671 for (j = 0; j < dev_stripes; ++j) {
3672 int s = i * dev_stripes + j;
3673 map->stripes[s].dev = devices_info[i].dev;
3674 map->stripes[s].physical = devices_info[i].dev_offset +
3675 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003676 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003677 }
Chris Mason593060d2008-03-25 16:50:33 -04003678 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003679 map->stripe_len = BTRFS_STRIPE_LEN;
3680 map->io_align = BTRFS_STRIPE_LEN;
3681 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003682 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003683 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003684
Yan Zheng2b820322008-11-17 21:11:30 -05003685 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003686 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003687
Arne Jansen73c5de02011-04-12 12:07:57 +02003688 *stripe_size_out = stripe_size;
3689 *num_bytes_out = num_bytes;
3690
3691 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003692
David Sterba172ddd62011-04-21 00:48:27 +02003693 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003694 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003695 ret = -ENOMEM;
3696 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003697 }
Chris Mason0b86a832008-03-24 15:01:56 -04003698 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003699 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003700 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003701 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003702 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003703
Chris Mason0b86a832008-03-24 15:01:56 -04003704 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003705 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003706 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003707 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003708 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003709 if (ret)
3710 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003711
3712 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3713 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003714 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003715 if (ret)
3716 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003717
Arne Jansen73c5de02011-04-12 12:07:57 +02003718 for (i = 0; i < map->num_stripes; ++i) {
3719 struct btrfs_device *device;
3720 u64 dev_offset;
3721
3722 device = map->stripes[i].dev;
3723 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003724
3725 ret = btrfs_alloc_dev_extent(trans, device,
3726 info->chunk_root->root_key.objectid,
3727 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003728 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003729 if (ret) {
3730 btrfs_abort_transaction(trans, extent_root, ret);
3731 goto error;
3732 }
Yan Zheng2b820322008-11-17 21:11:30 -05003733 }
3734
Miao Xieb2117a32011-01-05 10:07:28 +00003735 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003736 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003737
3738error:
3739 kfree(map);
3740 kfree(devices_info);
3741 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003742}
3743
3744static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3745 struct btrfs_root *extent_root,
3746 struct map_lookup *map, u64 chunk_offset,
3747 u64 chunk_size, u64 stripe_size)
3748{
3749 u64 dev_offset;
3750 struct btrfs_key key;
3751 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3752 struct btrfs_device *device;
3753 struct btrfs_chunk *chunk;
3754 struct btrfs_stripe *stripe;
3755 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3756 int index = 0;
3757 int ret;
3758
3759 chunk = kzalloc(item_size, GFP_NOFS);
3760 if (!chunk)
3761 return -ENOMEM;
3762
3763 index = 0;
3764 while (index < map->num_stripes) {
3765 device = map->stripes[index].dev;
3766 device->bytes_used += stripe_size;
3767 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003768 if (ret)
3769 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003770 index++;
3771 }
3772
Josef Bacik2bf64752011-09-26 17:12:22 -04003773 spin_lock(&extent_root->fs_info->free_chunk_lock);
3774 extent_root->fs_info->free_chunk_space -= (stripe_size *
3775 map->num_stripes);
3776 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3777
Yan Zheng2b820322008-11-17 21:11:30 -05003778 index = 0;
3779 stripe = &chunk->stripe;
3780 while (index < map->num_stripes) {
3781 device = map->stripes[index].dev;
3782 dev_offset = map->stripes[index].physical;
3783
3784 btrfs_set_stack_stripe_devid(stripe, device->devid);
3785 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3786 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3787 stripe++;
3788 index++;
3789 }
3790
3791 btrfs_set_stack_chunk_length(chunk, chunk_size);
3792 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3793 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3794 btrfs_set_stack_chunk_type(chunk, map->type);
3795 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3796 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3797 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3798 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3799 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3800
3801 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3802 key.type = BTRFS_CHUNK_ITEM_KEY;
3803 key.offset = chunk_offset;
3804
3805 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003806
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003807 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3808 /*
3809 * TODO: Cleanup of inserted chunk root in case of
3810 * failure.
3811 */
Li Zefan125ccb02011-12-08 15:07:24 +08003812 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003813 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003814 }
liubo1abe9b82011-03-24 11:18:59 +00003815
Mark Fasheh3acd3952011-09-08 17:40:01 -07003816out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003817 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003818 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003819}
3820
3821/*
3822 * Chunk allocation falls into two parts. The first part does works
3823 * that make the new allocated chunk useable, but not do any operation
3824 * that modifies the chunk tree. The second part does the works that
3825 * require modifying the chunk tree. This division is important for the
3826 * bootstrap process of adding storage to a seed btrfs.
3827 */
3828int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3829 struct btrfs_root *extent_root, u64 type)
3830{
3831 u64 chunk_offset;
3832 u64 chunk_size;
3833 u64 stripe_size;
3834 struct map_lookup *map;
3835 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3836 int ret;
3837
3838 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3839 &chunk_offset);
3840 if (ret)
3841 return ret;
3842
3843 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3844 &stripe_size, chunk_offset, type);
3845 if (ret)
3846 return ret;
3847
3848 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3849 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003850 if (ret)
3851 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003852 return 0;
3853}
3854
Chris Masond3977122009-01-05 21:25:51 -05003855static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003856 struct btrfs_root *root,
3857 struct btrfs_device *device)
3858{
3859 u64 chunk_offset;
3860 u64 sys_chunk_offset;
3861 u64 chunk_size;
3862 u64 sys_chunk_size;
3863 u64 stripe_size;
3864 u64 sys_stripe_size;
3865 u64 alloc_profile;
3866 struct map_lookup *map;
3867 struct map_lookup *sys_map;
3868 struct btrfs_fs_info *fs_info = root->fs_info;
3869 struct btrfs_root *extent_root = fs_info->extent_root;
3870 int ret;
3871
3872 ret = find_next_chunk(fs_info->chunk_root,
3873 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003874 if (ret)
3875 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003876
3877 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003878 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003879 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3880
3881 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3882 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003883 if (ret)
3884 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003885
3886 sys_chunk_offset = chunk_offset + chunk_size;
3887
3888 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003889 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003890 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3891
3892 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3893 &sys_chunk_size, &sys_stripe_size,
3894 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06003895 if (ret) {
3896 btrfs_abort_transaction(trans, root, ret);
3897 goto out;
3898 }
Yan Zheng2b820322008-11-17 21:11:30 -05003899
3900 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06003901 if (ret) {
3902 btrfs_abort_transaction(trans, root, ret);
3903 goto out;
3904 }
Yan Zheng2b820322008-11-17 21:11:30 -05003905
3906 /*
3907 * Modifying chunk tree needs allocating new blocks from both
3908 * system block group and metadata block group. So we only can
3909 * do operations require modifying the chunk tree after both
3910 * block groups were created.
3911 */
3912 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3913 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06003914 if (ret) {
3915 btrfs_abort_transaction(trans, root, ret);
3916 goto out;
3917 }
Yan Zheng2b820322008-11-17 21:11:30 -05003918
3919 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3920 sys_chunk_offset, sys_chunk_size,
3921 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003922 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06003923 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003924
David Sterba005d6422012-09-18 07:52:32 -06003925out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003926
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003927 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003928}
3929
3930int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3931{
3932 struct extent_map *em;
3933 struct map_lookup *map;
3934 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3935 int readonly = 0;
3936 int i;
3937
Chris Mason890871b2009-09-02 16:24:52 -04003938 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003939 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003940 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003941 if (!em)
3942 return 1;
3943
Josef Bacikf48b9072010-01-27 02:07:59 +00003944 if (btrfs_test_opt(root, DEGRADED)) {
3945 free_extent_map(em);
3946 return 0;
3947 }
3948
Yan Zheng2b820322008-11-17 21:11:30 -05003949 map = (struct map_lookup *)em->bdev;
3950 for (i = 0; i < map->num_stripes; i++) {
3951 if (!map->stripes[i].dev->writeable) {
3952 readonly = 1;
3953 break;
3954 }
3955 }
3956 free_extent_map(em);
3957 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003958}
3959
3960void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3961{
David Sterbaa8067e02011-04-21 00:34:43 +02003962 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003963}
3964
3965void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3966{
3967 struct extent_map *em;
3968
Chris Masond3977122009-01-05 21:25:51 -05003969 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003970 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003971 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3972 if (em)
3973 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003974 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003975 if (!em)
3976 break;
3977 kfree(em->bdev);
3978 /* once for us */
3979 free_extent_map(em);
3980 /* once for the tree */
3981 free_extent_map(em);
3982 }
3983}
3984
Stefan Behrens5d964052012-11-05 14:59:07 +01003985int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04003986{
Stefan Behrens5d964052012-11-05 14:59:07 +01003987 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04003988 struct extent_map *em;
3989 struct map_lookup *map;
3990 struct extent_map_tree *em_tree = &map_tree->map_tree;
3991 int ret;
3992
Chris Mason890871b2009-09-02 16:24:52 -04003993 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003994 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003995 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003996 BUG_ON(!em);
3997
3998 BUG_ON(em->start > logical || em->start + em->len < logical);
3999 map = (struct map_lookup *)em->bdev;
4000 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4001 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004002 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4003 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04004004 else
4005 ret = 1;
4006 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04004007 return ret;
4008}
4009
Chris Masondfe25022008-05-13 13:46:40 -04004010static int find_live_mirror(struct map_lookup *map, int first, int num,
4011 int optimal)
4012{
4013 int i;
4014 if (map->stripes[optimal].dev->bdev)
4015 return optimal;
4016 for (i = first; i < first + num; i++) {
4017 if (map->stripes[i].dev->bdev)
4018 return i;
4019 }
4020 /* we couldn't find one that doesn't fail. Just return something
4021 * and the io error handling code will clean up eventually
4022 */
4023 return optimal;
4024}
4025
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004026static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004027 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004028 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004029 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04004030{
4031 struct extent_map *em;
4032 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004033 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004034 struct extent_map_tree *em_tree = &map_tree->map_tree;
4035 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004036 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004037 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004038 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004039 u64 stripe_nr_orig;
4040 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04004041 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004042 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004043 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004044 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004045 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004046 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004047
Chris Mason890871b2009-09-02 16:24:52 -04004048 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004049 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004050 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004051
Chris Mason3b951512008-04-17 11:29:12 -04004052 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004053 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004054 (unsigned long long)logical,
4055 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04004056 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04004057 }
Chris Mason0b86a832008-03-24 15:01:56 -04004058
4059 BUG_ON(em->start > logical || em->start + em->len < logical);
4060 map = (struct map_lookup *)em->bdev;
4061 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004062
Chris Masonf1885912008-04-09 16:28:12 -04004063 if (mirror_num > map->num_stripes)
4064 mirror_num = 0;
4065
Chris Mason593060d2008-03-25 16:50:33 -04004066 stripe_nr = offset;
4067 /*
4068 * stripe_nr counts the total number of stripes we have to stride
4069 * to get to this block
4070 */
4071 do_div(stripe_nr, map->stripe_len);
4072
4073 stripe_offset = stripe_nr * map->stripe_len;
4074 BUG_ON(offset < stripe_offset);
4075
4076 /* stripe_offset is the offset of this block in its stripe*/
4077 stripe_offset = offset - stripe_offset;
4078
Li Dongyangfce3bb92011-03-24 10:24:26 +00004079 if (rw & REQ_DISCARD)
4080 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02004081 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04004082 /* we limit the length of each bio to what fits in a stripe */
4083 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00004084 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04004085 } else {
4086 *length = em->len - offset;
4087 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004088
Jan Schmidta1d3c472011-08-04 17:15:33 +02004089 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004090 goto out;
4091
Chris Masonf2d8d742008-04-21 10:03:05 -04004092 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004093 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004094 stripe_nr_orig = stripe_nr;
4095 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
4096 (~(map->stripe_len - 1));
4097 do_div(stripe_nr_end, map->stripe_len);
4098 stripe_end_offset = stripe_nr_end * map->stripe_len -
4099 (offset + *length);
4100 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4101 if (rw & REQ_DISCARD)
4102 num_stripes = min_t(u64, map->num_stripes,
4103 stripe_nr_end - stripe_nr_orig);
4104 stripe_index = do_div(stripe_nr, map->num_stripes);
4105 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07004106 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04004107 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004108 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004109 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004110 else {
4111 stripe_index = find_live_mirror(map, 0,
4112 map->num_stripes,
4113 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004114 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004115 }
Chris Mason2fff7342008-04-29 14:12:09 -04004116
Chris Mason611f0e02008-04-03 16:29:03 -04004117 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004118 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004119 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004120 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004121 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004122 } else {
4123 mirror_num = 1;
4124 }
Chris Mason2fff7342008-04-29 14:12:09 -04004125
Chris Mason321aecc2008-04-16 10:49:51 -04004126 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4127 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004128
4129 stripe_index = do_div(stripe_nr, factor);
4130 stripe_index *= map->sub_stripes;
4131
Jens Axboe7eaceac2011-03-10 08:52:07 +01004132 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04004133 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004134 else if (rw & REQ_DISCARD)
4135 num_stripes = min_t(u64, map->sub_stripes *
4136 (stripe_nr_end - stripe_nr_orig),
4137 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004138 else if (mirror_num)
4139 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004140 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004141 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04004142 stripe_index = find_live_mirror(map, stripe_index,
4143 map->sub_stripes, stripe_index +
4144 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04004145 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004146 }
Chris Mason8790d502008-04-03 16:29:03 -04004147 } else {
4148 /*
4149 * after this do_div call, stripe_nr is the number of stripes
4150 * on this device we have to walk to find the data, and
4151 * stripe_index is the number of our device in the stripe array
4152 */
4153 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004154 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004155 }
Chris Mason593060d2008-03-25 16:50:33 -04004156 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004157
Li Zefande11cc12011-12-01 12:55:47 +08004158 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
4159 if (!bbio) {
4160 ret = -ENOMEM;
4161 goto out;
4162 }
4163 atomic_set(&bbio->error, 0);
4164
Li Dongyangfce3bb92011-03-24 10:24:26 +00004165 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004166 int factor = 0;
4167 int sub_stripes = 0;
4168 u64 stripes_per_dev = 0;
4169 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004170 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004171
4172 if (map->type &
4173 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4174 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4175 sub_stripes = 1;
4176 else
4177 sub_stripes = map->sub_stripes;
4178
4179 factor = map->num_stripes / sub_stripes;
4180 stripes_per_dev = div_u64_rem(stripe_nr_end -
4181 stripe_nr_orig,
4182 factor,
4183 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004184 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4185 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004186 }
4187
Li Dongyangfce3bb92011-03-24 10:24:26 +00004188 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004189 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004190 map->stripes[stripe_index].physical +
4191 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004192 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004193
Li Zefanec9ef7a2011-12-01 14:06:42 +08004194 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4195 BTRFS_BLOCK_GROUP_RAID10)) {
4196 bbio->stripes[i].length = stripes_per_dev *
4197 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004198
Li Zefanec9ef7a2011-12-01 14:06:42 +08004199 if (i / sub_stripes < remaining_stripes)
4200 bbio->stripes[i].length +=
4201 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004202
4203 /*
4204 * Special for the first stripe and
4205 * the last stripe:
4206 *
4207 * |-------|...|-------|
4208 * |----------|
4209 * off end_off
4210 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004211 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004212 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004213 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004214
4215 if (stripe_index >= last_stripe &&
4216 stripe_index <= (last_stripe +
4217 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004218 bbio->stripes[i].length -=
4219 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004220
Li Zefanec9ef7a2011-12-01 14:06:42 +08004221 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004222 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004223 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004224 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004225
4226 stripe_index++;
4227 if (stripe_index == map->num_stripes) {
4228 /* This could only happen for RAID0/10 */
4229 stripe_index = 0;
4230 stripe_nr++;
4231 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004232 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004233 } else {
4234 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004235 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004236 map->stripes[stripe_index].physical +
4237 stripe_offset +
4238 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004239 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004240 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004241 stripe_index++;
4242 }
Chris Mason593060d2008-03-25 16:50:33 -04004243 }
Li Zefande11cc12011-12-01 12:55:47 +08004244
4245 if (rw & REQ_WRITE) {
4246 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4247 BTRFS_BLOCK_GROUP_RAID10 |
4248 BTRFS_BLOCK_GROUP_DUP)) {
4249 max_errors = 1;
4250 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004251 }
Li Zefande11cc12011-12-01 12:55:47 +08004252
4253 *bbio_ret = bbio;
4254 bbio->num_stripes = num_stripes;
4255 bbio->max_errors = max_errors;
4256 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04004257out:
Chris Mason0b86a832008-03-24 15:01:56 -04004258 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004259 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004260}
4261
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004262int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004263 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004264 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004265{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004266 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004267 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04004268}
4269
Yan Zhenga512bbf2008-12-08 16:46:26 -05004270int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4271 u64 chunk_start, u64 physical, u64 devid,
4272 u64 **logical, int *naddrs, int *stripe_len)
4273{
4274 struct extent_map_tree *em_tree = &map_tree->map_tree;
4275 struct extent_map *em;
4276 struct map_lookup *map;
4277 u64 *buf;
4278 u64 bytenr;
4279 u64 length;
4280 u64 stripe_nr;
4281 int i, j, nr = 0;
4282
Chris Mason890871b2009-09-02 16:24:52 -04004283 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004284 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004285 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004286
4287 BUG_ON(!em || em->start != chunk_start);
4288 map = (struct map_lookup *)em->bdev;
4289
4290 length = em->len;
4291 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4292 do_div(length, map->num_stripes / map->sub_stripes);
4293 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4294 do_div(length, map->num_stripes);
4295
4296 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004298
4299 for (i = 0; i < map->num_stripes; i++) {
4300 if (devid && map->stripes[i].dev->devid != devid)
4301 continue;
4302 if (map->stripes[i].physical > physical ||
4303 map->stripes[i].physical + length <= physical)
4304 continue;
4305
4306 stripe_nr = physical - map->stripes[i].physical;
4307 do_div(stripe_nr, map->stripe_len);
4308
4309 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4310 stripe_nr = stripe_nr * map->num_stripes + i;
4311 do_div(stripe_nr, map->sub_stripes);
4312 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4313 stripe_nr = stripe_nr * map->num_stripes + i;
4314 }
4315 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004316 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004317 for (j = 0; j < nr; j++) {
4318 if (buf[j] == bytenr)
4319 break;
4320 }
Chris Mason934d3752008-12-08 16:43:10 -05004321 if (j == nr) {
4322 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004323 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004324 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004325 }
4326
Yan Zhenga512bbf2008-12-08 16:46:26 -05004327 *logical = buf;
4328 *naddrs = nr;
4329 *stripe_len = map->stripe_len;
4330
4331 free_extent_map(em);
4332 return 0;
4333}
4334
Stefan Behrens442a4f62012-05-25 16:06:08 +02004335static void *merge_stripe_index_into_bio_private(void *bi_private,
4336 unsigned int stripe_index)
4337{
4338 /*
4339 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4340 * at most 1.
4341 * The alternative solution (instead of stealing bits from the
4342 * pointer) would be to allocate an intermediate structure
4343 * that contains the old private pointer plus the stripe_index.
4344 */
4345 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4346 BUG_ON(stripe_index > 3);
4347 return (void *)(((uintptr_t)bi_private) | stripe_index);
4348}
4349
4350static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4351{
4352 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4353}
4354
4355static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4356{
4357 return (unsigned int)((uintptr_t)bi_private) & 3;
4358}
4359
Jan Schmidta1d3c472011-08-04 17:15:33 +02004360static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004361{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004362 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004363 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004364
Stefan Behrens442a4f62012-05-25 16:06:08 +02004365 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004366 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004367 if (err == -EIO || err == -EREMOTEIO) {
4368 unsigned int stripe_index =
4369 extract_stripe_index_from_bio_private(
4370 bio->bi_private);
4371 struct btrfs_device *dev;
4372
4373 BUG_ON(stripe_index >= bbio->num_stripes);
4374 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004375 if (dev->bdev) {
4376 if (bio->bi_rw & WRITE)
4377 btrfs_dev_stat_inc(dev,
4378 BTRFS_DEV_STAT_WRITE_ERRS);
4379 else
4380 btrfs_dev_stat_inc(dev,
4381 BTRFS_DEV_STAT_READ_ERRS);
4382 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4383 btrfs_dev_stat_inc(dev,
4384 BTRFS_DEV_STAT_FLUSH_ERRS);
4385 btrfs_dev_stat_print_on_error(dev);
4386 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004387 }
4388 }
Chris Mason8790d502008-04-03 16:29:03 -04004389
Jan Schmidta1d3c472011-08-04 17:15:33 +02004390 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004391 is_orig_bio = 1;
4392
Jan Schmidta1d3c472011-08-04 17:15:33 +02004393 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004394 if (!is_orig_bio) {
4395 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004396 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004397 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004398 bio->bi_private = bbio->private;
4399 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2ca2011-06-16 12:05:19 +02004400 bio->bi_bdev = (struct block_device *)
4401 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004402 /* only send an error to the higher layers if it is
4403 * beyond the tolerance of the multi-bio
4404 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004405 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004406 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004407 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004408 /*
4409 * this bio is actually up to date, we didn't
4410 * go over the max number of errors
4411 */
4412 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004413 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004414 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004415 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004416
4417 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004418 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004419 bio_put(bio);
4420 }
Chris Mason8790d502008-04-03 16:29:03 -04004421}
4422
Chris Mason8b712842008-06-11 16:50:36 -04004423struct async_sched {
4424 struct bio *bio;
4425 int rw;
4426 struct btrfs_fs_info *info;
4427 struct btrfs_work work;
4428};
4429
4430/*
4431 * see run_scheduled_bios for a description of why bios are collected for
4432 * async submit.
4433 *
4434 * This will add one bio to the pending list for a device and make sure
4435 * the work struct is scheduled.
4436 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004437static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004438 struct btrfs_device *device,
4439 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004440{
4441 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004442 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004443
4444 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004445 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004446 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004447 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004448 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004449 return;
Chris Mason8b712842008-06-11 16:50:36 -04004450 }
4451
4452 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004453 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004454 * higher layers. Otherwise, the async bio makes it appear we have
4455 * made progress against dirty pages when we've really just put it
4456 * on a queue for later
4457 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004458 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004459 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004460 bio->bi_next = NULL;
4461 bio->bi_rw |= rw;
4462
4463 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004464 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004465 pending_bios = &device->pending_sync_bios;
4466 else
4467 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004468
Chris Masonffbd5172009-04-20 15:50:09 -04004469 if (pending_bios->tail)
4470 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004471
Chris Masonffbd5172009-04-20 15:50:09 -04004472 pending_bios->tail = bio;
4473 if (!pending_bios->head)
4474 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004475 if (device->running_pending)
4476 should_queue = 0;
4477
4478 spin_unlock(&device->io_lock);
4479
4480 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004481 btrfs_queue_worker(&root->fs_info->submit_workers,
4482 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004483}
4484
Josef Bacikde1ee922012-10-19 16:50:56 -04004485static int bio_size_ok(struct block_device *bdev, struct bio *bio,
4486 sector_t sector)
4487{
4488 struct bio_vec *prev;
4489 struct request_queue *q = bdev_get_queue(bdev);
4490 unsigned short max_sectors = queue_max_sectors(q);
4491 struct bvec_merge_data bvm = {
4492 .bi_bdev = bdev,
4493 .bi_sector = sector,
4494 .bi_rw = bio->bi_rw,
4495 };
4496
4497 if (bio->bi_vcnt == 0) {
4498 WARN_ON(1);
4499 return 1;
4500 }
4501
4502 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
4503 if ((bio->bi_size >> 9) > max_sectors)
4504 return 0;
4505
4506 if (!q->merge_bvec_fn)
4507 return 1;
4508
4509 bvm.bi_size = bio->bi_size - prev->bv_len;
4510 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
4511 return 0;
4512 return 1;
4513}
4514
4515static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4516 struct bio *bio, u64 physical, int dev_nr,
4517 int rw, int async)
4518{
4519 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
4520
4521 bio->bi_private = bbio;
4522 bio->bi_private = merge_stripe_index_into_bio_private(
4523 bio->bi_private, (unsigned int)dev_nr);
4524 bio->bi_end_io = btrfs_end_bio;
4525 bio->bi_sector = physical >> 9;
4526#ifdef DEBUG
4527 {
4528 struct rcu_string *name;
4529
4530 rcu_read_lock();
4531 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00004532 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04004533 "(%s id %llu), size=%u\n", rw,
4534 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4535 name->str, dev->devid, bio->bi_size);
4536 rcu_read_unlock();
4537 }
4538#endif
4539 bio->bi_bdev = dev->bdev;
4540 if (async)
4541 schedule_bio(root, dev, rw, bio);
4542 else
4543 btrfsic_submit_bio(rw, bio);
4544}
4545
4546static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4547 struct bio *first_bio, struct btrfs_device *dev,
4548 int dev_nr, int rw, int async)
4549{
4550 struct bio_vec *bvec = first_bio->bi_io_vec;
4551 struct bio *bio;
4552 int nr_vecs = bio_get_nr_vecs(dev->bdev);
4553 u64 physical = bbio->stripes[dev_nr].physical;
4554
4555again:
4556 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
4557 if (!bio)
4558 return -ENOMEM;
4559
4560 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
4561 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
4562 bvec->bv_offset) < bvec->bv_len) {
4563 u64 len = bio->bi_size;
4564
4565 atomic_inc(&bbio->stripes_pending);
4566 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
4567 rw, async);
4568 physical += len;
4569 goto again;
4570 }
4571 bvec++;
4572 }
4573
4574 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
4575 return 0;
4576}
4577
4578static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
4579{
4580 atomic_inc(&bbio->error);
4581 if (atomic_dec_and_test(&bbio->stripes_pending)) {
4582 bio->bi_private = bbio->private;
4583 bio->bi_end_io = bbio->end_io;
4584 bio->bi_bdev = (struct block_device *)
4585 (unsigned long)bbio->mirror_num;
4586 bio->bi_sector = logical >> 9;
4587 kfree(bbio);
4588 bio_endio(bio, -EIO);
4589 }
4590}
4591
Chris Masonf1885912008-04-09 16:28:12 -04004592int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004593 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004594{
Chris Mason0b86a832008-03-24 15:01:56 -04004595 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004596 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004597 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004598 u64 length = 0;
4599 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004600 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004601 int dev_nr = 0;
4602 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004603 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004604
Chris Masonf2d8d742008-04-21 10:03:05 -04004605 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004606 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004607
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004608 ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004609 mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01004610 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004611 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004612
Jan Schmidta1d3c472011-08-04 17:15:33 +02004613 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004614 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004615 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05004616 "len %llu\n", (unsigned long long)logical,
4617 (unsigned long long)length,
4618 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004619 BUG();
4620 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004621
4622 bbio->orig_bio = first_bio;
4623 bbio->private = first_bio->bi_private;
4624 bbio->end_io = first_bio->bi_end_io;
4625 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004626
Chris Masond3977122009-01-05 21:25:51 -05004627 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04004628 dev = bbio->stripes[dev_nr].dev;
4629 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
4630 bbio_error(bbio, first_bio, logical);
4631 dev_nr++;
4632 continue;
4633 }
4634
4635 /*
4636 * Check and see if we're ok with this bio based on it's size
4637 * and offset with the given device.
4638 */
4639 if (!bio_size_ok(dev->bdev, first_bio,
4640 bbio->stripes[dev_nr].physical >> 9)) {
4641 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
4642 dev_nr, rw, async_submit);
4643 BUG_ON(ret);
4644 dev_nr++;
4645 continue;
4646 }
4647
Jan Schmidta1d3c472011-08-04 17:15:33 +02004648 if (dev_nr < total_devs - 1) {
4649 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004650 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004651 } else {
4652 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004653 }
Josef Bacik606686e2012-06-04 14:03:51 -04004654
Josef Bacikde1ee922012-10-19 16:50:56 -04004655 submit_stripe_bio(root, bbio, bio,
4656 bbio->stripes[dev_nr].physical, dev_nr, rw,
4657 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04004658 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004659 }
Chris Mason0b86a832008-03-24 15:01:56 -04004660 return 0;
4661}
4662
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004663struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004664 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004665{
Yan Zheng2b820322008-11-17 21:11:30 -05004666 struct btrfs_device *device;
4667 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004668
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004669 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004670 while (cur_devices) {
4671 if (!fsid ||
4672 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4673 device = __find_device(&cur_devices->devices,
4674 devid, uuid);
4675 if (device)
4676 return device;
4677 }
4678 cur_devices = cur_devices->seed;
4679 }
4680 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004681}
4682
Chris Masondfe25022008-05-13 13:46:40 -04004683static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4684 u64 devid, u8 *dev_uuid)
4685{
4686 struct btrfs_device *device;
4687 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4688
4689 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004690 if (!device)
4691 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004692 list_add(&device->dev_list,
4693 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004694 device->dev_root = root->fs_info->dev_root;
4695 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004696 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004697 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004698 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004699 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004700 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004701 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004702 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004703 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4704 return device;
4705}
4706
Chris Mason0b86a832008-03-24 15:01:56 -04004707static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4708 struct extent_buffer *leaf,
4709 struct btrfs_chunk *chunk)
4710{
4711 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4712 struct map_lookup *map;
4713 struct extent_map *em;
4714 u64 logical;
4715 u64 length;
4716 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004717 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004718 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004719 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004720 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004721
Chris Masone17cade2008-04-15 15:41:47 -04004722 logical = key->offset;
4723 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004724
Chris Mason890871b2009-09-02 16:24:52 -04004725 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004726 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004727 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004728
4729 /* already mapped? */
4730 if (em && em->start <= logical && em->start + em->len > logical) {
4731 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004732 return 0;
4733 } else if (em) {
4734 free_extent_map(em);
4735 }
Chris Mason0b86a832008-03-24 15:01:56 -04004736
David Sterba172ddd62011-04-21 00:48:27 +02004737 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004738 if (!em)
4739 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004740 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4741 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004742 if (!map) {
4743 free_extent_map(em);
4744 return -ENOMEM;
4745 }
4746
4747 em->bdev = (struct block_device *)map;
4748 em->start = logical;
4749 em->len = length;
4750 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004751 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004752
Chris Mason593060d2008-03-25 16:50:33 -04004753 map->num_stripes = num_stripes;
4754 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4755 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4756 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4757 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4758 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004759 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004760 for (i = 0; i < num_stripes; i++) {
4761 map->stripes[i].physical =
4762 btrfs_stripe_offset_nr(leaf, chunk, i);
4763 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004764 read_extent_buffer(leaf, uuid, (unsigned long)
4765 btrfs_stripe_dev_uuid_nr(chunk, i),
4766 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004767 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
4768 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004769 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004770 kfree(map);
4771 free_extent_map(em);
4772 return -EIO;
4773 }
Chris Masondfe25022008-05-13 13:46:40 -04004774 if (!map->stripes[i].dev) {
4775 map->stripes[i].dev =
4776 add_missing_dev(root, devid, uuid);
4777 if (!map->stripes[i].dev) {
4778 kfree(map);
4779 free_extent_map(em);
4780 return -EIO;
4781 }
4782 }
4783 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004784 }
4785
Chris Mason890871b2009-09-02 16:24:52 -04004786 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004787 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004788 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004789 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004790 free_extent_map(em);
4791
4792 return 0;
4793}
4794
Jeff Mahoney143bede2012-03-01 14:56:26 +01004795static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004796 struct btrfs_dev_item *dev_item,
4797 struct btrfs_device *device)
4798{
4799 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004800
4801 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004802 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4803 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004804 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4805 device->type = btrfs_device_type(leaf, dev_item);
4806 device->io_align = btrfs_device_io_align(leaf, dev_item);
4807 device->io_width = btrfs_device_io_width(leaf, dev_item);
4808 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01004809 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004810 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004811
4812 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004813 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004814}
4815
Yan Zheng2b820322008-11-17 21:11:30 -05004816static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4817{
4818 struct btrfs_fs_devices *fs_devices;
4819 int ret;
4820
Li Zefanb367e472011-12-07 11:38:24 +08004821 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004822
4823 fs_devices = root->fs_info->fs_devices->seed;
4824 while (fs_devices) {
4825 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4826 ret = 0;
4827 goto out;
4828 }
4829 fs_devices = fs_devices->seed;
4830 }
4831
4832 fs_devices = find_fsid(fsid);
4833 if (!fs_devices) {
4834 ret = -ENOENT;
4835 goto out;
4836 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004837
4838 fs_devices = clone_fs_devices(fs_devices);
4839 if (IS_ERR(fs_devices)) {
4840 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004841 goto out;
4842 }
4843
Christoph Hellwig97288f22008-12-02 06:36:09 -05004844 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004845 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004846 if (ret) {
4847 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004848 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004849 }
Yan Zheng2b820322008-11-17 21:11:30 -05004850
4851 if (!fs_devices->seeding) {
4852 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004853 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004854 ret = -EINVAL;
4855 goto out;
4856 }
4857
4858 fs_devices->seed = root->fs_info->fs_devices->seed;
4859 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004860out:
Yan Zheng2b820322008-11-17 21:11:30 -05004861 return ret;
4862}
4863
Chris Mason0d81ba52008-03-24 15:02:07 -04004864static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004865 struct extent_buffer *leaf,
4866 struct btrfs_dev_item *dev_item)
4867{
4868 struct btrfs_device *device;
4869 u64 devid;
4870 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004871 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004872 u8 dev_uuid[BTRFS_UUID_SIZE];
4873
Chris Mason0b86a832008-03-24 15:01:56 -04004874 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004875 read_extent_buffer(leaf, dev_uuid,
4876 (unsigned long)btrfs_device_uuid(dev_item),
4877 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004878 read_extent_buffer(leaf, fs_uuid,
4879 (unsigned long)btrfs_device_fsid(dev_item),
4880 BTRFS_UUID_SIZE);
4881
4882 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4883 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004884 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004885 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004886 }
4887
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004888 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05004889 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004890 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004891 return -EIO;
4892
4893 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004894 printk(KERN_WARNING "warning devid %llu missing\n",
4895 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004896 device = add_missing_dev(root, devid, dev_uuid);
4897 if (!device)
4898 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004899 } else if (!device->missing) {
4900 /*
4901 * this happens when a device that was properly setup
4902 * in the device info lists suddenly goes bad.
4903 * device->bdev is NULL, and so we have to set
4904 * device->missing to one here
4905 */
4906 root->fs_info->fs_devices->missing_devices++;
4907 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004908 }
4909 }
4910
4911 if (device->fs_devices != root->fs_info->fs_devices) {
4912 BUG_ON(device->writeable);
4913 if (device->generation !=
4914 btrfs_device_generation(leaf, dev_item))
4915 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004916 }
Chris Mason0b86a832008-03-24 15:01:56 -04004917
4918 fill_device_from_item(leaf, dev_item, device);
4919 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004920 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01004921 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05004922 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004923 spin_lock(&root->fs_info->free_chunk_lock);
4924 root->fs_info->free_chunk_space += device->total_bytes -
4925 device->bytes_used;
4926 spin_unlock(&root->fs_info->free_chunk_lock);
4927 }
Chris Mason0b86a832008-03-24 15:01:56 -04004928 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004929 return ret;
4930}
4931
Yan Zhenge4404d62008-12-12 10:03:26 -05004932int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004933{
David Sterba6c417612011-04-13 15:41:04 +02004934 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004935 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004936 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004937 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004938 u8 *ptr;
4939 unsigned long sb_ptr;
4940 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004941 u32 num_stripes;
4942 u32 array_size;
4943 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004944 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004945 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004946
Yan Zhenge4404d62008-12-12 10:03:26 -05004947 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004948 BTRFS_SUPER_INFO_SIZE);
4949 if (!sb)
4950 return -ENOMEM;
4951 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004952 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004953 /*
4954 * The sb extent buffer is artifical and just used to read the system array.
4955 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4956 * pages up-to-date when the page is larger: extent does not cover the
4957 * whole page and consequently check_page_uptodate does not find all
4958 * the page's extents up-to-date (the hole beyond sb),
4959 * write_extent_buffer then triggers a WARN_ON.
4960 *
4961 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4962 * but sb spans only this function. Add an explicit SetPageUptodate call
4963 * to silence the warning eg. on PowerPC 64.
4964 */
4965 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004966 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004967
Chris Masona061fc82008-05-07 11:43:44 -04004968 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004969 array_size = btrfs_super_sys_array_size(super_copy);
4970
Chris Mason0b86a832008-03-24 15:01:56 -04004971 ptr = super_copy->sys_chunk_array;
4972 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4973 cur = 0;
4974
4975 while (cur < array_size) {
4976 disk_key = (struct btrfs_disk_key *)ptr;
4977 btrfs_disk_key_to_cpu(&key, disk_key);
4978
Chris Masona061fc82008-05-07 11:43:44 -04004979 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004980 sb_ptr += len;
4981 cur += len;
4982
Chris Mason0d81ba52008-03-24 15:02:07 -04004983 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004984 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004985 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004986 if (ret)
4987 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004988 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4989 len = btrfs_chunk_item_size(num_stripes);
4990 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004991 ret = -EIO;
4992 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004993 }
4994 ptr += len;
4995 sb_ptr += len;
4996 cur += len;
4997 }
Chris Masona061fc82008-05-07 11:43:44 -04004998 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004999 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005000}
5001
5002int btrfs_read_chunk_tree(struct btrfs_root *root)
5003{
5004 struct btrfs_path *path;
5005 struct extent_buffer *leaf;
5006 struct btrfs_key key;
5007 struct btrfs_key found_key;
5008 int ret;
5009 int slot;
5010
5011 root = root->fs_info->chunk_root;
5012
5013 path = btrfs_alloc_path();
5014 if (!path)
5015 return -ENOMEM;
5016
Li Zefanb367e472011-12-07 11:38:24 +08005017 mutex_lock(&uuid_mutex);
5018 lock_chunks(root);
5019
Chris Mason0b86a832008-03-24 15:01:56 -04005020 /* first we search for all of the device items, and then we
5021 * read in all of the chunk items. This way we can create chunk
5022 * mappings that reference all of the devices that are afound
5023 */
5024 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5025 key.offset = 0;
5026 key.type = 0;
5027again:
5028 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005029 if (ret < 0)
5030 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005031 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005032 leaf = path->nodes[0];
5033 slot = path->slots[0];
5034 if (slot >= btrfs_header_nritems(leaf)) {
5035 ret = btrfs_next_leaf(root, path);
5036 if (ret == 0)
5037 continue;
5038 if (ret < 0)
5039 goto error;
5040 break;
5041 }
5042 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5043 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5044 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5045 break;
5046 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5047 struct btrfs_dev_item *dev_item;
5048 dev_item = btrfs_item_ptr(leaf, slot,
5049 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005050 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005051 if (ret)
5052 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005053 }
5054 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5055 struct btrfs_chunk *chunk;
5056 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5057 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005058 if (ret)
5059 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005060 }
5061 path->slots[0]++;
5062 }
5063 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5064 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005065 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005066 goto again;
5067 }
Chris Mason0b86a832008-03-24 15:01:56 -04005068 ret = 0;
5069error:
Li Zefanb367e472011-12-07 11:38:24 +08005070 unlock_chunks(root);
5071 mutex_unlock(&uuid_mutex);
5072
Yan Zheng2b820322008-11-17 21:11:30 -05005073 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005074 return ret;
5075}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005076
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005077static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5078{
5079 int i;
5080
5081 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5082 btrfs_dev_stat_reset(dev, i);
5083}
5084
5085int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5086{
5087 struct btrfs_key key;
5088 struct btrfs_key found_key;
5089 struct btrfs_root *dev_root = fs_info->dev_root;
5090 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5091 struct extent_buffer *eb;
5092 int slot;
5093 int ret = 0;
5094 struct btrfs_device *device;
5095 struct btrfs_path *path = NULL;
5096 int i;
5097
5098 path = btrfs_alloc_path();
5099 if (!path) {
5100 ret = -ENOMEM;
5101 goto out;
5102 }
5103
5104 mutex_lock(&fs_devices->device_list_mutex);
5105 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5106 int item_size;
5107 struct btrfs_dev_stats_item *ptr;
5108
5109 key.objectid = 0;
5110 key.type = BTRFS_DEV_STATS_KEY;
5111 key.offset = device->devid;
5112 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5113 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005114 __btrfs_reset_dev_stats(device);
5115 device->dev_stats_valid = 1;
5116 btrfs_release_path(path);
5117 continue;
5118 }
5119 slot = path->slots[0];
5120 eb = path->nodes[0];
5121 btrfs_item_key_to_cpu(eb, &found_key, slot);
5122 item_size = btrfs_item_size_nr(eb, slot);
5123
5124 ptr = btrfs_item_ptr(eb, slot,
5125 struct btrfs_dev_stats_item);
5126
5127 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5128 if (item_size >= (1 + i) * sizeof(__le64))
5129 btrfs_dev_stat_set(device, i,
5130 btrfs_dev_stats_value(eb, ptr, i));
5131 else
5132 btrfs_dev_stat_reset(device, i);
5133 }
5134
5135 device->dev_stats_valid = 1;
5136 btrfs_dev_stat_print_on_load(device);
5137 btrfs_release_path(path);
5138 }
5139 mutex_unlock(&fs_devices->device_list_mutex);
5140
5141out:
5142 btrfs_free_path(path);
5143 return ret < 0 ? ret : 0;
5144}
5145
5146static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5147 struct btrfs_root *dev_root,
5148 struct btrfs_device *device)
5149{
5150 struct btrfs_path *path;
5151 struct btrfs_key key;
5152 struct extent_buffer *eb;
5153 struct btrfs_dev_stats_item *ptr;
5154 int ret;
5155 int i;
5156
5157 key.objectid = 0;
5158 key.type = BTRFS_DEV_STATS_KEY;
5159 key.offset = device->devid;
5160
5161 path = btrfs_alloc_path();
5162 BUG_ON(!path);
5163 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5164 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005165 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5166 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005167 goto out;
5168 }
5169
5170 if (ret == 0 &&
5171 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5172 /* need to delete old one and insert a new one */
5173 ret = btrfs_del_item(trans, dev_root, path);
5174 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005175 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5176 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005177 goto out;
5178 }
5179 ret = 1;
5180 }
5181
5182 if (ret == 1) {
5183 /* need to insert a new item */
5184 btrfs_release_path(path);
5185 ret = btrfs_insert_empty_item(trans, dev_root, path,
5186 &key, sizeof(*ptr));
5187 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005188 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5189 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005190 goto out;
5191 }
5192 }
5193
5194 eb = path->nodes[0];
5195 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5196 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5197 btrfs_set_dev_stats_value(eb, ptr, i,
5198 btrfs_dev_stat_read(device, i));
5199 btrfs_mark_buffer_dirty(eb);
5200
5201out:
5202 btrfs_free_path(path);
5203 return ret;
5204}
5205
5206/*
5207 * called from commit_transaction. Writes all changed device stats to disk.
5208 */
5209int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5210 struct btrfs_fs_info *fs_info)
5211{
5212 struct btrfs_root *dev_root = fs_info->dev_root;
5213 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5214 struct btrfs_device *device;
5215 int ret = 0;
5216
5217 mutex_lock(&fs_devices->device_list_mutex);
5218 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5219 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5220 continue;
5221
5222 ret = update_dev_stat_item(trans, dev_root, device);
5223 if (!ret)
5224 device->dev_stats_dirty = 0;
5225 }
5226 mutex_unlock(&fs_devices->device_list_mutex);
5227
5228 return ret;
5229}
5230
Stefan Behrens442a4f62012-05-25 16:06:08 +02005231void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5232{
5233 btrfs_dev_stat_inc(dev, index);
5234 btrfs_dev_stat_print_on_error(dev);
5235}
5236
5237void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5238{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005239 if (!dev->dev_stats_valid)
5240 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005241 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005242 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005243 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005244 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5245 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5246 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5247 btrfs_dev_stat_read(dev,
5248 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5249 btrfs_dev_stat_read(dev,
5250 BTRFS_DEV_STAT_GENERATION_ERRS));
5251}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005252
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005253static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5254{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005255 int i;
5256
5257 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5258 if (btrfs_dev_stat_read(dev, i) != 0)
5259 break;
5260 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5261 return; /* all values == 0, suppress message */
5262
Josef Bacik606686e2012-06-04 14:03:51 -04005263 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5264 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005265 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5266 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5267 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5268 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5269 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5270}
5271
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005272int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005273 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005274{
5275 struct btrfs_device *dev;
5276 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5277 int i;
5278
5279 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005280 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005281 mutex_unlock(&fs_devices->device_list_mutex);
5282
5283 if (!dev) {
5284 printk(KERN_WARNING
5285 "btrfs: get dev_stats failed, device not found\n");
5286 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005287 } else if (!dev->dev_stats_valid) {
5288 printk(KERN_WARNING
5289 "btrfs: get dev_stats failed, not yet valid\n");
5290 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005291 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005292 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5293 if (stats->nr_items > i)
5294 stats->values[i] =
5295 btrfs_dev_stat_read_and_reset(dev, i);
5296 else
5297 btrfs_dev_stat_reset(dev, i);
5298 }
5299 } else {
5300 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5301 if (stats->nr_items > i)
5302 stats->values[i] = btrfs_dev_stat_read(dev, i);
5303 }
5304 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5305 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5306 return 0;
5307}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005308
5309int btrfs_scratch_superblock(struct btrfs_device *device)
5310{
5311 struct buffer_head *bh;
5312 struct btrfs_super_block *disk_super;
5313
5314 bh = btrfs_read_dev_super(device->bdev);
5315 if (!bh)
5316 return -EINVAL;
5317 disk_super = (struct btrfs_super_block *)bh->b_data;
5318
5319 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5320 set_buffer_dirty(bh);
5321 sync_dirty_buffer(bh);
5322 brelse(bh);
5323
5324 return 0;
5325}