blob: 31f7af878d96ef010a36eb0c4e5df749d32ba082 [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"
Chris Mason0b86a832008-03-24 15:01:56 -040039
Yan Zheng2b820322008-11-17 21:11:30 -050040static int init_first_rw_device(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 struct btrfs_device *device);
43static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020044static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
45static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050046
Chris Mason8a4b83c2008-03-24 15:02:07 -040047static DEFINE_MUTEX(uuid_mutex);
48static LIST_HEAD(fs_uuids);
49
Chris Mason7d9eb122008-07-08 14:19:17 -040050static void lock_chunks(struct btrfs_root *root)
51{
Chris Mason7d9eb122008-07-08 14:19:17 -040052 mutex_lock(&root->fs_info->chunk_mutex);
53}
54
55static void unlock_chunks(struct btrfs_root *root)
56{
Chris Mason7d9eb122008-07-08 14:19:17 -040057 mutex_unlock(&root->fs_info->chunk_mutex);
58}
59
Yan Zhenge4404d62008-12-12 10:03:26 -050060static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
61{
62 struct btrfs_device *device;
63 WARN_ON(fs_devices->opened);
64 while (!list_empty(&fs_devices->devices)) {
65 device = list_entry(fs_devices->devices.next,
66 struct btrfs_device, dev_list);
67 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040068 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050069 kfree(device);
70 }
71 kfree(fs_devices);
72}
73
Jeff Mahoney143bede2012-03-01 14:56:26 +010074void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040075{
76 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040077
Yan Zheng2b820322008-11-17 21:11:30 -050078 while (!list_empty(&fs_uuids)) {
79 fs_devices = list_entry(fs_uuids.next,
80 struct btrfs_fs_devices, list);
81 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050082 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040083 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040084}
85
Chris Masona1b32a52008-09-05 16:09:51 -040086static noinline struct btrfs_device *__find_device(struct list_head *head,
87 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040088{
89 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040090
Qinghuang Fengc6e30872009-01-21 10:59:08 -050091 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040092 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040093 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040094 return dev;
Chris Masona4437552008-04-18 10:29:38 -040095 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040096 }
97 return NULL;
98}
99
Chris Masona1b32a52008-09-05 16:09:51 -0400100static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 struct btrfs_fs_devices *fs_devices;
103
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500104 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
106 return fs_devices;
107 }
108 return NULL;
109}
110
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100111static int
112btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
113 int flush, struct block_device **bdev,
114 struct buffer_head **bh)
115{
116 int ret;
117
118 *bdev = blkdev_get_by_path(device_path, flags, holder);
119
120 if (IS_ERR(*bdev)) {
121 ret = PTR_ERR(*bdev);
122 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
123 goto error;
124 }
125
126 if (flush)
127 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
128 ret = set_blocksize(*bdev, 4096);
129 if (ret) {
130 blkdev_put(*bdev, flags);
131 goto error;
132 }
133 invalidate_bdev(*bdev);
134 *bh = btrfs_read_dev_super(*bdev);
135 if (!*bh) {
136 ret = -EINVAL;
137 blkdev_put(*bdev, flags);
138 goto error;
139 }
140
141 return 0;
142
143error:
144 *bdev = NULL;
145 *bh = NULL;
146 return ret;
147}
148
Chris Masonffbd5172009-04-20 15:50:09 -0400149static void requeue_list(struct btrfs_pending_bios *pending_bios,
150 struct bio *head, struct bio *tail)
151{
152
153 struct bio *old_head;
154
155 old_head = pending_bios->head;
156 pending_bios->head = head;
157 if (pending_bios->tail)
158 tail->bi_next = old_head;
159 else
160 pending_bios->tail = tail;
161}
162
Chris Mason8b712842008-06-11 16:50:36 -0400163/*
164 * we try to collect pending bios for a device so we don't get a large
165 * number of procs sending bios down to the same device. This greatly
166 * improves the schedulers ability to collect and merge the bios.
167 *
168 * But, it also turns into a long list of bios to process and that is sure
169 * to eventually make the worker thread block. The solution here is to
170 * make some progress and then put this work struct back at the end of
171 * the list if the block device is congested. This way, multiple devices
172 * can make progress from a single worker thread.
173 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100174static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400175{
176 struct bio *pending;
177 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400178 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400179 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400180 struct bio *tail;
181 struct bio *cur;
182 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400183 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400184 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400185 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400186 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400187 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000188 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400189 struct blk_plug plug;
190
191 /*
192 * this function runs all the bios we've collected for
193 * a particular device. We don't want to wander off to
194 * another device without first sending all of these down.
195 * So, setup a plug here and finish it off before we return
196 */
197 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400198
Chris Masonbedf7622009-04-03 10:32:58 -0400199 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400200 fs_info = device->dev_root->fs_info;
201 limit = btrfs_async_submit_limit(fs_info);
202 limit = limit * 2 / 3;
203
Chris Mason8b712842008-06-11 16:50:36 -0400204loop:
205 spin_lock(&device->io_lock);
206
Chris Masona6837052009-02-04 09:19:41 -0500207loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400208 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400209
Chris Mason8b712842008-06-11 16:50:36 -0400210 /* take all the bios off the list at once and process them
211 * later on (without the lock held). But, remember the
212 * tail and other pointers so the bios can be properly reinserted
213 * into the list if we hit congestion
214 */
Chris Masond84275c2009-06-09 15:39:08 -0400215 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400216 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400217 force_reg = 1;
218 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400219 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400220 force_reg = 0;
221 }
Chris Masonffbd5172009-04-20 15:50:09 -0400222
223 pending = pending_bios->head;
224 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400225 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400226
227 /*
228 * if pending was null this time around, no bios need processing
229 * at all and we can stop. Otherwise it'll loop back up again
230 * and do an additional check so no bios are missed.
231 *
232 * device->running_pending is used to synchronize with the
233 * schedule_bio code.
234 */
Chris Masonffbd5172009-04-20 15:50:09 -0400235 if (device->pending_sync_bios.head == NULL &&
236 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400237 again = 0;
238 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400239 } else {
240 again = 1;
241 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400242 }
Chris Masonffbd5172009-04-20 15:50:09 -0400243
244 pending_bios->head = NULL;
245 pending_bios->tail = NULL;
246
Chris Mason8b712842008-06-11 16:50:36 -0400247 spin_unlock(&device->io_lock);
248
Chris Masond3977122009-01-05 21:25:51 -0500249 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400250
251 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400252 /* we want to work on both lists, but do more bios on the
253 * sync list than the regular list
254 */
255 if ((num_run > 32 &&
256 pending_bios != &device->pending_sync_bios &&
257 device->pending_sync_bios.head) ||
258 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
259 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400260 spin_lock(&device->io_lock);
261 requeue_list(pending_bios, pending, tail);
262 goto loop_lock;
263 }
264
Chris Mason8b712842008-06-11 16:50:36 -0400265 cur = pending;
266 pending = pending->bi_next;
267 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400268
Josef Bacik66657b32012-08-01 15:36:24 -0400269 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400270 waitqueue_active(&fs_info->async_submit_wait))
271 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400272
273 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400274
Chris Mason2ab1ba62011-08-04 14:28:36 -0400275 /*
276 * if we're doing the sync list, record that our
277 * plug has some sync requests on it
278 *
279 * If we're doing the regular list and there are
280 * sync requests sitting around, unplug before
281 * we add more
282 */
283 if (pending_bios == &device->pending_sync_bios) {
284 sync_pending = 1;
285 } else if (sync_pending) {
286 blk_finish_plug(&plug);
287 blk_start_plug(&plug);
288 sync_pending = 0;
289 }
290
Stefan Behrens21adbd52011-11-09 13:44:05 +0100291 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400292 num_run++;
293 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100294 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400295 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400296
297 /*
298 * we made progress, there is more work to do and the bdi
299 * is now congested. Back off and let other work structs
300 * run instead
301 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400302 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500303 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400304 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400305
Chris Masonb765ead2009-04-03 10:27:10 -0400306 ioc = current->io_context;
307
308 /*
309 * the main goal here is that we don't want to
310 * block if we're going to be able to submit
311 * more requests without blocking.
312 *
313 * This code does two great things, it pokes into
314 * the elevator code from a filesystem _and_
315 * it makes assumptions about how batching works.
316 */
317 if (ioc && ioc->nr_batch_requests > 0 &&
318 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
319 (last_waited == 0 ||
320 ioc->last_waited == last_waited)) {
321 /*
322 * we want to go through our batch of
323 * requests and stop. So, we copy out
324 * the ioc->last_waited time and test
325 * against it before looping
326 */
327 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100328 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400329 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400330 continue;
331 }
Chris Mason8b712842008-06-11 16:50:36 -0400332 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400333 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500334 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400335
336 spin_unlock(&device->io_lock);
337 btrfs_requeue_work(&device->work);
338 goto done;
339 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500340 /* unplug every 64 requests just for good measure */
341 if (batch_run % 64 == 0) {
342 blk_finish_plug(&plug);
343 blk_start_plug(&plug);
344 sync_pending = 0;
345 }
Chris Mason8b712842008-06-11 16:50:36 -0400346 }
Chris Masonffbd5172009-04-20 15:50:09 -0400347
Chris Mason51684082010-03-10 15:33:32 -0500348 cond_resched();
349 if (again)
350 goto loop;
351
352 spin_lock(&device->io_lock);
353 if (device->pending_bios.head || device->pending_sync_bios.head)
354 goto loop_lock;
355 spin_unlock(&device->io_lock);
356
Chris Mason8b712842008-06-11 16:50:36 -0400357done:
Chris Mason211588a2011-04-19 20:12:40 -0400358 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400359}
360
Christoph Hellwigb2950862008-12-02 09:54:17 -0500361static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400362{
363 struct btrfs_device *device;
364
365 device = container_of(work, struct btrfs_device, work);
366 run_scheduled_bios(device);
367}
368
Chris Masona1b32a52008-09-05 16:09:51 -0400369static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 struct btrfs_super_block *disk_super,
371 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
372{
373 struct btrfs_device *device;
374 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400375 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400376 u64 found_transid = btrfs_super_generation(disk_super);
377
378 fs_devices = find_fsid(disk_super->fsid);
379 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400380 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400381 if (!fs_devices)
382 return -ENOMEM;
383 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400384 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400385 list_add(&fs_devices->list, &fs_uuids);
386 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
387 fs_devices->latest_devid = devid;
388 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400389 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400390 device = NULL;
391 } else {
Chris Masona4437552008-04-18 10:29:38 -0400392 device = __find_device(&fs_devices->devices, devid,
393 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400394 }
395 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500396 if (fs_devices->opened)
397 return -EBUSY;
398
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 device = kzalloc(sizeof(*device), GFP_NOFS);
400 if (!device) {
401 /* we can safely leave the fs_devices entry around */
402 return -ENOMEM;
403 }
404 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200405 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400406 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400407 memcpy(device->uuid, disk_super->dev_item.uuid,
408 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400409 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400410
411 name = rcu_string_strdup(path, GFP_NOFS);
412 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400413 kfree(device);
414 return -ENOMEM;
415 }
Josef Bacik606686e2012-06-04 14:03:51 -0400416 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500417 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400418
Arne Jansen90519d62011-05-23 14:30:00 +0200419 /* init readahead state */
420 spin_lock_init(&device->reada_lock);
421 device->reada_curr_zone = NULL;
422 atomic_set(&device->reada_in_flight, 0);
423 device->reada_next = 0;
424 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
425 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
426
Chris Masone5e9a522009-06-10 15:17:02 -0400427 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000428 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400429 mutex_unlock(&fs_devices->device_list_mutex);
430
Yan Zheng2b820322008-11-17 21:11:30 -0500431 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400432 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400433 } else if (!device->name || strcmp(device->name->str, path)) {
434 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000435 if (!name)
436 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400437 rcu_string_free(device->name);
438 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500439 if (device->missing) {
440 fs_devices->missing_devices--;
441 device->missing = 0;
442 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400443 }
444
445 if (found_transid > fs_devices->latest_trans) {
446 fs_devices->latest_devid = devid;
447 fs_devices->latest_trans = found_transid;
448 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400449 *fs_devices_ret = fs_devices;
450 return 0;
451}
452
Yan Zhenge4404d62008-12-12 10:03:26 -0500453static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
454{
455 struct btrfs_fs_devices *fs_devices;
456 struct btrfs_device *device;
457 struct btrfs_device *orig_dev;
458
459 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
460 if (!fs_devices)
461 return ERR_PTR(-ENOMEM);
462
463 INIT_LIST_HEAD(&fs_devices->devices);
464 INIT_LIST_HEAD(&fs_devices->alloc_list);
465 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400466 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500467 fs_devices->latest_devid = orig->latest_devid;
468 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400469 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500470 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
471
Xiao Guangrong46224702011-04-20 10:08:47 +0000472 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500473 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400474 struct rcu_string *name;
475
Yan Zhenge4404d62008-12-12 10:03:26 -0500476 device = kzalloc(sizeof(*device), GFP_NOFS);
477 if (!device)
478 goto error;
479
Josef Bacik606686e2012-06-04 14:03:51 -0400480 /*
481 * This is ok to do without rcu read locked because we hold the
482 * uuid mutex so nothing we touch in here is going to disappear.
483 */
484 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
485 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400486 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500487 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400488 }
Josef Bacik606686e2012-06-04 14:03:51 -0400489 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500490
491 device->devid = orig_dev->devid;
492 device->work.func = pending_bios_fn;
493 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500494 spin_lock_init(&device->io_lock);
495 INIT_LIST_HEAD(&device->dev_list);
496 INIT_LIST_HEAD(&device->dev_alloc_list);
497
498 list_add(&device->dev_list, &fs_devices->devices);
499 device->fs_devices = fs_devices;
500 fs_devices->num_devices++;
501 }
502 return fs_devices;
503error:
504 free_fs_devices(fs_devices);
505 return ERR_PTR(-ENOMEM);
506}
507
Jeff Mahoney143bede2012-03-01 14:56:26 +0100508void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400509{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500510 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400511
Chris Masona6b0d5c2012-02-20 20:53:43 -0500512 struct block_device *latest_bdev = NULL;
513 u64 latest_devid = 0;
514 u64 latest_transid = 0;
515
Chris Masondfe25022008-05-13 13:46:40 -0400516 mutex_lock(&uuid_mutex);
517again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000518 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500519 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500520 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100521 if (!device->is_tgtdev_for_dev_replace &&
522 (!latest_transid ||
523 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500524 latest_devid = device->devid;
525 latest_transid = device->generation;
526 latest_bdev = device->bdev;
527 }
Yan Zheng2b820322008-11-17 21:11:30 -0500528 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500529 }
Yan Zheng2b820322008-11-17 21:11:30 -0500530
531 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100532 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500533 device->bdev = NULL;
534 fs_devices->open_devices--;
535 }
536 if (device->writeable) {
537 list_del_init(&device->dev_alloc_list);
538 device->writeable = 0;
539 fs_devices->rw_devices--;
540 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500541 list_del_init(&device->dev_list);
542 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400543 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500544 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400545 }
Yan Zheng2b820322008-11-17 21:11:30 -0500546
547 if (fs_devices->seed) {
548 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500549 goto again;
550 }
551
Chris Masona6b0d5c2012-02-20 20:53:43 -0500552 fs_devices->latest_bdev = latest_bdev;
553 fs_devices->latest_devid = latest_devid;
554 fs_devices->latest_trans = latest_transid;
555
Chris Masondfe25022008-05-13 13:46:40 -0400556 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400557}
Chris Masona0af4692008-05-13 16:03:06 -0400558
Xiao Guangrong1f781602011-04-20 10:09:16 +0000559static void __free_device(struct work_struct *work)
560{
561 struct btrfs_device *device;
562
563 device = container_of(work, struct btrfs_device, rcu_work);
564
565 if (device->bdev)
566 blkdev_put(device->bdev, device->mode);
567
Josef Bacik606686e2012-06-04 14:03:51 -0400568 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000569 kfree(device);
570}
571
572static void free_device(struct rcu_head *head)
573{
574 struct btrfs_device *device;
575
576 device = container_of(head, struct btrfs_device, rcu);
577
578 INIT_WORK(&device->rcu_work, __free_device);
579 schedule_work(&device->rcu_work);
580}
581
Yan Zheng2b820322008-11-17 21:11:30 -0500582static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400583{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400584 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500585
Yan Zheng2b820322008-11-17 21:11:30 -0500586 if (--fs_devices->opened > 0)
587 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000589 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500590 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000591 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400592 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000593
594 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400595 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000596
Yan Zheng2b820322008-11-17 21:11:30 -0500597 if (device->writeable) {
598 list_del_init(&device->dev_alloc_list);
599 fs_devices->rw_devices--;
600 }
601
Josef Bacikd5e20032011-08-04 14:52:27 +0000602 if (device->can_discard)
603 fs_devices->num_can_discard--;
604
Xiao Guangrong1f781602011-04-20 10:09:16 +0000605 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100606 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000607 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400608
609 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400610 if (device->name) {
611 name = rcu_string_strdup(device->name->str, GFP_NOFS);
612 BUG_ON(device->name && !name); /* -ENOMEM */
613 rcu_assign_pointer(new_device->name, name);
614 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000615 new_device->bdev = NULL;
616 new_device->writeable = 0;
617 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000618 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000619 list_replace_rcu(&device->dev_list, &new_device->dev_list);
620
621 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000623 mutex_unlock(&fs_devices->device_list_mutex);
624
Yan Zhenge4404d62008-12-12 10:03:26 -0500625 WARN_ON(fs_devices->open_devices);
626 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500627 fs_devices->opened = 0;
628 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500629
Chris Mason8a4b83c2008-03-24 15:02:07 -0400630 return 0;
631}
632
Yan Zheng2b820322008-11-17 21:11:30 -0500633int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
634{
Yan Zhenge4404d62008-12-12 10:03:26 -0500635 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500636 int ret;
637
638 mutex_lock(&uuid_mutex);
639 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500640 if (!fs_devices->opened) {
641 seed_devices = fs_devices->seed;
642 fs_devices->seed = NULL;
643 }
Yan Zheng2b820322008-11-17 21:11:30 -0500644 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500645
646 while (seed_devices) {
647 fs_devices = seed_devices;
648 seed_devices = fs_devices->seed;
649 __btrfs_close_devices(fs_devices);
650 free_fs_devices(fs_devices);
651 }
Yan Zheng2b820322008-11-17 21:11:30 -0500652 return ret;
653}
654
Yan Zhenge4404d62008-12-12 10:03:26 -0500655static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
656 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400657{
Josef Bacikd5e20032011-08-04 14:52:27 +0000658 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400659 struct block_device *bdev;
660 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400661 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400662 struct block_device *latest_bdev = NULL;
663 struct buffer_head *bh;
664 struct btrfs_super_block *disk_super;
665 u64 latest_devid = 0;
666 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400667 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500668 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400669 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400670
Tejun Heod4d77622010-11-13 11:55:18 +0100671 flags |= FMODE_EXCL;
672
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500673 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400674 if (device->bdev)
675 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400676 if (!device->name)
677 continue;
678
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100679 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
680 &bdev, &bh);
681 if (ret)
682 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400683
684 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000685 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400686 if (devid != device->devid)
687 goto error_brelse;
688
Yan Zheng2b820322008-11-17 21:11:30 -0500689 if (memcmp(device->uuid, disk_super->dev_item.uuid,
690 BTRFS_UUID_SIZE))
691 goto error_brelse;
692
693 device->generation = btrfs_super_generation(disk_super);
694 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400695 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500696 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400697 latest_bdev = bdev;
698 }
699
Yan Zheng2b820322008-11-17 21:11:30 -0500700 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
701 device->writeable = 0;
702 } else {
703 device->writeable = !bdev_read_only(bdev);
704 seeding = 0;
705 }
706
Josef Bacikd5e20032011-08-04 14:52:27 +0000707 q = bdev_get_queue(bdev);
708 if (blk_queue_discard(q)) {
709 device->can_discard = 1;
710 fs_devices->num_can_discard++;
711 }
712
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400714 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500715 device->mode = flags;
716
Chris Masonc2898112009-06-10 09:51:32 -0400717 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
718 fs_devices->rotating = 1;
719
Chris Masona0af4692008-05-13 16:03:06 -0400720 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500721 if (device->writeable) {
722 fs_devices->rw_devices++;
723 list_add(&device->dev_alloc_list,
724 &fs_devices->alloc_list);
725 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000726 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400727 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400728
Chris Masona0af4692008-05-13 16:03:06 -0400729error_brelse:
730 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100731 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400732 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400733 }
Chris Masona0af4692008-05-13 16:03:06 -0400734 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300735 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400736 goto out;
737 }
Yan Zheng2b820322008-11-17 21:11:30 -0500738 fs_devices->seeding = seeding;
739 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400740 fs_devices->latest_bdev = latest_bdev;
741 fs_devices->latest_devid = latest_devid;
742 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500743 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400744out:
Yan Zheng2b820322008-11-17 21:11:30 -0500745 return ret;
746}
747
748int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500749 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500750{
751 int ret;
752
753 mutex_lock(&uuid_mutex);
754 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500755 fs_devices->opened++;
756 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500757 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500758 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500759 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400761 return ret;
762}
763
Christoph Hellwig97288f22008-12-02 06:36:09 -0500764int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765 struct btrfs_fs_devices **fs_devices_ret)
766{
767 struct btrfs_super_block *disk_super;
768 struct block_device *bdev;
769 struct buffer_head *bh;
770 int ret;
771 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400772 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400773 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400774
Tejun Heod4d77622010-11-13 11:55:18 +0100775 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500776 mutex_lock(&uuid_mutex);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100777 ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400778 if (ret)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100779 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400780 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000781 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400782 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400783 total_devices = btrfs_super_num_devices(disk_super);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000784 if (disk_super->label[0]) {
785 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
786 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500787 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000788 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200789 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000790 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500791 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500792 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400793 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400794 if (!ret && fs_devices_ret)
795 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400796 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100797 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400798error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100799 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400800 return ret;
801}
Chris Mason0b86a832008-03-24 15:01:56 -0400802
Miao Xie6d07bce2011-01-05 10:07:31 +0000803/* helper to account the used device space in the range */
804int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
805 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400806{
807 struct btrfs_key key;
808 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000809 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500810 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000811 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400812 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000813 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400814 struct extent_buffer *l;
815
Miao Xie6d07bce2011-01-05 10:07:31 +0000816 *length = 0;
817
Stefan Behrens63a212a2012-11-05 18:29:28 +0100818 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000819 return 0;
820
Yan Zheng2b820322008-11-17 21:11:30 -0500821 path = btrfs_alloc_path();
822 if (!path)
823 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400824 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400825
Chris Mason0b86a832008-03-24 15:01:56 -0400826 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000827 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400828 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000829
830 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400831 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000832 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400833 if (ret > 0) {
834 ret = btrfs_previous_item(root, path, key.objectid, key.type);
835 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000836 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400837 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000838
Chris Mason0b86a832008-03-24 15:01:56 -0400839 while (1) {
840 l = path->nodes[0];
841 slot = path->slots[0];
842 if (slot >= btrfs_header_nritems(l)) {
843 ret = btrfs_next_leaf(root, path);
844 if (ret == 0)
845 continue;
846 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000847 goto out;
848
849 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400850 }
851 btrfs_item_key_to_cpu(l, &key, slot);
852
853 if (key.objectid < device->devid)
854 goto next;
855
856 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000857 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400858
Chris Masond3977122009-01-05 21:25:51 -0500859 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400860 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400861
Chris Mason0b86a832008-03-24 15:01:56 -0400862 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000863 extent_end = key.offset + btrfs_dev_extent_length(l,
864 dev_extent);
865 if (key.offset <= start && extent_end > end) {
866 *length = end - start + 1;
867 break;
868 } else if (key.offset <= start && extent_end > start)
869 *length += extent_end - start;
870 else if (key.offset > start && extent_end <= end)
871 *length += extent_end - key.offset;
872 else if (key.offset > start && key.offset <= end) {
873 *length += end - key.offset + 1;
874 break;
875 } else if (key.offset > end)
876 break;
877
878next:
879 path->slots[0]++;
880 }
881 ret = 0;
882out:
883 btrfs_free_path(path);
884 return ret;
885}
886
Chris Mason0b86a832008-03-24 15:01:56 -0400887/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000888 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000889 * @device: the device which we search the free space in
890 * @num_bytes: the size of the free space that we need
891 * @start: store the start of the free space.
892 * @len: the size of the free space. that we find, or the size of the max
893 * free space if we don't find suitable free space
894 *
Chris Mason0b86a832008-03-24 15:01:56 -0400895 * this uses a pretty simple search, the expectation is that it is
896 * called very infrequently and that a given device has a small number
897 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000898 *
899 * @start is used to store the start of the free space if we find. But if we
900 * don't find suitable free space, it will be used to store the start position
901 * of the max free space.
902 *
903 * @len is used to store the size of the free space that we find.
904 * But if we don't find suitable free space, it is used to store the size of
905 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400906 */
Li Zefan125ccb02011-12-08 15:07:24 +0800907int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000908 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400909{
910 struct btrfs_key key;
911 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000912 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400913 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000914 u64 hole_size;
915 u64 max_hole_start;
916 u64 max_hole_size;
917 u64 extent_end;
918 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400919 u64 search_end = device->total_bytes;
920 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000921 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400922 struct extent_buffer *l;
923
Chris Mason0b86a832008-03-24 15:01:56 -0400924 /* FIXME use last free of some kind */
925
926 /* we don't want to overwrite the superblock on the drive,
927 * so we make sure to start at an offset of at least 1MB
928 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200929 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400930
Miao Xie7bfc8372011-01-05 10:07:26 +0000931 max_hole_start = search_start;
932 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000933 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000934
Stefan Behrens63a212a2012-11-05 18:29:28 +0100935 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +0000936 ret = -ENOSPC;
937 goto error;
938 }
939
940 path = btrfs_alloc_path();
941 if (!path) {
942 ret = -ENOMEM;
943 goto error;
944 }
945 path->reada = 2;
946
Chris Mason0b86a832008-03-24 15:01:56 -0400947 key.objectid = device->devid;
948 key.offset = search_start;
949 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000950
Li Zefan125ccb02011-12-08 15:07:24 +0800951 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400952 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000953 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400954 if (ret > 0) {
955 ret = btrfs_previous_item(root, path, key.objectid, key.type);
956 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000957 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400958 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000959
Chris Mason0b86a832008-03-24 15:01:56 -0400960 while (1) {
961 l = path->nodes[0];
962 slot = path->slots[0];
963 if (slot >= btrfs_header_nritems(l)) {
964 ret = btrfs_next_leaf(root, path);
965 if (ret == 0)
966 continue;
967 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000968 goto out;
969
970 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400971 }
972 btrfs_item_key_to_cpu(l, &key, slot);
973
974 if (key.objectid < device->devid)
975 goto next;
976
977 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000978 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400979
Chris Mason0b86a832008-03-24 15:01:56 -0400980 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
981 goto next;
982
Miao Xie7bfc8372011-01-05 10:07:26 +0000983 if (key.offset > search_start) {
984 hole_size = key.offset - search_start;
985
986 if (hole_size > max_hole_size) {
987 max_hole_start = search_start;
988 max_hole_size = hole_size;
989 }
990
991 /*
992 * If this free space is greater than which we need,
993 * it must be the max free space that we have found
994 * until now, so max_hole_start must point to the start
995 * of this free space and the length of this free space
996 * is stored in max_hole_size. Thus, we return
997 * max_hole_start and max_hole_size and go back to the
998 * caller.
999 */
1000 if (hole_size >= num_bytes) {
1001 ret = 0;
1002 goto out;
1003 }
1004 }
1005
Chris Mason0b86a832008-03-24 15:01:56 -04001006 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001007 extent_end = key.offset + btrfs_dev_extent_length(l,
1008 dev_extent);
1009 if (extent_end > search_start)
1010 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001011next:
1012 path->slots[0]++;
1013 cond_resched();
1014 }
Chris Mason0b86a832008-03-24 15:01:56 -04001015
liubo38c01b92011-08-02 02:39:03 +00001016 /*
1017 * At this point, search_start should be the end of
1018 * allocated dev extents, and when shrinking the device,
1019 * search_end may be smaller than search_start.
1020 */
1021 if (search_end > search_start)
1022 hole_size = search_end - search_start;
1023
Miao Xie7bfc8372011-01-05 10:07:26 +00001024 if (hole_size > max_hole_size) {
1025 max_hole_start = search_start;
1026 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001027 }
Chris Mason0b86a832008-03-24 15:01:56 -04001028
Miao Xie7bfc8372011-01-05 10:07:26 +00001029 /* See above. */
1030 if (hole_size < num_bytes)
1031 ret = -ENOSPC;
1032 else
1033 ret = 0;
1034
1035out:
Yan Zheng2b820322008-11-17 21:11:30 -05001036 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001037error:
1038 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001039 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001040 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001041 return ret;
1042}
1043
Christoph Hellwigb2950862008-12-02 09:54:17 -05001044static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001045 struct btrfs_device *device,
1046 u64 start)
1047{
1048 int ret;
1049 struct btrfs_path *path;
1050 struct btrfs_root *root = device->dev_root;
1051 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001052 struct btrfs_key found_key;
1053 struct extent_buffer *leaf = NULL;
1054 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001055
1056 path = btrfs_alloc_path();
1057 if (!path)
1058 return -ENOMEM;
1059
1060 key.objectid = device->devid;
1061 key.offset = start;
1062 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001063again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001064 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001065 if (ret > 0) {
1066 ret = btrfs_previous_item(root, path, key.objectid,
1067 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001068 if (ret)
1069 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001070 leaf = path->nodes[0];
1071 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1072 extent = btrfs_item_ptr(leaf, path->slots[0],
1073 struct btrfs_dev_extent);
1074 BUG_ON(found_key.offset > start || found_key.offset +
1075 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001076 key = found_key;
1077 btrfs_release_path(path);
1078 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001079 } else if (ret == 0) {
1080 leaf = path->nodes[0];
1081 extent = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001083 } else {
1084 btrfs_error(root->fs_info, ret, "Slot search failed");
1085 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001086 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001087
Josef Bacik2bf64752011-09-26 17:12:22 -04001088 if (device->bytes_used > 0) {
1089 u64 len = btrfs_dev_extent_length(leaf, extent);
1090 device->bytes_used -= len;
1091 spin_lock(&root->fs_info->free_chunk_lock);
1092 root->fs_info->free_chunk_space += len;
1093 spin_unlock(&root->fs_info->free_chunk_lock);
1094 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001095 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001096 if (ret) {
1097 btrfs_error(root->fs_info, ret,
1098 "Failed to remove dev extent item");
1099 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001100out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001101 btrfs_free_path(path);
1102 return ret;
1103}
1104
Yan Zheng2b820322008-11-17 21:11:30 -05001105int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001106 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001107 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001108 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001109{
1110 int ret;
1111 struct btrfs_path *path;
1112 struct btrfs_root *root = device->dev_root;
1113 struct btrfs_dev_extent *extent;
1114 struct extent_buffer *leaf;
1115 struct btrfs_key key;
1116
Chris Masondfe25022008-05-13 13:46:40 -04001117 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001118 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001119 path = btrfs_alloc_path();
1120 if (!path)
1121 return -ENOMEM;
1122
Chris Mason0b86a832008-03-24 15:01:56 -04001123 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001124 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001125 key.type = BTRFS_DEV_EXTENT_KEY;
1126 ret = btrfs_insert_empty_item(trans, root, path, &key,
1127 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001128 if (ret)
1129 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001130
1131 leaf = path->nodes[0];
1132 extent = btrfs_item_ptr(leaf, path->slots[0],
1133 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001134 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1135 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1136 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1137
1138 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1139 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1140 BTRFS_UUID_SIZE);
1141
Chris Mason0b86a832008-03-24 15:01:56 -04001142 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1143 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001144out:
Chris Mason0b86a832008-03-24 15:01:56 -04001145 btrfs_free_path(path);
1146 return ret;
1147}
1148
Chris Masona1b32a52008-09-05 16:09:51 -04001149static noinline int find_next_chunk(struct btrfs_root *root,
1150 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001151{
1152 struct btrfs_path *path;
1153 int ret;
1154 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001155 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001156 struct btrfs_key found_key;
1157
1158 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001159 if (!path)
1160 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001161
Chris Masone17cade2008-04-15 15:41:47 -04001162 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001163 key.offset = (u64)-1;
1164 key.type = BTRFS_CHUNK_ITEM_KEY;
1165
1166 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1167 if (ret < 0)
1168 goto error;
1169
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001170 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001171
1172 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1173 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001174 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001175 } else {
1176 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1177 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001178 if (found_key.objectid != objectid)
1179 *offset = 0;
1180 else {
1181 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1182 struct btrfs_chunk);
1183 *offset = found_key.offset +
1184 btrfs_chunk_length(path->nodes[0], chunk);
1185 }
Chris Mason0b86a832008-03-24 15:01:56 -04001186 }
1187 ret = 0;
1188error:
1189 btrfs_free_path(path);
1190 return ret;
1191}
1192
Yan Zheng2b820322008-11-17 21:11:30 -05001193static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001194{
1195 int ret;
1196 struct btrfs_key key;
1197 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001198 struct btrfs_path *path;
1199
1200 root = root->fs_info->chunk_root;
1201
1202 path = btrfs_alloc_path();
1203 if (!path)
1204 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001205
1206 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1207 key.type = BTRFS_DEV_ITEM_KEY;
1208 key.offset = (u64)-1;
1209
1210 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1211 if (ret < 0)
1212 goto error;
1213
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001214 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001215
1216 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1217 BTRFS_DEV_ITEM_KEY);
1218 if (ret) {
1219 *objectid = 1;
1220 } else {
1221 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1222 path->slots[0]);
1223 *objectid = found_key.offset + 1;
1224 }
1225 ret = 0;
1226error:
Yan Zheng2b820322008-11-17 21:11:30 -05001227 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001228 return ret;
1229}
1230
1231/*
1232 * the device information is stored in the chunk root
1233 * the btrfs_device struct should be fully filled in
1234 */
1235int btrfs_add_device(struct btrfs_trans_handle *trans,
1236 struct btrfs_root *root,
1237 struct btrfs_device *device)
1238{
1239 int ret;
1240 struct btrfs_path *path;
1241 struct btrfs_dev_item *dev_item;
1242 struct extent_buffer *leaf;
1243 struct btrfs_key key;
1244 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001245
1246 root = root->fs_info->chunk_root;
1247
1248 path = btrfs_alloc_path();
1249 if (!path)
1250 return -ENOMEM;
1251
Chris Mason0b86a832008-03-24 15:01:56 -04001252 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1253 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001254 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001255
1256 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001257 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001258 if (ret)
1259 goto out;
1260
1261 leaf = path->nodes[0];
1262 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1263
1264 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001265 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001266 btrfs_set_device_type(leaf, dev_item, device->type);
1267 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1268 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1269 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001270 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1271 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001272 btrfs_set_device_group(leaf, dev_item, 0);
1273 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1274 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001275 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001276
Chris Mason0b86a832008-03-24 15:01:56 -04001277 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001278 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001279 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1280 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001281 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001282
Yan Zheng2b820322008-11-17 21:11:30 -05001283 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001284out:
1285 btrfs_free_path(path);
1286 return ret;
1287}
Chris Mason8f18cf12008-04-25 16:53:30 -04001288
Chris Masona061fc82008-05-07 11:43:44 -04001289static int btrfs_rm_dev_item(struct btrfs_root *root,
1290 struct btrfs_device *device)
1291{
1292 int ret;
1293 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001294 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001295 struct btrfs_trans_handle *trans;
1296
1297 root = root->fs_info->chunk_root;
1298
1299 path = btrfs_alloc_path();
1300 if (!path)
1301 return -ENOMEM;
1302
Yan, Zhenga22285a2010-05-16 10:48:46 -04001303 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001304 if (IS_ERR(trans)) {
1305 btrfs_free_path(path);
1306 return PTR_ERR(trans);
1307 }
Chris Masona061fc82008-05-07 11:43:44 -04001308 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1309 key.type = BTRFS_DEV_ITEM_KEY;
1310 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001311 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001312
1313 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1314 if (ret < 0)
1315 goto out;
1316
1317 if (ret > 0) {
1318 ret = -ENOENT;
1319 goto out;
1320 }
1321
1322 ret = btrfs_del_item(trans, root, path);
1323 if (ret)
1324 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001325out:
1326 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001327 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001328 btrfs_commit_transaction(trans, root);
1329 return ret;
1330}
1331
1332int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1333{
1334 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001335 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001336 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001337 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001338 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001339 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001340 u64 all_avail;
1341 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001342 u64 num_devices;
1343 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001344 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001345 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001346
Chris Masona061fc82008-05-07 11:43:44 -04001347 mutex_lock(&uuid_mutex);
1348
1349 all_avail = root->fs_info->avail_data_alloc_bits |
1350 root->fs_info->avail_system_alloc_bits |
1351 root->fs_info->avail_metadata_alloc_bits;
1352
1353 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001354 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001355 printk(KERN_ERR "btrfs: unable to go below four devices "
1356 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001357 ret = -EINVAL;
1358 goto out;
1359 }
1360
1361 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001362 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001363 printk(KERN_ERR "btrfs: unable to go below two "
1364 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001365 ret = -EINVAL;
1366 goto out;
1367 }
1368
Chris Masondfe25022008-05-13 13:46:40 -04001369 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001370 struct list_head *devices;
1371 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001372
Chris Masondfe25022008-05-13 13:46:40 -04001373 device = NULL;
1374 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001375 /*
1376 * It is safe to read the devices since the volume_mutex
1377 * is held.
1378 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001379 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001380 if (tmp->in_fs_metadata &&
1381 !tmp->is_tgtdev_for_dev_replace &&
1382 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001383 device = tmp;
1384 break;
1385 }
1386 }
1387 bdev = NULL;
1388 bh = NULL;
1389 disk_super = NULL;
1390 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001391 printk(KERN_ERR "btrfs: no missing devices found to "
1392 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001393 goto out;
1394 }
Chris Masondfe25022008-05-13 13:46:40 -04001395 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001396 ret = btrfs_get_bdev_and_sb(device_path,
1397 FMODE_READ | FMODE_EXCL,
1398 root->fs_info->bdev_holder, 0,
1399 &bdev, &bh);
1400 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001401 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001402 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001403 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001404 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001405 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001406 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001407 if (!device) {
1408 ret = -ENOENT;
1409 goto error_brelse;
1410 }
Chris Masondfe25022008-05-13 13:46:40 -04001411 }
Yan Zheng2b820322008-11-17 21:11:30 -05001412
Stefan Behrens63a212a2012-11-05 18:29:28 +01001413 if (device->is_tgtdev_for_dev_replace) {
1414 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1415 ret = -EINVAL;
1416 goto error_brelse;
1417 }
1418
Yan Zheng2b820322008-11-17 21:11:30 -05001419 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001420 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1421 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001422 ret = -EINVAL;
1423 goto error_brelse;
1424 }
1425
1426 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001427 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001428 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001429 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001430 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001431 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001432 }
Chris Masona061fc82008-05-07 11:43:44 -04001433
1434 ret = btrfs_shrink_device(device, 0);
1435 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001436 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001437
Stefan Behrens63a212a2012-11-05 18:29:28 +01001438 /*
1439 * TODO: the superblock still includes this device in its num_devices
1440 * counter although write_all_supers() is not locked out. This
1441 * could give a filesystem state which requires a degraded mount.
1442 */
Chris Masona061fc82008-05-07 11:43:44 -04001443 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1444 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001445 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001446
Josef Bacik2bf64752011-09-26 17:12:22 -04001447 spin_lock(&root->fs_info->free_chunk_lock);
1448 root->fs_info->free_chunk_space = device->total_bytes -
1449 device->bytes_used;
1450 spin_unlock(&root->fs_info->free_chunk_lock);
1451
Yan Zheng2b820322008-11-17 21:11:30 -05001452 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001453 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001454
1455 /*
1456 * the device list mutex makes sure that we don't change
1457 * the device list while someone else is writing out all
1458 * the device supers.
1459 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001460
1461 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001462 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001463 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001464
Yan Zhenge4404d62008-12-12 10:03:26 -05001465 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001466 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001467
Chris Masoncd02dca2010-12-13 14:56:23 -05001468 if (device->missing)
1469 root->fs_info->fs_devices->missing_devices--;
1470
Yan Zheng2b820322008-11-17 21:11:30 -05001471 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1472 struct btrfs_device, dev_list);
1473 if (device->bdev == root->fs_info->sb->s_bdev)
1474 root->fs_info->sb->s_bdev = next_device->bdev;
1475 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1476 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1477
Xiao Guangrong1f781602011-04-20 10:09:16 +00001478 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001479 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001480
1481 call_rcu(&device->rcu, free_device);
1482 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001483
David Sterba6c417612011-04-13 15:41:04 +02001484 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1485 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001486
Xiao Guangrong1f781602011-04-20 10:09:16 +00001487 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001488 struct btrfs_fs_devices *fs_devices;
1489 fs_devices = root->fs_info->fs_devices;
1490 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001491 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001492 break;
1493 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001494 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001495 fs_devices->seed = cur_devices->seed;
1496 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001497 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001498 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001499 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001500 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001501 }
1502
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001503 root->fs_info->num_tolerated_disk_barrier_failures =
1504 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1505
Yan Zheng2b820322008-11-17 21:11:30 -05001506 /*
1507 * at this point, the device is zero sized. We want to
1508 * remove it from the devices list and zero out the old super
1509 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001510 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001511 /* make sure this device isn't detected as part of
1512 * the FS anymore
1513 */
1514 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1515 set_buffer_dirty(bh);
1516 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001517 }
Chris Masona061fc82008-05-07 11:43:44 -04001518
Chris Masona061fc82008-05-07 11:43:44 -04001519 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001520
1521error_brelse:
1522 brelse(bh);
1523error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001524 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001525 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001526out:
1527 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001528 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001529error_undo:
1530 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001531 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001532 list_add(&device->dev_alloc_list,
1533 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001534 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001535 root->fs_info->fs_devices->rw_devices++;
1536 }
1537 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001538}
1539
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001540int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1541 struct btrfs_device **device)
1542{
1543 int ret = 0;
1544 struct btrfs_super_block *disk_super;
1545 u64 devid;
1546 u8 *dev_uuid;
1547 struct block_device *bdev;
1548 struct buffer_head *bh;
1549
1550 *device = NULL;
1551 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1552 root->fs_info->bdev_holder, 0, &bdev, &bh);
1553 if (ret)
1554 return ret;
1555 disk_super = (struct btrfs_super_block *)bh->b_data;
1556 devid = btrfs_stack_device_id(&disk_super->dev_item);
1557 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001558 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001559 disk_super->fsid);
1560 brelse(bh);
1561 if (!*device)
1562 ret = -ENOENT;
1563 blkdev_put(bdev, FMODE_READ);
1564 return ret;
1565}
1566
1567int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1568 char *device_path,
1569 struct btrfs_device **device)
1570{
1571 *device = NULL;
1572 if (strcmp(device_path, "missing") == 0) {
1573 struct list_head *devices;
1574 struct btrfs_device *tmp;
1575
1576 devices = &root->fs_info->fs_devices->devices;
1577 /*
1578 * It is safe to read the devices since the volume_mutex
1579 * is held by the caller.
1580 */
1581 list_for_each_entry(tmp, devices, dev_list) {
1582 if (tmp->in_fs_metadata && !tmp->bdev) {
1583 *device = tmp;
1584 break;
1585 }
1586 }
1587
1588 if (!*device) {
1589 pr_err("btrfs: no missing device found\n");
1590 return -ENOENT;
1591 }
1592
1593 return 0;
1594 } else {
1595 return btrfs_find_device_by_path(root, device_path, device);
1596 }
1597}
1598
Yan Zheng2b820322008-11-17 21:11:30 -05001599/*
1600 * does all the dirty work required for changing file system's UUID.
1601 */
Li Zefan125ccb02011-12-08 15:07:24 +08001602static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001603{
1604 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1605 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001606 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001607 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001608 struct btrfs_device *device;
1609 u64 super_flags;
1610
1611 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001612 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001613 return -EINVAL;
1614
Yan Zhenge4404d62008-12-12 10:03:26 -05001615 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1616 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001617 return -ENOMEM;
1618
Yan Zhenge4404d62008-12-12 10:03:26 -05001619 old_devices = clone_fs_devices(fs_devices);
1620 if (IS_ERR(old_devices)) {
1621 kfree(seed_devices);
1622 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001623 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001624
Yan Zheng2b820322008-11-17 21:11:30 -05001625 list_add(&old_devices->list, &fs_uuids);
1626
Yan Zhenge4404d62008-12-12 10:03:26 -05001627 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1628 seed_devices->opened = 1;
1629 INIT_LIST_HEAD(&seed_devices->devices);
1630 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001631 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001632
1633 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001634 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1635 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001636 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1637
Yan Zhenge4404d62008-12-12 10:03:26 -05001638 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1639 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1640 device->fs_devices = seed_devices;
1641 }
1642
Yan Zheng2b820322008-11-17 21:11:30 -05001643 fs_devices->seeding = 0;
1644 fs_devices->num_devices = 0;
1645 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001646 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001647 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001648
1649 generate_random_uuid(fs_devices->fsid);
1650 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1651 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1652 super_flags = btrfs_super_flags(disk_super) &
1653 ~BTRFS_SUPER_FLAG_SEEDING;
1654 btrfs_set_super_flags(disk_super, super_flags);
1655
1656 return 0;
1657}
1658
1659/*
1660 * strore the expected generation for seed devices in device items.
1661 */
1662static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1663 struct btrfs_root *root)
1664{
1665 struct btrfs_path *path;
1666 struct extent_buffer *leaf;
1667 struct btrfs_dev_item *dev_item;
1668 struct btrfs_device *device;
1669 struct btrfs_key key;
1670 u8 fs_uuid[BTRFS_UUID_SIZE];
1671 u8 dev_uuid[BTRFS_UUID_SIZE];
1672 u64 devid;
1673 int ret;
1674
1675 path = btrfs_alloc_path();
1676 if (!path)
1677 return -ENOMEM;
1678
1679 root = root->fs_info->chunk_root;
1680 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1681 key.offset = 0;
1682 key.type = BTRFS_DEV_ITEM_KEY;
1683
1684 while (1) {
1685 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1686 if (ret < 0)
1687 goto error;
1688
1689 leaf = path->nodes[0];
1690next_slot:
1691 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1692 ret = btrfs_next_leaf(root, path);
1693 if (ret > 0)
1694 break;
1695 if (ret < 0)
1696 goto error;
1697 leaf = path->nodes[0];
1698 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001699 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001700 continue;
1701 }
1702
1703 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1704 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1705 key.type != BTRFS_DEV_ITEM_KEY)
1706 break;
1707
1708 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1709 struct btrfs_dev_item);
1710 devid = btrfs_device_id(leaf, dev_item);
1711 read_extent_buffer(leaf, dev_uuid,
1712 (unsigned long)btrfs_device_uuid(dev_item),
1713 BTRFS_UUID_SIZE);
1714 read_extent_buffer(leaf, fs_uuid,
1715 (unsigned long)btrfs_device_fsid(dev_item),
1716 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001717 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1718 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001719 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001720
1721 if (device->fs_devices->seeding) {
1722 btrfs_set_device_generation(leaf, dev_item,
1723 device->generation);
1724 btrfs_mark_buffer_dirty(leaf);
1725 }
1726
1727 path->slots[0]++;
1728 goto next_slot;
1729 }
1730 ret = 0;
1731error:
1732 btrfs_free_path(path);
1733 return ret;
1734}
1735
Chris Mason788f20e2008-04-28 15:29:42 -04001736int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1737{
Josef Bacikd5e20032011-08-04 14:52:27 +00001738 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001739 struct btrfs_trans_handle *trans;
1740 struct btrfs_device *device;
1741 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001742 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001743 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001744 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001745 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001746 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001747 int ret = 0;
1748
Yan Zheng2b820322008-11-17 21:11:30 -05001749 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001750 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001751
Li Zefana5d16332011-12-07 20:08:40 -05001752 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001753 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001754 if (IS_ERR(bdev))
1755 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001756
Yan Zheng2b820322008-11-17 21:11:30 -05001757 if (root->fs_info->fs_devices->seeding) {
1758 seeding_dev = 1;
1759 down_write(&sb->s_umount);
1760 mutex_lock(&uuid_mutex);
1761 }
1762
Chris Mason8c8bee12008-09-29 11:19:10 -04001763 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001764
Chris Mason788f20e2008-04-28 15:29:42 -04001765 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001766
1767 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001768 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001769 if (device->bdev == bdev) {
1770 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001771 mutex_unlock(
1772 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001773 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001774 }
1775 }
Liu Bod25628b2012-11-14 14:35:30 +00001776 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001777
1778 device = kzalloc(sizeof(*device), GFP_NOFS);
1779 if (!device) {
1780 /* we can safely leave the fs_devices entry around */
1781 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001782 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001783 }
1784
Josef Bacik606686e2012-06-04 14:03:51 -04001785 name = rcu_string_strdup(device_path, GFP_NOFS);
1786 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001787 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001788 ret = -ENOMEM;
1789 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001790 }
Josef Bacik606686e2012-06-04 14:03:51 -04001791 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001792
1793 ret = find_next_devid(root, &device->devid);
1794 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001795 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001796 kfree(device);
1797 goto error;
1798 }
1799
Yan, Zhenga22285a2010-05-16 10:48:46 -04001800 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001801 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001802 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001803 kfree(device);
1804 ret = PTR_ERR(trans);
1805 goto error;
1806 }
1807
Yan Zheng2b820322008-11-17 21:11:30 -05001808 lock_chunks(root);
1809
Josef Bacikd5e20032011-08-04 14:52:27 +00001810 q = bdev_get_queue(bdev);
1811 if (blk_queue_discard(q))
1812 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001813 device->writeable = 1;
1814 device->work.func = pending_bios_fn;
1815 generate_random_uuid(device->uuid);
1816 spin_lock_init(&device->io_lock);
1817 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001818 device->io_width = root->sectorsize;
1819 device->io_align = root->sectorsize;
1820 device->sector_size = root->sectorsize;
1821 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001822 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001823 device->dev_root = root->fs_info->dev_root;
1824 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001825 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001826 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001827 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001828 set_blocksize(device->bdev, 4096);
1829
Yan Zheng2b820322008-11-17 21:11:30 -05001830 if (seeding_dev) {
1831 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001832 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001833 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001834 }
1835
1836 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001837
Chris Masone5e9a522009-06-10 15:17:02 -04001838 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001839 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001840 list_add(&device->dev_alloc_list,
1841 &root->fs_info->fs_devices->alloc_list);
1842 root->fs_info->fs_devices->num_devices++;
1843 root->fs_info->fs_devices->open_devices++;
1844 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001845 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001846 if (device->can_discard)
1847 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001848 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1849
Josef Bacik2bf64752011-09-26 17:12:22 -04001850 spin_lock(&root->fs_info->free_chunk_lock);
1851 root->fs_info->free_chunk_space += device->total_bytes;
1852 spin_unlock(&root->fs_info->free_chunk_lock);
1853
Chris Masonc2898112009-06-10 09:51:32 -04001854 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1855 root->fs_info->fs_devices->rotating = 1;
1856
David Sterba6c417612011-04-13 15:41:04 +02001857 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1858 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001859 total_bytes + device->total_bytes);
1860
David Sterba6c417612011-04-13 15:41:04 +02001861 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1862 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001863 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001864 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001865
Yan Zheng2b820322008-11-17 21:11:30 -05001866 if (seeding_dev) {
1867 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001868 if (ret) {
1869 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001870 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001871 }
Yan Zheng2b820322008-11-17 21:11:30 -05001872 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06001873 if (ret) {
1874 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001875 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001876 }
Yan Zheng2b820322008-11-17 21:11:30 -05001877 } else {
1878 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001879 if (ret) {
1880 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001881 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001882 }
Yan Zheng2b820322008-11-17 21:11:30 -05001883 }
1884
Chris Mason913d9522009-03-10 13:17:18 -04001885 /*
1886 * we've got more storage, clear any full flags on the space
1887 * infos
1888 */
1889 btrfs_clear_space_info_full(root->fs_info);
1890
Chris Mason7d9eb122008-07-08 14:19:17 -04001891 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001892 root->fs_info->num_tolerated_disk_barrier_failures =
1893 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001895
1896 if (seeding_dev) {
1897 mutex_unlock(&uuid_mutex);
1898 up_write(&sb->s_umount);
1899
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001900 if (ret) /* transaction commit */
1901 return ret;
1902
Yan Zheng2b820322008-11-17 21:11:30 -05001903 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001904 if (ret < 0)
1905 btrfs_error(root->fs_info, ret,
1906 "Failed to relocate sys chunks after "
1907 "device initialization. This can be fixed "
1908 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00001909 trans = btrfs_attach_transaction(root);
1910 if (IS_ERR(trans)) {
1911 if (PTR_ERR(trans) == -ENOENT)
1912 return 0;
1913 return PTR_ERR(trans);
1914 }
1915 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001916 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001917
Chris Mason788f20e2008-04-28 15:29:42 -04001918 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001919
1920error_trans:
1921 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001923 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001925error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001926 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001927 if (seeding_dev) {
1928 mutex_unlock(&uuid_mutex);
1929 up_write(&sb->s_umount);
1930 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001931 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001932}
1933
Chris Masond3977122009-01-05 21:25:51 -05001934static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1935 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001936{
1937 int ret;
1938 struct btrfs_path *path;
1939 struct btrfs_root *root;
1940 struct btrfs_dev_item *dev_item;
1941 struct extent_buffer *leaf;
1942 struct btrfs_key key;
1943
1944 root = device->dev_root->fs_info->chunk_root;
1945
1946 path = btrfs_alloc_path();
1947 if (!path)
1948 return -ENOMEM;
1949
1950 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1951 key.type = BTRFS_DEV_ITEM_KEY;
1952 key.offset = device->devid;
1953
1954 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1955 if (ret < 0)
1956 goto out;
1957
1958 if (ret > 0) {
1959 ret = -ENOENT;
1960 goto out;
1961 }
1962
1963 leaf = path->nodes[0];
1964 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1965
1966 btrfs_set_device_id(leaf, dev_item, device->devid);
1967 btrfs_set_device_type(leaf, dev_item, device->type);
1968 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1969 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1970 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001971 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001972 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1973 btrfs_mark_buffer_dirty(leaf);
1974
1975out:
1976 btrfs_free_path(path);
1977 return ret;
1978}
1979
Chris Mason7d9eb122008-07-08 14:19:17 -04001980static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001981 struct btrfs_device *device, u64 new_size)
1982{
1983 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001984 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001985 u64 old_total = btrfs_super_total_bytes(super_copy);
1986 u64 diff = new_size - device->total_bytes;
1987
Yan Zheng2b820322008-11-17 21:11:30 -05001988 if (!device->writeable)
1989 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001990 if (new_size <= device->total_bytes ||
1991 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05001992 return -EINVAL;
1993
Chris Mason8f18cf12008-04-25 16:53:30 -04001994 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001995 device->fs_devices->total_rw_bytes += diff;
1996
1997 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001998 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001999 btrfs_clear_space_info_full(device->dev_root->fs_info);
2000
Chris Mason8f18cf12008-04-25 16:53:30 -04002001 return btrfs_update_device(trans, device);
2002}
2003
Chris Mason7d9eb122008-07-08 14:19:17 -04002004int btrfs_grow_device(struct btrfs_trans_handle *trans,
2005 struct btrfs_device *device, u64 new_size)
2006{
2007 int ret;
2008 lock_chunks(device->dev_root);
2009 ret = __btrfs_grow_device(trans, device, new_size);
2010 unlock_chunks(device->dev_root);
2011 return ret;
2012}
2013
Chris Mason8f18cf12008-04-25 16:53:30 -04002014static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2015 struct btrfs_root *root,
2016 u64 chunk_tree, u64 chunk_objectid,
2017 u64 chunk_offset)
2018{
2019 int ret;
2020 struct btrfs_path *path;
2021 struct btrfs_key key;
2022
2023 root = root->fs_info->chunk_root;
2024 path = btrfs_alloc_path();
2025 if (!path)
2026 return -ENOMEM;
2027
2028 key.objectid = chunk_objectid;
2029 key.offset = chunk_offset;
2030 key.type = BTRFS_CHUNK_ITEM_KEY;
2031
2032 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002033 if (ret < 0)
2034 goto out;
2035 else if (ret > 0) { /* Logic error or corruption */
2036 btrfs_error(root->fs_info, -ENOENT,
2037 "Failed lookup while freeing chunk.");
2038 ret = -ENOENT;
2039 goto out;
2040 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002041
2042 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002043 if (ret < 0)
2044 btrfs_error(root->fs_info, ret,
2045 "Failed to delete chunk item.");
2046out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002047 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002048 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002049}
2050
Christoph Hellwigb2950862008-12-02 09:54:17 -05002051static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002052 chunk_offset)
2053{
David Sterba6c417612011-04-13 15:41:04 +02002054 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002055 struct btrfs_disk_key *disk_key;
2056 struct btrfs_chunk *chunk;
2057 u8 *ptr;
2058 int ret = 0;
2059 u32 num_stripes;
2060 u32 array_size;
2061 u32 len = 0;
2062 u32 cur;
2063 struct btrfs_key key;
2064
2065 array_size = btrfs_super_sys_array_size(super_copy);
2066
2067 ptr = super_copy->sys_chunk_array;
2068 cur = 0;
2069
2070 while (cur < array_size) {
2071 disk_key = (struct btrfs_disk_key *)ptr;
2072 btrfs_disk_key_to_cpu(&key, disk_key);
2073
2074 len = sizeof(*disk_key);
2075
2076 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2077 chunk = (struct btrfs_chunk *)(ptr + len);
2078 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2079 len += btrfs_chunk_item_size(num_stripes);
2080 } else {
2081 ret = -EIO;
2082 break;
2083 }
2084 if (key.objectid == chunk_objectid &&
2085 key.offset == chunk_offset) {
2086 memmove(ptr, ptr + len, array_size - (cur + len));
2087 array_size -= len;
2088 btrfs_set_super_sys_array_size(super_copy, array_size);
2089 } else {
2090 ptr += len;
2091 cur += len;
2092 }
2093 }
2094 return ret;
2095}
2096
Christoph Hellwigb2950862008-12-02 09:54:17 -05002097static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002098 u64 chunk_tree, u64 chunk_objectid,
2099 u64 chunk_offset)
2100{
2101 struct extent_map_tree *em_tree;
2102 struct btrfs_root *extent_root;
2103 struct btrfs_trans_handle *trans;
2104 struct extent_map *em;
2105 struct map_lookup *map;
2106 int ret;
2107 int i;
2108
2109 root = root->fs_info->chunk_root;
2110 extent_root = root->fs_info->extent_root;
2111 em_tree = &root->fs_info->mapping_tree.map_tree;
2112
Josef Bacikba1bf482009-09-11 16:11:19 -04002113 ret = btrfs_can_relocate(extent_root, chunk_offset);
2114 if (ret)
2115 return -ENOSPC;
2116
Chris Mason8f18cf12008-04-25 16:53:30 -04002117 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002118 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002119 if (ret)
2120 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002121
Yan, Zhenga22285a2010-05-16 10:48:46 -04002122 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002123 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002124
Chris Mason7d9eb122008-07-08 14:19:17 -04002125 lock_chunks(root);
2126
Chris Mason8f18cf12008-04-25 16:53:30 -04002127 /*
2128 * step two, delete the device extents and the
2129 * chunk tree entries
2130 */
Chris Mason890871b2009-09-02 16:24:52 -04002131 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002132 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002133 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002134
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002135 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002136 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002137 map = (struct map_lookup *)em->bdev;
2138
2139 for (i = 0; i < map->num_stripes; i++) {
2140 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2141 map->stripes[i].physical);
2142 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002143
Chris Masondfe25022008-05-13 13:46:40 -04002144 if (map->stripes[i].dev) {
2145 ret = btrfs_update_device(trans, map->stripes[i].dev);
2146 BUG_ON(ret);
2147 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002148 }
2149 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2150 chunk_offset);
2151
2152 BUG_ON(ret);
2153
liubo1abe9b82011-03-24 11:18:59 +00002154 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2155
Chris Mason8f18cf12008-04-25 16:53:30 -04002156 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2157 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2158 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002159 }
2160
Zheng Yan1a40e232008-09-26 10:09:34 -04002161 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2162 BUG_ON(ret);
2163
Chris Mason890871b2009-09-02 16:24:52 -04002164 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002165 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002166 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002167
Chris Mason8f18cf12008-04-25 16:53:30 -04002168 kfree(map);
2169 em->bdev = NULL;
2170
2171 /* once for the tree */
2172 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002173 /* once for us */
2174 free_extent_map(em);
2175
Chris Mason7d9eb122008-07-08 14:19:17 -04002176 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002177 btrfs_end_transaction(trans, root);
2178 return 0;
2179}
2180
Yan Zheng2b820322008-11-17 21:11:30 -05002181static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2182{
2183 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2184 struct btrfs_path *path;
2185 struct extent_buffer *leaf;
2186 struct btrfs_chunk *chunk;
2187 struct btrfs_key key;
2188 struct btrfs_key found_key;
2189 u64 chunk_tree = chunk_root->root_key.objectid;
2190 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002191 bool retried = false;
2192 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002193 int ret;
2194
2195 path = btrfs_alloc_path();
2196 if (!path)
2197 return -ENOMEM;
2198
Josef Bacikba1bf482009-09-11 16:11:19 -04002199again:
Yan Zheng2b820322008-11-17 21:11:30 -05002200 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2201 key.offset = (u64)-1;
2202 key.type = BTRFS_CHUNK_ITEM_KEY;
2203
2204 while (1) {
2205 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2206 if (ret < 0)
2207 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002208 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002209
2210 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2211 key.type);
2212 if (ret < 0)
2213 goto error;
2214 if (ret > 0)
2215 break;
2216
2217 leaf = path->nodes[0];
2218 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2219
2220 chunk = btrfs_item_ptr(leaf, path->slots[0],
2221 struct btrfs_chunk);
2222 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002223 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002224
2225 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2226 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2227 found_key.objectid,
2228 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002229 if (ret == -ENOSPC)
2230 failed++;
2231 else if (ret)
2232 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002233 }
2234
2235 if (found_key.offset == 0)
2236 break;
2237 key.offset = found_key.offset - 1;
2238 }
2239 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002240 if (failed && !retried) {
2241 failed = 0;
2242 retried = true;
2243 goto again;
2244 } else if (failed && retried) {
2245 WARN_ON(1);
2246 ret = -ENOSPC;
2247 }
Yan Zheng2b820322008-11-17 21:11:30 -05002248error:
2249 btrfs_free_path(path);
2250 return ret;
2251}
2252
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002253static int insert_balance_item(struct btrfs_root *root,
2254 struct btrfs_balance_control *bctl)
2255{
2256 struct btrfs_trans_handle *trans;
2257 struct btrfs_balance_item *item;
2258 struct btrfs_disk_balance_args disk_bargs;
2259 struct btrfs_path *path;
2260 struct extent_buffer *leaf;
2261 struct btrfs_key key;
2262 int ret, err;
2263
2264 path = btrfs_alloc_path();
2265 if (!path)
2266 return -ENOMEM;
2267
2268 trans = btrfs_start_transaction(root, 0);
2269 if (IS_ERR(trans)) {
2270 btrfs_free_path(path);
2271 return PTR_ERR(trans);
2272 }
2273
2274 key.objectid = BTRFS_BALANCE_OBJECTID;
2275 key.type = BTRFS_BALANCE_ITEM_KEY;
2276 key.offset = 0;
2277
2278 ret = btrfs_insert_empty_item(trans, root, path, &key,
2279 sizeof(*item));
2280 if (ret)
2281 goto out;
2282
2283 leaf = path->nodes[0];
2284 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2285
2286 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2287
2288 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2289 btrfs_set_balance_data(leaf, item, &disk_bargs);
2290 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2291 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2292 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2293 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2294
2295 btrfs_set_balance_flags(leaf, item, bctl->flags);
2296
2297 btrfs_mark_buffer_dirty(leaf);
2298out:
2299 btrfs_free_path(path);
2300 err = btrfs_commit_transaction(trans, root);
2301 if (err && !ret)
2302 ret = err;
2303 return ret;
2304}
2305
2306static int del_balance_item(struct btrfs_root *root)
2307{
2308 struct btrfs_trans_handle *trans;
2309 struct btrfs_path *path;
2310 struct btrfs_key key;
2311 int ret, err;
2312
2313 path = btrfs_alloc_path();
2314 if (!path)
2315 return -ENOMEM;
2316
2317 trans = btrfs_start_transaction(root, 0);
2318 if (IS_ERR(trans)) {
2319 btrfs_free_path(path);
2320 return PTR_ERR(trans);
2321 }
2322
2323 key.objectid = BTRFS_BALANCE_OBJECTID;
2324 key.type = BTRFS_BALANCE_ITEM_KEY;
2325 key.offset = 0;
2326
2327 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2328 if (ret < 0)
2329 goto out;
2330 if (ret > 0) {
2331 ret = -ENOENT;
2332 goto out;
2333 }
2334
2335 ret = btrfs_del_item(trans, root, path);
2336out:
2337 btrfs_free_path(path);
2338 err = btrfs_commit_transaction(trans, root);
2339 if (err && !ret)
2340 ret = err;
2341 return ret;
2342}
2343
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002344/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002345 * This is a heuristic used to reduce the number of chunks balanced on
2346 * resume after balance was interrupted.
2347 */
2348static void update_balance_args(struct btrfs_balance_control *bctl)
2349{
2350 /*
2351 * Turn on soft mode for chunk types that were being converted.
2352 */
2353 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2354 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2355 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2356 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2357 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2358 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2359
2360 /*
2361 * Turn on usage filter if is not already used. The idea is
2362 * that chunks that we have already balanced should be
2363 * reasonably full. Don't do it for chunks that are being
2364 * converted - that will keep us from relocating unconverted
2365 * (albeit full) chunks.
2366 */
2367 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2368 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2369 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2370 bctl->data.usage = 90;
2371 }
2372 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2373 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2374 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2375 bctl->sys.usage = 90;
2376 }
2377 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2378 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2379 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2380 bctl->meta.usage = 90;
2381 }
2382}
2383
2384/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002385 * Should be called with both balance and volume mutexes held to
2386 * serialize other volume operations (add_dev/rm_dev/resize) with
2387 * restriper. Same goes for unset_balance_control.
2388 */
2389static void set_balance_control(struct btrfs_balance_control *bctl)
2390{
2391 struct btrfs_fs_info *fs_info = bctl->fs_info;
2392
2393 BUG_ON(fs_info->balance_ctl);
2394
2395 spin_lock(&fs_info->balance_lock);
2396 fs_info->balance_ctl = bctl;
2397 spin_unlock(&fs_info->balance_lock);
2398}
2399
2400static void unset_balance_control(struct btrfs_fs_info *fs_info)
2401{
2402 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2403
2404 BUG_ON(!fs_info->balance_ctl);
2405
2406 spin_lock(&fs_info->balance_lock);
2407 fs_info->balance_ctl = NULL;
2408 spin_unlock(&fs_info->balance_lock);
2409
2410 kfree(bctl);
2411}
2412
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002413/*
2414 * Balance filters. Return 1 if chunk should be filtered out
2415 * (should not be balanced).
2416 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002417static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002418 struct btrfs_balance_args *bargs)
2419{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002420 chunk_type = chunk_to_extended(chunk_type) &
2421 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002422
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002423 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002424 return 0;
2425
2426 return 1;
2427}
2428
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002429static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2430 struct btrfs_balance_args *bargs)
2431{
2432 struct btrfs_block_group_cache *cache;
2433 u64 chunk_used, user_thresh;
2434 int ret = 1;
2435
2436 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2437 chunk_used = btrfs_block_group_used(&cache->item);
2438
2439 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2440 if (chunk_used < user_thresh)
2441 ret = 0;
2442
2443 btrfs_put_block_group(cache);
2444 return ret;
2445}
2446
Ilya Dryomov409d4042012-01-16 22:04:47 +02002447static int chunk_devid_filter(struct extent_buffer *leaf,
2448 struct btrfs_chunk *chunk,
2449 struct btrfs_balance_args *bargs)
2450{
2451 struct btrfs_stripe *stripe;
2452 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2453 int i;
2454
2455 for (i = 0; i < num_stripes; i++) {
2456 stripe = btrfs_stripe_nr(chunk, i);
2457 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2458 return 0;
2459 }
2460
2461 return 1;
2462}
2463
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002464/* [pstart, pend) */
2465static int chunk_drange_filter(struct extent_buffer *leaf,
2466 struct btrfs_chunk *chunk,
2467 u64 chunk_offset,
2468 struct btrfs_balance_args *bargs)
2469{
2470 struct btrfs_stripe *stripe;
2471 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2472 u64 stripe_offset;
2473 u64 stripe_length;
2474 int factor;
2475 int i;
2476
2477 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2478 return 0;
2479
2480 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2481 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2482 factor = 2;
2483 else
2484 factor = 1;
2485 factor = num_stripes / factor;
2486
2487 for (i = 0; i < num_stripes; i++) {
2488 stripe = btrfs_stripe_nr(chunk, i);
2489 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2490 continue;
2491
2492 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2493 stripe_length = btrfs_chunk_length(leaf, chunk);
2494 do_div(stripe_length, factor);
2495
2496 if (stripe_offset < bargs->pend &&
2497 stripe_offset + stripe_length > bargs->pstart)
2498 return 0;
2499 }
2500
2501 return 1;
2502}
2503
Ilya Dryomovea671762012-01-16 22:04:48 +02002504/* [vstart, vend) */
2505static int chunk_vrange_filter(struct extent_buffer *leaf,
2506 struct btrfs_chunk *chunk,
2507 u64 chunk_offset,
2508 struct btrfs_balance_args *bargs)
2509{
2510 if (chunk_offset < bargs->vend &&
2511 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2512 /* at least part of the chunk is inside this vrange */
2513 return 0;
2514
2515 return 1;
2516}
2517
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002518static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002519 struct btrfs_balance_args *bargs)
2520{
2521 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2522 return 0;
2523
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002524 chunk_type = chunk_to_extended(chunk_type) &
2525 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002526
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002527 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002528 return 1;
2529
2530 return 0;
2531}
2532
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002533static int should_balance_chunk(struct btrfs_root *root,
2534 struct extent_buffer *leaf,
2535 struct btrfs_chunk *chunk, u64 chunk_offset)
2536{
2537 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2538 struct btrfs_balance_args *bargs = NULL;
2539 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2540
2541 /* type filter */
2542 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2543 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2544 return 0;
2545 }
2546
2547 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2548 bargs = &bctl->data;
2549 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2550 bargs = &bctl->sys;
2551 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2552 bargs = &bctl->meta;
2553
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002554 /* profiles filter */
2555 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2556 chunk_profiles_filter(chunk_type, bargs)) {
2557 return 0;
2558 }
2559
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002560 /* usage filter */
2561 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2562 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2563 return 0;
2564 }
2565
Ilya Dryomov409d4042012-01-16 22:04:47 +02002566 /* devid filter */
2567 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2568 chunk_devid_filter(leaf, chunk, bargs)) {
2569 return 0;
2570 }
2571
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002572 /* drange filter, makes sense only with devid filter */
2573 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2574 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2575 return 0;
2576 }
2577
Ilya Dryomovea671762012-01-16 22:04:48 +02002578 /* vrange filter */
2579 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2580 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2581 return 0;
2582 }
2583
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002584 /* soft profile changing mode */
2585 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2586 chunk_soft_convert_filter(chunk_type, bargs)) {
2587 return 0;
2588 }
2589
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002590 return 1;
2591}
2592
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002593static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002594{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002595 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002596 struct btrfs_root *chunk_root = fs_info->chunk_root;
2597 struct btrfs_root *dev_root = fs_info->dev_root;
2598 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002599 struct btrfs_device *device;
2600 u64 old_size;
2601 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002602 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002603 struct btrfs_path *path;
2604 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002605 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002606 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002607 struct extent_buffer *leaf;
2608 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002609 int ret;
2610 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002611 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002612
Chris Masonec44a352008-04-28 15:29:52 -04002613 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002614 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002615 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002616 old_size = device->total_bytes;
2617 size_to_free = div_factor(old_size, 1);
2618 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002619 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002620 device->total_bytes - device->bytes_used > size_to_free ||
2621 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002622 continue;
2623
2624 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002625 if (ret == -ENOSPC)
2626 break;
Chris Masonec44a352008-04-28 15:29:52 -04002627 BUG_ON(ret);
2628
Yan, Zhenga22285a2010-05-16 10:48:46 -04002629 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002630 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002631
2632 ret = btrfs_grow_device(trans, device, old_size);
2633 BUG_ON(ret);
2634
2635 btrfs_end_transaction(trans, dev_root);
2636 }
2637
2638 /* step two, relocate all the chunks */
2639 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002640 if (!path) {
2641 ret = -ENOMEM;
2642 goto error;
2643 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002644
2645 /* zero out stat counters */
2646 spin_lock(&fs_info->balance_lock);
2647 memset(&bctl->stat, 0, sizeof(bctl->stat));
2648 spin_unlock(&fs_info->balance_lock);
2649again:
Chris Masonec44a352008-04-28 15:29:52 -04002650 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2651 key.offset = (u64)-1;
2652 key.type = BTRFS_CHUNK_ITEM_KEY;
2653
Chris Masond3977122009-01-05 21:25:51 -05002654 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002655 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002656 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002657 ret = -ECANCELED;
2658 goto error;
2659 }
2660
Chris Masonec44a352008-04-28 15:29:52 -04002661 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2662 if (ret < 0)
2663 goto error;
2664
2665 /*
2666 * this shouldn't happen, it means the last relocate
2667 * failed
2668 */
2669 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002670 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002671
2672 ret = btrfs_previous_item(chunk_root, path, 0,
2673 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002674 if (ret) {
2675 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002676 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002677 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002678
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002679 leaf = path->nodes[0];
2680 slot = path->slots[0];
2681 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2682
Chris Masonec44a352008-04-28 15:29:52 -04002683 if (found_key.objectid != key.objectid)
2684 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002685
Chris Masonec44a352008-04-28 15:29:52 -04002686 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002687 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002688 break;
2689
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002690 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2691
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002692 if (!counting) {
2693 spin_lock(&fs_info->balance_lock);
2694 bctl->stat.considered++;
2695 spin_unlock(&fs_info->balance_lock);
2696 }
2697
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002698 ret = should_balance_chunk(chunk_root, leaf, chunk,
2699 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002700 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002701 if (!ret)
2702 goto loop;
2703
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002704 if (counting) {
2705 spin_lock(&fs_info->balance_lock);
2706 bctl->stat.expected++;
2707 spin_unlock(&fs_info->balance_lock);
2708 goto loop;
2709 }
2710
Chris Masonec44a352008-04-28 15:29:52 -04002711 ret = btrfs_relocate_chunk(chunk_root,
2712 chunk_root->root_key.objectid,
2713 found_key.objectid,
2714 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002715 if (ret && ret != -ENOSPC)
2716 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002717 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002718 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002719 } else {
2720 spin_lock(&fs_info->balance_lock);
2721 bctl->stat.completed++;
2722 spin_unlock(&fs_info->balance_lock);
2723 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002724loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002725 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002726 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002727
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002728 if (counting) {
2729 btrfs_release_path(path);
2730 counting = false;
2731 goto again;
2732 }
Chris Masonec44a352008-04-28 15:29:52 -04002733error:
2734 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002735 if (enospc_errors) {
2736 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2737 enospc_errors);
2738 if (!ret)
2739 ret = -ENOSPC;
2740 }
2741
Chris Masonec44a352008-04-28 15:29:52 -04002742 return ret;
2743}
2744
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002745/**
2746 * alloc_profile_is_valid - see if a given profile is valid and reduced
2747 * @flags: profile to validate
2748 * @extended: if true @flags is treated as an extended profile
2749 */
2750static int alloc_profile_is_valid(u64 flags, int extended)
2751{
2752 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2753 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2754
2755 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2756
2757 /* 1) check that all other bits are zeroed */
2758 if (flags & ~mask)
2759 return 0;
2760
2761 /* 2) see if profile is reduced */
2762 if (flags == 0)
2763 return !extended; /* "0" is valid for usual profiles */
2764
2765 /* true if exactly one bit set */
2766 return (flags & (flags - 1)) == 0;
2767}
2768
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002769static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2770{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002771 /* cancel requested || normal exit path */
2772 return atomic_read(&fs_info->balance_cancel_req) ||
2773 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2774 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002775}
2776
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002777static void __cancel_balance(struct btrfs_fs_info *fs_info)
2778{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002779 int ret;
2780
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002781 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002782 ret = del_balance_item(fs_info->tree_root);
2783 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002784}
2785
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002786void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002787 struct btrfs_ioctl_balance_args *bargs);
2788
2789/*
2790 * Should be called with both balance and volume mutexes held
2791 */
2792int btrfs_balance(struct btrfs_balance_control *bctl,
2793 struct btrfs_ioctl_balance_args *bargs)
2794{
2795 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002796 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002797 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002798 int ret;
2799
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002800 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002801 atomic_read(&fs_info->balance_pause_req) ||
2802 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002803 ret = -EINVAL;
2804 goto out;
2805 }
2806
Ilya Dryomove4837f82012-03-27 17:09:17 +03002807 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2808 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2809 mixed = 1;
2810
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002811 /*
2812 * In case of mixed groups both data and meta should be picked,
2813 * and identical options should be given for both of them.
2814 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002815 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2816 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002817 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2818 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2819 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2820 printk(KERN_ERR "btrfs: with mixed groups data and "
2821 "metadata balance options must be the same\n");
2822 ret = -EINVAL;
2823 goto out;
2824 }
2825 }
2826
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002827 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2828 if (fs_info->fs_devices->num_devices == 1)
2829 allowed |= BTRFS_BLOCK_GROUP_DUP;
2830 else if (fs_info->fs_devices->num_devices < 4)
2831 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2832 else
2833 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2834 BTRFS_BLOCK_GROUP_RAID10);
2835
Ilya Dryomov6728b192012-03-27 17:09:17 +03002836 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2837 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2838 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002839 printk(KERN_ERR "btrfs: unable to start balance with target "
2840 "data profile %llu\n",
2841 (unsigned long long)bctl->data.target);
2842 ret = -EINVAL;
2843 goto out;
2844 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002845 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2846 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2847 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002848 printk(KERN_ERR "btrfs: unable to start balance with target "
2849 "metadata profile %llu\n",
2850 (unsigned long long)bctl->meta.target);
2851 ret = -EINVAL;
2852 goto out;
2853 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002854 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2855 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2856 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002857 printk(KERN_ERR "btrfs: unable to start balance with target "
2858 "system profile %llu\n",
2859 (unsigned long long)bctl->sys.target);
2860 ret = -EINVAL;
2861 goto out;
2862 }
2863
Ilya Dryomove4837f82012-03-27 17:09:17 +03002864 /* allow dup'ed data chunks only in mixed mode */
2865 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002866 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002867 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2868 ret = -EINVAL;
2869 goto out;
2870 }
2871
2872 /* allow to reduce meta or sys integrity only if force set */
2873 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2874 BTRFS_BLOCK_GROUP_RAID10;
2875 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2876 (fs_info->avail_system_alloc_bits & allowed) &&
2877 !(bctl->sys.target & allowed)) ||
2878 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2879 (fs_info->avail_metadata_alloc_bits & allowed) &&
2880 !(bctl->meta.target & allowed))) {
2881 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2882 printk(KERN_INFO "btrfs: force reducing metadata "
2883 "integrity\n");
2884 } else {
2885 printk(KERN_ERR "btrfs: balance will reduce metadata "
2886 "integrity, use force if you want this\n");
2887 ret = -EINVAL;
2888 goto out;
2889 }
2890 }
2891
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002892 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
2893 int num_tolerated_disk_barrier_failures;
2894 u64 target = bctl->sys.target;
2895
2896 num_tolerated_disk_barrier_failures =
2897 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2898 if (num_tolerated_disk_barrier_failures > 0 &&
2899 (target &
2900 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
2901 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
2902 num_tolerated_disk_barrier_failures = 0;
2903 else if (num_tolerated_disk_barrier_failures > 1 &&
2904 (target &
2905 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
2906 num_tolerated_disk_barrier_failures = 1;
2907
2908 fs_info->num_tolerated_disk_barrier_failures =
2909 num_tolerated_disk_barrier_failures;
2910 }
2911
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002912 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002913 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002914 goto out;
2915
Ilya Dryomov59641012012-01-16 22:04:48 +02002916 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2917 BUG_ON(ret == -EEXIST);
2918 set_balance_control(bctl);
2919 } else {
2920 BUG_ON(ret != -EEXIST);
2921 spin_lock(&fs_info->balance_lock);
2922 update_balance_args(bctl);
2923 spin_unlock(&fs_info->balance_lock);
2924 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002925
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002926 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002927 mutex_unlock(&fs_info->balance_mutex);
2928
2929 ret = __btrfs_balance(fs_info);
2930
2931 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002932 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002933
2934 if (bargs) {
2935 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002936 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002937 }
2938
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002939 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2940 balance_need_close(fs_info)) {
2941 __cancel_balance(fs_info);
2942 }
2943
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002944 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
2945 fs_info->num_tolerated_disk_barrier_failures =
2946 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2947 }
2948
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002949 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002950
2951 return ret;
2952out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002953 if (bctl->flags & BTRFS_BALANCE_RESUME)
2954 __cancel_balance(fs_info);
2955 else
2956 kfree(bctl);
2957 return ret;
2958}
2959
2960static int balance_kthread(void *data)
2961{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002962 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002963 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002964
2965 mutex_lock(&fs_info->volume_mutex);
2966 mutex_lock(&fs_info->balance_mutex);
2967
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002968 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002969 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002970 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002971 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002972
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002973 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov59641012012-01-16 22:04:48 +02002974 mutex_unlock(&fs_info->balance_mutex);
2975 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002976
Ilya Dryomov59641012012-01-16 22:04:48 +02002977 return ret;
2978}
2979
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002980int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
2981{
2982 struct task_struct *tsk;
2983
2984 spin_lock(&fs_info->balance_lock);
2985 if (!fs_info->balance_ctl) {
2986 spin_unlock(&fs_info->balance_lock);
2987 return 0;
2988 }
2989 spin_unlock(&fs_info->balance_lock);
2990
2991 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2992 printk(KERN_INFO "btrfs: force skipping balance\n");
2993 return 0;
2994 }
2995
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002996 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002997 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
2998 if (IS_ERR(tsk))
2999 return PTR_ERR(tsk);
3000
3001 return 0;
3002}
3003
Ilya Dryomov68310a52012-06-22 12:24:12 -06003004int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003005{
Ilya Dryomov59641012012-01-16 22:04:48 +02003006 struct btrfs_balance_control *bctl;
3007 struct btrfs_balance_item *item;
3008 struct btrfs_disk_balance_args disk_bargs;
3009 struct btrfs_path *path;
3010 struct extent_buffer *leaf;
3011 struct btrfs_key key;
3012 int ret;
3013
3014 path = btrfs_alloc_path();
3015 if (!path)
3016 return -ENOMEM;
3017
Ilya Dryomov68310a52012-06-22 12:24:12 -06003018 key.objectid = BTRFS_BALANCE_OBJECTID;
3019 key.type = BTRFS_BALANCE_ITEM_KEY;
3020 key.offset = 0;
3021
3022 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3023 if (ret < 0)
3024 goto out;
3025 if (ret > 0) { /* ret = -ENOENT; */
3026 ret = 0;
3027 goto out;
3028 }
3029
Ilya Dryomov59641012012-01-16 22:04:48 +02003030 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3031 if (!bctl) {
3032 ret = -ENOMEM;
3033 goto out;
3034 }
3035
Ilya Dryomov59641012012-01-16 22:04:48 +02003036 leaf = path->nodes[0];
3037 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3038
Ilya Dryomov68310a52012-06-22 12:24:12 -06003039 bctl->fs_info = fs_info;
3040 bctl->flags = btrfs_balance_flags(leaf, item);
3041 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003042
3043 btrfs_balance_data(leaf, item, &disk_bargs);
3044 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3045 btrfs_balance_meta(leaf, item, &disk_bargs);
3046 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3047 btrfs_balance_sys(leaf, item, &disk_bargs);
3048 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3049
Ilya Dryomov68310a52012-06-22 12:24:12 -06003050 mutex_lock(&fs_info->volume_mutex);
3051 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003052
Ilya Dryomov68310a52012-06-22 12:24:12 -06003053 set_balance_control(bctl);
3054
3055 mutex_unlock(&fs_info->balance_mutex);
3056 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003057out:
3058 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003059 return ret;
3060}
3061
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003062int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3063{
3064 int ret = 0;
3065
3066 mutex_lock(&fs_info->balance_mutex);
3067 if (!fs_info->balance_ctl) {
3068 mutex_unlock(&fs_info->balance_mutex);
3069 return -ENOTCONN;
3070 }
3071
3072 if (atomic_read(&fs_info->balance_running)) {
3073 atomic_inc(&fs_info->balance_pause_req);
3074 mutex_unlock(&fs_info->balance_mutex);
3075
3076 wait_event(fs_info->balance_wait_q,
3077 atomic_read(&fs_info->balance_running) == 0);
3078
3079 mutex_lock(&fs_info->balance_mutex);
3080 /* we are good with balance_ctl ripped off from under us */
3081 BUG_ON(atomic_read(&fs_info->balance_running));
3082 atomic_dec(&fs_info->balance_pause_req);
3083 } else {
3084 ret = -ENOTCONN;
3085 }
3086
3087 mutex_unlock(&fs_info->balance_mutex);
3088 return ret;
3089}
3090
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003091int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3092{
3093 mutex_lock(&fs_info->balance_mutex);
3094 if (!fs_info->balance_ctl) {
3095 mutex_unlock(&fs_info->balance_mutex);
3096 return -ENOTCONN;
3097 }
3098
3099 atomic_inc(&fs_info->balance_cancel_req);
3100 /*
3101 * if we are running just wait and return, balance item is
3102 * deleted in btrfs_balance in this case
3103 */
3104 if (atomic_read(&fs_info->balance_running)) {
3105 mutex_unlock(&fs_info->balance_mutex);
3106 wait_event(fs_info->balance_wait_q,
3107 atomic_read(&fs_info->balance_running) == 0);
3108 mutex_lock(&fs_info->balance_mutex);
3109 } else {
3110 /* __cancel_balance needs volume_mutex */
3111 mutex_unlock(&fs_info->balance_mutex);
3112 mutex_lock(&fs_info->volume_mutex);
3113 mutex_lock(&fs_info->balance_mutex);
3114
3115 if (fs_info->balance_ctl)
3116 __cancel_balance(fs_info);
3117
3118 mutex_unlock(&fs_info->volume_mutex);
3119 }
3120
3121 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3122 atomic_dec(&fs_info->balance_cancel_req);
3123 mutex_unlock(&fs_info->balance_mutex);
3124 return 0;
3125}
3126
Chris Mason8f18cf12008-04-25 16:53:30 -04003127/*
3128 * shrinking a device means finding all of the device extents past
3129 * the new size, and then following the back refs to the chunks.
3130 * The chunk relocation code actually frees the device extent
3131 */
3132int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3133{
3134 struct btrfs_trans_handle *trans;
3135 struct btrfs_root *root = device->dev_root;
3136 struct btrfs_dev_extent *dev_extent = NULL;
3137 struct btrfs_path *path;
3138 u64 length;
3139 u64 chunk_tree;
3140 u64 chunk_objectid;
3141 u64 chunk_offset;
3142 int ret;
3143 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003144 int failed = 0;
3145 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003146 struct extent_buffer *l;
3147 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003148 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003149 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003150 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003151 u64 diff = device->total_bytes - new_size;
3152
Stefan Behrens63a212a2012-11-05 18:29:28 +01003153 if (device->is_tgtdev_for_dev_replace)
3154 return -EINVAL;
3155
Chris Mason8f18cf12008-04-25 16:53:30 -04003156 path = btrfs_alloc_path();
3157 if (!path)
3158 return -ENOMEM;
3159
Chris Mason8f18cf12008-04-25 16:53:30 -04003160 path->reada = 2;
3161
Chris Mason7d9eb122008-07-08 14:19:17 -04003162 lock_chunks(root);
3163
Chris Mason8f18cf12008-04-25 16:53:30 -04003164 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003165 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003166 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003167 spin_lock(&root->fs_info->free_chunk_lock);
3168 root->fs_info->free_chunk_space -= diff;
3169 spin_unlock(&root->fs_info->free_chunk_lock);
3170 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003171 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003172
Josef Bacikba1bf482009-09-11 16:11:19 -04003173again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003174 key.objectid = device->devid;
3175 key.offset = (u64)-1;
3176 key.type = BTRFS_DEV_EXTENT_KEY;
3177
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003178 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003179 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3180 if (ret < 0)
3181 goto done;
3182
3183 ret = btrfs_previous_item(root, path, 0, key.type);
3184 if (ret < 0)
3185 goto done;
3186 if (ret) {
3187 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003188 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003189 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003190 }
3191
3192 l = path->nodes[0];
3193 slot = path->slots[0];
3194 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3195
Josef Bacikba1bf482009-09-11 16:11:19 -04003196 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003197 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003198 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003199 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003200
3201 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3202 length = btrfs_dev_extent_length(l, dev_extent);
3203
Josef Bacikba1bf482009-09-11 16:11:19 -04003204 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003205 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003206 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003207 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003208
3209 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3210 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3211 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003212 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003213
3214 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3215 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003216 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003217 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003218 if (ret == -ENOSPC)
3219 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003220 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003221
3222 if (failed && !retried) {
3223 failed = 0;
3224 retried = true;
3225 goto again;
3226 } else if (failed && retried) {
3227 ret = -ENOSPC;
3228 lock_chunks(root);
3229
3230 device->total_bytes = old_size;
3231 if (device->writeable)
3232 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003233 spin_lock(&root->fs_info->free_chunk_lock);
3234 root->fs_info->free_chunk_space += diff;
3235 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003236 unlock_chunks(root);
3237 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003238 }
3239
Chris Balld6397ba2009-04-27 07:29:03 -04003240 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003241 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003242 if (IS_ERR(trans)) {
3243 ret = PTR_ERR(trans);
3244 goto done;
3245 }
3246
Chris Balld6397ba2009-04-27 07:29:03 -04003247 lock_chunks(root);
3248
3249 device->disk_total_bytes = new_size;
3250 /* Now btrfs_update_device() will change the on-disk size. */
3251 ret = btrfs_update_device(trans, device);
3252 if (ret) {
3253 unlock_chunks(root);
3254 btrfs_end_transaction(trans, root);
3255 goto done;
3256 }
3257 WARN_ON(diff > old_total);
3258 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3259 unlock_chunks(root);
3260 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003261done:
3262 btrfs_free_path(path);
3263 return ret;
3264}
3265
Li Zefan125ccb02011-12-08 15:07:24 +08003266static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003267 struct btrfs_key *key,
3268 struct btrfs_chunk *chunk, int item_size)
3269{
David Sterba6c417612011-04-13 15:41:04 +02003270 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003271 struct btrfs_disk_key disk_key;
3272 u32 array_size;
3273 u8 *ptr;
3274
3275 array_size = btrfs_super_sys_array_size(super_copy);
3276 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3277 return -EFBIG;
3278
3279 ptr = super_copy->sys_chunk_array + array_size;
3280 btrfs_cpu_key_to_disk(&disk_key, key);
3281 memcpy(ptr, &disk_key, sizeof(disk_key));
3282 ptr += sizeof(disk_key);
3283 memcpy(ptr, chunk, item_size);
3284 item_size += sizeof(disk_key);
3285 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3286 return 0;
3287}
3288
Miao Xieb2117a32011-01-05 10:07:28 +00003289/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003290 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003291 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003292static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003293{
Arne Jansen73c5de02011-04-12 12:07:57 +02003294 const struct btrfs_device_info *di_a = a;
3295 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003296
Arne Jansen73c5de02011-04-12 12:07:57 +02003297 if (di_a->max_avail > di_b->max_avail)
3298 return -1;
3299 if (di_a->max_avail < di_b->max_avail)
3300 return 1;
3301 if (di_a->total_avail > di_b->total_avail)
3302 return -1;
3303 if (di_a->total_avail < di_b->total_avail)
3304 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003305 return 0;
3306}
3307
3308static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3309 struct btrfs_root *extent_root,
3310 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003311 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003312 u64 start, u64 type)
3313{
3314 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003315 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3316 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003317 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003318 struct extent_map_tree *em_tree;
3319 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003320 struct btrfs_device_info *devices_info = NULL;
3321 u64 total_avail;
3322 int num_stripes; /* total number of stripes to allocate */
3323 int sub_stripes; /* sub_stripes info for map */
3324 int dev_stripes; /* stripes per dev */
3325 int devs_max; /* max devs to use */
3326 int devs_min; /* min devs needed */
3327 int devs_increment; /* ndevs has to be a multiple of this */
3328 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003329 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003330 u64 max_stripe_size;
3331 u64 max_chunk_size;
3332 u64 stripe_size;
3333 u64 num_bytes;
3334 int ndevs;
3335 int i;
3336 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003337
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003338 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003339
Miao Xieb2117a32011-01-05 10:07:28 +00003340 if (list_empty(&fs_devices->alloc_list))
3341 return -ENOSPC;
3342
Arne Jansen73c5de02011-04-12 12:07:57 +02003343 sub_stripes = 1;
3344 dev_stripes = 1;
3345 devs_increment = 1;
3346 ncopies = 1;
3347 devs_max = 0; /* 0 == as many as possible */
3348 devs_min = 1;
3349
3350 /*
3351 * define the properties of each RAID type.
3352 * FIXME: move this to a global table and use it in all RAID
3353 * calculation code
3354 */
3355 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3356 dev_stripes = 2;
3357 ncopies = 2;
3358 devs_max = 1;
3359 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3360 devs_min = 2;
3361 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3362 devs_increment = 2;
3363 ncopies = 2;
3364 devs_max = 2;
3365 devs_min = 2;
3366 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3367 sub_stripes = 2;
3368 devs_increment = 2;
3369 ncopies = 2;
3370 devs_min = 4;
3371 } else {
3372 devs_max = 1;
3373 }
3374
3375 if (type & BTRFS_BLOCK_GROUP_DATA) {
3376 max_stripe_size = 1024 * 1024 * 1024;
3377 max_chunk_size = 10 * max_stripe_size;
3378 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003379 /* for larger filesystems, use larger metadata chunks */
3380 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3381 max_stripe_size = 1024 * 1024 * 1024;
3382 else
3383 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003384 max_chunk_size = max_stripe_size;
3385 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003386 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003387 max_chunk_size = 2 * max_stripe_size;
3388 } else {
3389 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3390 type);
3391 BUG_ON(1);
3392 }
3393
3394 /* we don't want a chunk larger than 10% of writeable space */
3395 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3396 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003397
3398 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3399 GFP_NOFS);
3400 if (!devices_info)
3401 return -ENOMEM;
3402
Arne Jansen73c5de02011-04-12 12:07:57 +02003403 cur = fs_devices->alloc_list.next;
3404
3405 /*
3406 * in the first pass through the devices list, we gather information
3407 * about the available holes on each device.
3408 */
3409 ndevs = 0;
3410 while (cur != &fs_devices->alloc_list) {
3411 struct btrfs_device *device;
3412 u64 max_avail;
3413 u64 dev_offset;
3414
3415 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3416
3417 cur = cur->next;
3418
3419 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003420 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003421 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003422 continue;
3423 }
3424
Stefan Behrens63a212a2012-11-05 18:29:28 +01003425 if (!device->in_fs_metadata ||
3426 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003427 continue;
3428
3429 if (device->total_bytes > device->bytes_used)
3430 total_avail = device->total_bytes - device->bytes_used;
3431 else
3432 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003433
3434 /* If there is no space on this device, skip it. */
3435 if (total_avail == 0)
3436 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003437
Li Zefan125ccb02011-12-08 15:07:24 +08003438 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003439 max_stripe_size * dev_stripes,
3440 &dev_offset, &max_avail);
3441 if (ret && ret != -ENOSPC)
3442 goto error;
3443
3444 if (ret == 0)
3445 max_avail = max_stripe_size * dev_stripes;
3446
3447 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3448 continue;
3449
3450 devices_info[ndevs].dev_offset = dev_offset;
3451 devices_info[ndevs].max_avail = max_avail;
3452 devices_info[ndevs].total_avail = total_avail;
3453 devices_info[ndevs].dev = device;
3454 ++ndevs;
3455 }
3456
3457 /*
3458 * now sort the devices by hole size / available space
3459 */
3460 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3461 btrfs_cmp_device_info, NULL);
3462
3463 /* round down to number of usable stripes */
3464 ndevs -= ndevs % devs_increment;
3465
3466 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3467 ret = -ENOSPC;
3468 goto error;
3469 }
3470
3471 if (devs_max && ndevs > devs_max)
3472 ndevs = devs_max;
3473 /*
3474 * the primary goal is to maximize the number of stripes, so use as many
3475 * devices as possible, even if the stripes are not maximum sized.
3476 */
3477 stripe_size = devices_info[ndevs-1].max_avail;
3478 num_stripes = ndevs * dev_stripes;
3479
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003480 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003481 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003482 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003483 }
3484
3485 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003486
3487 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003488 do_div(stripe_size, BTRFS_STRIPE_LEN);
3489 stripe_size *= BTRFS_STRIPE_LEN;
3490
Miao Xieb2117a32011-01-05 10:07:28 +00003491 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3492 if (!map) {
3493 ret = -ENOMEM;
3494 goto error;
3495 }
3496 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003497
Arne Jansen73c5de02011-04-12 12:07:57 +02003498 for (i = 0; i < ndevs; ++i) {
3499 for (j = 0; j < dev_stripes; ++j) {
3500 int s = i * dev_stripes + j;
3501 map->stripes[s].dev = devices_info[i].dev;
3502 map->stripes[s].physical = devices_info[i].dev_offset +
3503 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003504 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003505 }
Chris Mason593060d2008-03-25 16:50:33 -04003506 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003507 map->stripe_len = BTRFS_STRIPE_LEN;
3508 map->io_align = BTRFS_STRIPE_LEN;
3509 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003510 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003511 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003512
Yan Zheng2b820322008-11-17 21:11:30 -05003513 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003514 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003515
Arne Jansen73c5de02011-04-12 12:07:57 +02003516 *stripe_size_out = stripe_size;
3517 *num_bytes_out = num_bytes;
3518
3519 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003520
David Sterba172ddd62011-04-21 00:48:27 +02003521 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003522 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003523 ret = -ENOMEM;
3524 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003525 }
Chris Mason0b86a832008-03-24 15:01:56 -04003526 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003527 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003528 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003529 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003530 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003531
Chris Mason0b86a832008-03-24 15:01:56 -04003532 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003533 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003534 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003535 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003536 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003537 if (ret)
3538 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003539
3540 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3541 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003542 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003543 if (ret)
3544 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003545
Arne Jansen73c5de02011-04-12 12:07:57 +02003546 for (i = 0; i < map->num_stripes; ++i) {
3547 struct btrfs_device *device;
3548 u64 dev_offset;
3549
3550 device = map->stripes[i].dev;
3551 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003552
3553 ret = btrfs_alloc_dev_extent(trans, device,
3554 info->chunk_root->root_key.objectid,
3555 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003556 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003557 if (ret) {
3558 btrfs_abort_transaction(trans, extent_root, ret);
3559 goto error;
3560 }
Yan Zheng2b820322008-11-17 21:11:30 -05003561 }
3562
Miao Xieb2117a32011-01-05 10:07:28 +00003563 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003564 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003565
3566error:
3567 kfree(map);
3568 kfree(devices_info);
3569 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003570}
3571
3572static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3573 struct btrfs_root *extent_root,
3574 struct map_lookup *map, u64 chunk_offset,
3575 u64 chunk_size, u64 stripe_size)
3576{
3577 u64 dev_offset;
3578 struct btrfs_key key;
3579 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3580 struct btrfs_device *device;
3581 struct btrfs_chunk *chunk;
3582 struct btrfs_stripe *stripe;
3583 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3584 int index = 0;
3585 int ret;
3586
3587 chunk = kzalloc(item_size, GFP_NOFS);
3588 if (!chunk)
3589 return -ENOMEM;
3590
3591 index = 0;
3592 while (index < map->num_stripes) {
3593 device = map->stripes[index].dev;
3594 device->bytes_used += stripe_size;
3595 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003596 if (ret)
3597 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003598 index++;
3599 }
3600
Josef Bacik2bf64752011-09-26 17:12:22 -04003601 spin_lock(&extent_root->fs_info->free_chunk_lock);
3602 extent_root->fs_info->free_chunk_space -= (stripe_size *
3603 map->num_stripes);
3604 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3605
Yan Zheng2b820322008-11-17 21:11:30 -05003606 index = 0;
3607 stripe = &chunk->stripe;
3608 while (index < map->num_stripes) {
3609 device = map->stripes[index].dev;
3610 dev_offset = map->stripes[index].physical;
3611
3612 btrfs_set_stack_stripe_devid(stripe, device->devid);
3613 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3614 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3615 stripe++;
3616 index++;
3617 }
3618
3619 btrfs_set_stack_chunk_length(chunk, chunk_size);
3620 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3621 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3622 btrfs_set_stack_chunk_type(chunk, map->type);
3623 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3624 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3625 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3626 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3627 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3628
3629 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3630 key.type = BTRFS_CHUNK_ITEM_KEY;
3631 key.offset = chunk_offset;
3632
3633 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003634
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003635 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3636 /*
3637 * TODO: Cleanup of inserted chunk root in case of
3638 * failure.
3639 */
Li Zefan125ccb02011-12-08 15:07:24 +08003640 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003641 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003642 }
liubo1abe9b82011-03-24 11:18:59 +00003643
Mark Fasheh3acd3952011-09-08 17:40:01 -07003644out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003645 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003646 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003647}
3648
3649/*
3650 * Chunk allocation falls into two parts. The first part does works
3651 * that make the new allocated chunk useable, but not do any operation
3652 * that modifies the chunk tree. The second part does the works that
3653 * require modifying the chunk tree. This division is important for the
3654 * bootstrap process of adding storage to a seed btrfs.
3655 */
3656int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3657 struct btrfs_root *extent_root, u64 type)
3658{
3659 u64 chunk_offset;
3660 u64 chunk_size;
3661 u64 stripe_size;
3662 struct map_lookup *map;
3663 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3664 int ret;
3665
3666 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3667 &chunk_offset);
3668 if (ret)
3669 return ret;
3670
3671 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3672 &stripe_size, chunk_offset, type);
3673 if (ret)
3674 return ret;
3675
3676 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3677 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003678 if (ret)
3679 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003680 return 0;
3681}
3682
Chris Masond3977122009-01-05 21:25:51 -05003683static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003684 struct btrfs_root *root,
3685 struct btrfs_device *device)
3686{
3687 u64 chunk_offset;
3688 u64 sys_chunk_offset;
3689 u64 chunk_size;
3690 u64 sys_chunk_size;
3691 u64 stripe_size;
3692 u64 sys_stripe_size;
3693 u64 alloc_profile;
3694 struct map_lookup *map;
3695 struct map_lookup *sys_map;
3696 struct btrfs_fs_info *fs_info = root->fs_info;
3697 struct btrfs_root *extent_root = fs_info->extent_root;
3698 int ret;
3699
3700 ret = find_next_chunk(fs_info->chunk_root,
3701 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003702 if (ret)
3703 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003704
3705 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003706 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003707 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3708
3709 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3710 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003711 if (ret)
3712 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003713
3714 sys_chunk_offset = chunk_offset + chunk_size;
3715
3716 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003717 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003718 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3719
3720 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3721 &sys_chunk_size, &sys_stripe_size,
3722 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06003723 if (ret) {
3724 btrfs_abort_transaction(trans, root, ret);
3725 goto out;
3726 }
Yan Zheng2b820322008-11-17 21:11:30 -05003727
3728 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06003729 if (ret) {
3730 btrfs_abort_transaction(trans, root, ret);
3731 goto out;
3732 }
Yan Zheng2b820322008-11-17 21:11:30 -05003733
3734 /*
3735 * Modifying chunk tree needs allocating new blocks from both
3736 * system block group and metadata block group. So we only can
3737 * do operations require modifying the chunk tree after both
3738 * block groups were created.
3739 */
3740 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3741 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06003742 if (ret) {
3743 btrfs_abort_transaction(trans, root, ret);
3744 goto out;
3745 }
Yan Zheng2b820322008-11-17 21:11:30 -05003746
3747 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3748 sys_chunk_offset, sys_chunk_size,
3749 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003750 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06003751 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003752
David Sterba005d6422012-09-18 07:52:32 -06003753out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003755 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003756}
3757
3758int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3759{
3760 struct extent_map *em;
3761 struct map_lookup *map;
3762 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3763 int readonly = 0;
3764 int i;
3765
Chris Mason890871b2009-09-02 16:24:52 -04003766 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003767 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003768 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003769 if (!em)
3770 return 1;
3771
Josef Bacikf48b9072010-01-27 02:07:59 +00003772 if (btrfs_test_opt(root, DEGRADED)) {
3773 free_extent_map(em);
3774 return 0;
3775 }
3776
Yan Zheng2b820322008-11-17 21:11:30 -05003777 map = (struct map_lookup *)em->bdev;
3778 for (i = 0; i < map->num_stripes; i++) {
3779 if (!map->stripes[i].dev->writeable) {
3780 readonly = 1;
3781 break;
3782 }
3783 }
3784 free_extent_map(em);
3785 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003786}
3787
3788void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3789{
David Sterbaa8067e02011-04-21 00:34:43 +02003790 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003791}
3792
3793void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3794{
3795 struct extent_map *em;
3796
Chris Masond3977122009-01-05 21:25:51 -05003797 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003798 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003799 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3800 if (em)
3801 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003802 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003803 if (!em)
3804 break;
3805 kfree(em->bdev);
3806 /* once for us */
3807 free_extent_map(em);
3808 /* once for the tree */
3809 free_extent_map(em);
3810 }
3811}
3812
Stefan Behrens5d964052012-11-05 14:59:07 +01003813int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04003814{
Stefan Behrens5d964052012-11-05 14:59:07 +01003815 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04003816 struct extent_map *em;
3817 struct map_lookup *map;
3818 struct extent_map_tree *em_tree = &map_tree->map_tree;
3819 int ret;
3820
Chris Mason890871b2009-09-02 16:24:52 -04003821 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003822 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003823 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003824 BUG_ON(!em);
3825
3826 BUG_ON(em->start > logical || em->start + em->len < logical);
3827 map = (struct map_lookup *)em->bdev;
3828 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3829 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003830 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3831 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003832 else
3833 ret = 1;
3834 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003835 return ret;
3836}
3837
Chris Masondfe25022008-05-13 13:46:40 -04003838static int find_live_mirror(struct map_lookup *map, int first, int num,
3839 int optimal)
3840{
3841 int i;
3842 if (map->stripes[optimal].dev->bdev)
3843 return optimal;
3844 for (i = first; i < first + num; i++) {
3845 if (map->stripes[i].dev->bdev)
3846 return i;
3847 }
3848 /* we couldn't find one that doesn't fail. Just return something
3849 * and the io error handling code will clean up eventually
3850 */
3851 return optimal;
3852}
3853
Stefan Behrens3ec706c2012-11-05 15:46:42 +01003854static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04003855 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003856 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003857 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003858{
3859 struct extent_map *em;
3860 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01003861 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04003862 struct extent_map_tree *em_tree = &map_tree->map_tree;
3863 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003864 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003865 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003866 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003867 u64 stripe_nr_orig;
3868 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003869 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003870 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003871 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003872 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003873 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003874 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003875
Chris Mason890871b2009-09-02 16:24:52 -04003876 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003877 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003878 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003879
Chris Mason3b951512008-04-17 11:29:12 -04003880 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06003881 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003882 (unsigned long long)logical,
3883 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003884 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003885 }
Chris Mason0b86a832008-03-24 15:01:56 -04003886
3887 BUG_ON(em->start > logical || em->start + em->len < logical);
3888 map = (struct map_lookup *)em->bdev;
3889 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003890
Chris Masonf1885912008-04-09 16:28:12 -04003891 if (mirror_num > map->num_stripes)
3892 mirror_num = 0;
3893
Chris Mason593060d2008-03-25 16:50:33 -04003894 stripe_nr = offset;
3895 /*
3896 * stripe_nr counts the total number of stripes we have to stride
3897 * to get to this block
3898 */
3899 do_div(stripe_nr, map->stripe_len);
3900
3901 stripe_offset = stripe_nr * map->stripe_len;
3902 BUG_ON(offset < stripe_offset);
3903
3904 /* stripe_offset is the offset of this block in its stripe*/
3905 stripe_offset = offset - stripe_offset;
3906
Li Dongyangfce3bb92011-03-24 10:24:26 +00003907 if (rw & REQ_DISCARD)
3908 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003909 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003910 /* we limit the length of each bio to what fits in a stripe */
3911 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003912 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003913 } else {
3914 *length = em->len - offset;
3915 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003916
Jan Schmidta1d3c472011-08-04 17:15:33 +02003917 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003918 goto out;
3919
Chris Masonf2d8d742008-04-21 10:03:05 -04003920 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003921 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003922 stripe_nr_orig = stripe_nr;
3923 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3924 (~(map->stripe_len - 1));
3925 do_div(stripe_nr_end, map->stripe_len);
3926 stripe_end_offset = stripe_nr_end * map->stripe_len -
3927 (offset + *length);
3928 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3929 if (rw & REQ_DISCARD)
3930 num_stripes = min_t(u64, map->num_stripes,
3931 stripe_nr_end - stripe_nr_orig);
3932 stripe_index = do_div(stripe_nr, map->num_stripes);
3933 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003934 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003935 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003936 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003937 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003938 else {
3939 stripe_index = find_live_mirror(map, 0,
3940 map->num_stripes,
3941 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003942 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003943 }
Chris Mason2fff7342008-04-29 14:12:09 -04003944
Chris Mason611f0e02008-04-03 16:29:03 -04003945 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003946 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003947 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003948 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003949 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003950 } else {
3951 mirror_num = 1;
3952 }
Chris Mason2fff7342008-04-29 14:12:09 -04003953
Chris Mason321aecc2008-04-16 10:49:51 -04003954 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3955 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003956
3957 stripe_index = do_div(stripe_nr, factor);
3958 stripe_index *= map->sub_stripes;
3959
Jens Axboe7eaceac2011-03-10 08:52:07 +01003960 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003961 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003962 else if (rw & REQ_DISCARD)
3963 num_stripes = min_t(u64, map->sub_stripes *
3964 (stripe_nr_end - stripe_nr_orig),
3965 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003966 else if (mirror_num)
3967 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003968 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003969 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003970 stripe_index = find_live_mirror(map, stripe_index,
3971 map->sub_stripes, stripe_index +
3972 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003973 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003974 }
Chris Mason8790d502008-04-03 16:29:03 -04003975 } else {
3976 /*
3977 * after this do_div call, stripe_nr is the number of stripes
3978 * on this device we have to walk to find the data, and
3979 * stripe_index is the number of our device in the stripe array
3980 */
3981 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003982 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003983 }
Chris Mason593060d2008-03-25 16:50:33 -04003984 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003985
Li Zefande11cc12011-12-01 12:55:47 +08003986 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3987 if (!bbio) {
3988 ret = -ENOMEM;
3989 goto out;
3990 }
3991 atomic_set(&bbio->error, 0);
3992
Li Dongyangfce3bb92011-03-24 10:24:26 +00003993 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003994 int factor = 0;
3995 int sub_stripes = 0;
3996 u64 stripes_per_dev = 0;
3997 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003998 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003999
4000 if (map->type &
4001 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4002 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4003 sub_stripes = 1;
4004 else
4005 sub_stripes = map->sub_stripes;
4006
4007 factor = map->num_stripes / sub_stripes;
4008 stripes_per_dev = div_u64_rem(stripe_nr_end -
4009 stripe_nr_orig,
4010 factor,
4011 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004012 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4013 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004014 }
4015
Li Dongyangfce3bb92011-03-24 10:24:26 +00004016 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004017 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004018 map->stripes[stripe_index].physical +
4019 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004020 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004021
Li Zefanec9ef7a2011-12-01 14:06:42 +08004022 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4023 BTRFS_BLOCK_GROUP_RAID10)) {
4024 bbio->stripes[i].length = stripes_per_dev *
4025 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004026
Li Zefanec9ef7a2011-12-01 14:06:42 +08004027 if (i / sub_stripes < remaining_stripes)
4028 bbio->stripes[i].length +=
4029 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004030
4031 /*
4032 * Special for the first stripe and
4033 * the last stripe:
4034 *
4035 * |-------|...|-------|
4036 * |----------|
4037 * off end_off
4038 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004039 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004040 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004041 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004042
4043 if (stripe_index >= last_stripe &&
4044 stripe_index <= (last_stripe +
4045 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004046 bbio->stripes[i].length -=
4047 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004048
Li Zefanec9ef7a2011-12-01 14:06:42 +08004049 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004050 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004051 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004052 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004053
4054 stripe_index++;
4055 if (stripe_index == map->num_stripes) {
4056 /* This could only happen for RAID0/10 */
4057 stripe_index = 0;
4058 stripe_nr++;
4059 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004060 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004061 } else {
4062 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004063 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004064 map->stripes[stripe_index].physical +
4065 stripe_offset +
4066 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004067 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004068 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004069 stripe_index++;
4070 }
Chris Mason593060d2008-03-25 16:50:33 -04004071 }
Li Zefande11cc12011-12-01 12:55:47 +08004072
4073 if (rw & REQ_WRITE) {
4074 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4075 BTRFS_BLOCK_GROUP_RAID10 |
4076 BTRFS_BLOCK_GROUP_DUP)) {
4077 max_errors = 1;
4078 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004079 }
Li Zefande11cc12011-12-01 12:55:47 +08004080
4081 *bbio_ret = bbio;
4082 bbio->num_stripes = num_stripes;
4083 bbio->max_errors = max_errors;
4084 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04004085out:
Chris Mason0b86a832008-03-24 15:01:56 -04004086 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004087 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004088}
4089
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004090int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004091 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004092 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004093{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004094 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004095 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04004096}
4097
Yan Zhenga512bbf2008-12-08 16:46:26 -05004098int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4099 u64 chunk_start, u64 physical, u64 devid,
4100 u64 **logical, int *naddrs, int *stripe_len)
4101{
4102 struct extent_map_tree *em_tree = &map_tree->map_tree;
4103 struct extent_map *em;
4104 struct map_lookup *map;
4105 u64 *buf;
4106 u64 bytenr;
4107 u64 length;
4108 u64 stripe_nr;
4109 int i, j, nr = 0;
4110
Chris Mason890871b2009-09-02 16:24:52 -04004111 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004112 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004113 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004114
4115 BUG_ON(!em || em->start != chunk_start);
4116 map = (struct map_lookup *)em->bdev;
4117
4118 length = em->len;
4119 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4120 do_div(length, map->num_stripes / map->sub_stripes);
4121 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4122 do_div(length, map->num_stripes);
4123
4124 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004126
4127 for (i = 0; i < map->num_stripes; i++) {
4128 if (devid && map->stripes[i].dev->devid != devid)
4129 continue;
4130 if (map->stripes[i].physical > physical ||
4131 map->stripes[i].physical + length <= physical)
4132 continue;
4133
4134 stripe_nr = physical - map->stripes[i].physical;
4135 do_div(stripe_nr, map->stripe_len);
4136
4137 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4138 stripe_nr = stripe_nr * map->num_stripes + i;
4139 do_div(stripe_nr, map->sub_stripes);
4140 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4141 stripe_nr = stripe_nr * map->num_stripes + i;
4142 }
4143 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004144 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004145 for (j = 0; j < nr; j++) {
4146 if (buf[j] == bytenr)
4147 break;
4148 }
Chris Mason934d3752008-12-08 16:43:10 -05004149 if (j == nr) {
4150 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004151 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004152 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004153 }
4154
Yan Zhenga512bbf2008-12-08 16:46:26 -05004155 *logical = buf;
4156 *naddrs = nr;
4157 *stripe_len = map->stripe_len;
4158
4159 free_extent_map(em);
4160 return 0;
4161}
4162
Stefan Behrens442a4f62012-05-25 16:06:08 +02004163static void *merge_stripe_index_into_bio_private(void *bi_private,
4164 unsigned int stripe_index)
4165{
4166 /*
4167 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4168 * at most 1.
4169 * The alternative solution (instead of stealing bits from the
4170 * pointer) would be to allocate an intermediate structure
4171 * that contains the old private pointer plus the stripe_index.
4172 */
4173 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4174 BUG_ON(stripe_index > 3);
4175 return (void *)(((uintptr_t)bi_private) | stripe_index);
4176}
4177
4178static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4179{
4180 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4181}
4182
4183static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4184{
4185 return (unsigned int)((uintptr_t)bi_private) & 3;
4186}
4187
Jan Schmidta1d3c472011-08-04 17:15:33 +02004188static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004189{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004190 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004191 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004192
Stefan Behrens442a4f62012-05-25 16:06:08 +02004193 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004194 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004195 if (err == -EIO || err == -EREMOTEIO) {
4196 unsigned int stripe_index =
4197 extract_stripe_index_from_bio_private(
4198 bio->bi_private);
4199 struct btrfs_device *dev;
4200
4201 BUG_ON(stripe_index >= bbio->num_stripes);
4202 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004203 if (dev->bdev) {
4204 if (bio->bi_rw & WRITE)
4205 btrfs_dev_stat_inc(dev,
4206 BTRFS_DEV_STAT_WRITE_ERRS);
4207 else
4208 btrfs_dev_stat_inc(dev,
4209 BTRFS_DEV_STAT_READ_ERRS);
4210 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4211 btrfs_dev_stat_inc(dev,
4212 BTRFS_DEV_STAT_FLUSH_ERRS);
4213 btrfs_dev_stat_print_on_error(dev);
4214 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004215 }
4216 }
Chris Mason8790d502008-04-03 16:29:03 -04004217
Jan Schmidta1d3c472011-08-04 17:15:33 +02004218 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004219 is_orig_bio = 1;
4220
Jan Schmidta1d3c472011-08-04 17:15:33 +02004221 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004222 if (!is_orig_bio) {
4223 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004224 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004225 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004226 bio->bi_private = bbio->private;
4227 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004228 bio->bi_bdev = (struct block_device *)
4229 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004230 /* only send an error to the higher layers if it is
4231 * beyond the tolerance of the multi-bio
4232 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004233 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004234 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004235 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004236 /*
4237 * this bio is actually up to date, we didn't
4238 * go over the max number of errors
4239 */
4240 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004241 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004242 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004243 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004244
4245 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004246 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004247 bio_put(bio);
4248 }
Chris Mason8790d502008-04-03 16:29:03 -04004249}
4250
Chris Mason8b712842008-06-11 16:50:36 -04004251struct async_sched {
4252 struct bio *bio;
4253 int rw;
4254 struct btrfs_fs_info *info;
4255 struct btrfs_work work;
4256};
4257
4258/*
4259 * see run_scheduled_bios for a description of why bios are collected for
4260 * async submit.
4261 *
4262 * This will add one bio to the pending list for a device and make sure
4263 * the work struct is scheduled.
4264 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004265static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004266 struct btrfs_device *device,
4267 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004268{
4269 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004270 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004271
4272 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004273 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004274 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004275 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004276 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004277 return;
Chris Mason8b712842008-06-11 16:50:36 -04004278 }
4279
4280 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004281 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004282 * higher layers. Otherwise, the async bio makes it appear we have
4283 * made progress against dirty pages when we've really just put it
4284 * on a queue for later
4285 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004286 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004287 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004288 bio->bi_next = NULL;
4289 bio->bi_rw |= rw;
4290
4291 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004292 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004293 pending_bios = &device->pending_sync_bios;
4294 else
4295 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004296
Chris Masonffbd5172009-04-20 15:50:09 -04004297 if (pending_bios->tail)
4298 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004299
Chris Masonffbd5172009-04-20 15:50:09 -04004300 pending_bios->tail = bio;
4301 if (!pending_bios->head)
4302 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004303 if (device->running_pending)
4304 should_queue = 0;
4305
4306 spin_unlock(&device->io_lock);
4307
4308 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004309 btrfs_queue_worker(&root->fs_info->submit_workers,
4310 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004311}
4312
Josef Bacikde1ee922012-10-19 16:50:56 -04004313static int bio_size_ok(struct block_device *bdev, struct bio *bio,
4314 sector_t sector)
4315{
4316 struct bio_vec *prev;
4317 struct request_queue *q = bdev_get_queue(bdev);
4318 unsigned short max_sectors = queue_max_sectors(q);
4319 struct bvec_merge_data bvm = {
4320 .bi_bdev = bdev,
4321 .bi_sector = sector,
4322 .bi_rw = bio->bi_rw,
4323 };
4324
4325 if (bio->bi_vcnt == 0) {
4326 WARN_ON(1);
4327 return 1;
4328 }
4329
4330 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
4331 if ((bio->bi_size >> 9) > max_sectors)
4332 return 0;
4333
4334 if (!q->merge_bvec_fn)
4335 return 1;
4336
4337 bvm.bi_size = bio->bi_size - prev->bv_len;
4338 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
4339 return 0;
4340 return 1;
4341}
4342
4343static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4344 struct bio *bio, u64 physical, int dev_nr,
4345 int rw, int async)
4346{
4347 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
4348
4349 bio->bi_private = bbio;
4350 bio->bi_private = merge_stripe_index_into_bio_private(
4351 bio->bi_private, (unsigned int)dev_nr);
4352 bio->bi_end_io = btrfs_end_bio;
4353 bio->bi_sector = physical >> 9;
4354#ifdef DEBUG
4355 {
4356 struct rcu_string *name;
4357
4358 rcu_read_lock();
4359 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00004360 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04004361 "(%s id %llu), size=%u\n", rw,
4362 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4363 name->str, dev->devid, bio->bi_size);
4364 rcu_read_unlock();
4365 }
4366#endif
4367 bio->bi_bdev = dev->bdev;
4368 if (async)
4369 schedule_bio(root, dev, rw, bio);
4370 else
4371 btrfsic_submit_bio(rw, bio);
4372}
4373
4374static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4375 struct bio *first_bio, struct btrfs_device *dev,
4376 int dev_nr, int rw, int async)
4377{
4378 struct bio_vec *bvec = first_bio->bi_io_vec;
4379 struct bio *bio;
4380 int nr_vecs = bio_get_nr_vecs(dev->bdev);
4381 u64 physical = bbio->stripes[dev_nr].physical;
4382
4383again:
4384 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
4385 if (!bio)
4386 return -ENOMEM;
4387
4388 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
4389 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
4390 bvec->bv_offset) < bvec->bv_len) {
4391 u64 len = bio->bi_size;
4392
4393 atomic_inc(&bbio->stripes_pending);
4394 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
4395 rw, async);
4396 physical += len;
4397 goto again;
4398 }
4399 bvec++;
4400 }
4401
4402 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
4403 return 0;
4404}
4405
4406static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
4407{
4408 atomic_inc(&bbio->error);
4409 if (atomic_dec_and_test(&bbio->stripes_pending)) {
4410 bio->bi_private = bbio->private;
4411 bio->bi_end_io = bbio->end_io;
4412 bio->bi_bdev = (struct block_device *)
4413 (unsigned long)bbio->mirror_num;
4414 bio->bi_sector = logical >> 9;
4415 kfree(bbio);
4416 bio_endio(bio, -EIO);
4417 }
4418}
4419
Chris Masonf1885912008-04-09 16:28:12 -04004420int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004421 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004422{
Chris Mason0b86a832008-03-24 15:01:56 -04004423 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004424 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004425 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004426 u64 length = 0;
4427 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004428 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004429 int dev_nr = 0;
4430 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004431 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004432
Chris Masonf2d8d742008-04-21 10:03:05 -04004433 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004434 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004435
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004436 ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004437 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004438 if (ret) /* -ENOMEM */
4439 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004440
Jan Schmidta1d3c472011-08-04 17:15:33 +02004441 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004442 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004443 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05004444 "len %llu\n", (unsigned long long)logical,
4445 (unsigned long long)length,
4446 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004447 BUG();
4448 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004449
4450 bbio->orig_bio = first_bio;
4451 bbio->private = first_bio->bi_private;
4452 bbio->end_io = first_bio->bi_end_io;
4453 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004454
Chris Masond3977122009-01-05 21:25:51 -05004455 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04004456 dev = bbio->stripes[dev_nr].dev;
4457 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
4458 bbio_error(bbio, first_bio, logical);
4459 dev_nr++;
4460 continue;
4461 }
4462
4463 /*
4464 * Check and see if we're ok with this bio based on it's size
4465 * and offset with the given device.
4466 */
4467 if (!bio_size_ok(dev->bdev, first_bio,
4468 bbio->stripes[dev_nr].physical >> 9)) {
4469 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
4470 dev_nr, rw, async_submit);
4471 BUG_ON(ret);
4472 dev_nr++;
4473 continue;
4474 }
4475
Jan Schmidta1d3c472011-08-04 17:15:33 +02004476 if (dev_nr < total_devs - 1) {
4477 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004478 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004479 } else {
4480 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004481 }
Josef Bacik606686e2012-06-04 14:03:51 -04004482
Josef Bacikde1ee922012-10-19 16:50:56 -04004483 submit_stripe_bio(root, bbio, bio,
4484 bbio->stripes[dev_nr].physical, dev_nr, rw,
4485 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04004486 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004487 }
Chris Mason0b86a832008-03-24 15:01:56 -04004488 return 0;
4489}
4490
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004491struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004492 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004493{
Yan Zheng2b820322008-11-17 21:11:30 -05004494 struct btrfs_device *device;
4495 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004496
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004497 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004498 while (cur_devices) {
4499 if (!fsid ||
4500 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4501 device = __find_device(&cur_devices->devices,
4502 devid, uuid);
4503 if (device)
4504 return device;
4505 }
4506 cur_devices = cur_devices->seed;
4507 }
4508 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004509}
4510
Chris Masondfe25022008-05-13 13:46:40 -04004511static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4512 u64 devid, u8 *dev_uuid)
4513{
4514 struct btrfs_device *device;
4515 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4516
4517 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004518 if (!device)
4519 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004520 list_add(&device->dev_list,
4521 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004522 device->dev_root = root->fs_info->dev_root;
4523 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004524 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004525 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004526 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004527 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004528 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004529 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004530 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004531 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4532 return device;
4533}
4534
Chris Mason0b86a832008-03-24 15:01:56 -04004535static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4536 struct extent_buffer *leaf,
4537 struct btrfs_chunk *chunk)
4538{
4539 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4540 struct map_lookup *map;
4541 struct extent_map *em;
4542 u64 logical;
4543 u64 length;
4544 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004545 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004546 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004547 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004548 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004549
Chris Masone17cade2008-04-15 15:41:47 -04004550 logical = key->offset;
4551 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004552
Chris Mason890871b2009-09-02 16:24:52 -04004553 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004554 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004555 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004556
4557 /* already mapped? */
4558 if (em && em->start <= logical && em->start + em->len > logical) {
4559 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004560 return 0;
4561 } else if (em) {
4562 free_extent_map(em);
4563 }
Chris Mason0b86a832008-03-24 15:01:56 -04004564
David Sterba172ddd62011-04-21 00:48:27 +02004565 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004566 if (!em)
4567 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004568 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4569 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004570 if (!map) {
4571 free_extent_map(em);
4572 return -ENOMEM;
4573 }
4574
4575 em->bdev = (struct block_device *)map;
4576 em->start = logical;
4577 em->len = length;
4578 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004579 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004580
Chris Mason593060d2008-03-25 16:50:33 -04004581 map->num_stripes = num_stripes;
4582 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4583 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4584 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4585 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4586 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004587 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004588 for (i = 0; i < num_stripes; i++) {
4589 map->stripes[i].physical =
4590 btrfs_stripe_offset_nr(leaf, chunk, i);
4591 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004592 read_extent_buffer(leaf, uuid, (unsigned long)
4593 btrfs_stripe_dev_uuid_nr(chunk, i),
4594 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004595 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
4596 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004597 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004598 kfree(map);
4599 free_extent_map(em);
4600 return -EIO;
4601 }
Chris Masondfe25022008-05-13 13:46:40 -04004602 if (!map->stripes[i].dev) {
4603 map->stripes[i].dev =
4604 add_missing_dev(root, devid, uuid);
4605 if (!map->stripes[i].dev) {
4606 kfree(map);
4607 free_extent_map(em);
4608 return -EIO;
4609 }
4610 }
4611 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004612 }
4613
Chris Mason890871b2009-09-02 16:24:52 -04004614 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004615 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004616 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004617 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004618 free_extent_map(em);
4619
4620 return 0;
4621}
4622
Jeff Mahoney143bede2012-03-01 14:56:26 +01004623static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004624 struct btrfs_dev_item *dev_item,
4625 struct btrfs_device *device)
4626{
4627 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004628
4629 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004630 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4631 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004632 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4633 device->type = btrfs_device_type(leaf, dev_item);
4634 device->io_align = btrfs_device_io_align(leaf, dev_item);
4635 device->io_width = btrfs_device_io_width(leaf, dev_item);
4636 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004637 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004638
4639 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004640 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004641}
4642
Yan Zheng2b820322008-11-17 21:11:30 -05004643static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4644{
4645 struct btrfs_fs_devices *fs_devices;
4646 int ret;
4647
Li Zefanb367e472011-12-07 11:38:24 +08004648 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004649
4650 fs_devices = root->fs_info->fs_devices->seed;
4651 while (fs_devices) {
4652 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4653 ret = 0;
4654 goto out;
4655 }
4656 fs_devices = fs_devices->seed;
4657 }
4658
4659 fs_devices = find_fsid(fsid);
4660 if (!fs_devices) {
4661 ret = -ENOENT;
4662 goto out;
4663 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004664
4665 fs_devices = clone_fs_devices(fs_devices);
4666 if (IS_ERR(fs_devices)) {
4667 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004668 goto out;
4669 }
4670
Christoph Hellwig97288f22008-12-02 06:36:09 -05004671 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004672 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004673 if (ret) {
4674 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004675 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004676 }
Yan Zheng2b820322008-11-17 21:11:30 -05004677
4678 if (!fs_devices->seeding) {
4679 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004680 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004681 ret = -EINVAL;
4682 goto out;
4683 }
4684
4685 fs_devices->seed = root->fs_info->fs_devices->seed;
4686 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004687out:
Yan Zheng2b820322008-11-17 21:11:30 -05004688 return ret;
4689}
4690
Chris Mason0d81ba52008-03-24 15:02:07 -04004691static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004692 struct extent_buffer *leaf,
4693 struct btrfs_dev_item *dev_item)
4694{
4695 struct btrfs_device *device;
4696 u64 devid;
4697 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004698 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004699 u8 dev_uuid[BTRFS_UUID_SIZE];
4700
Chris Mason0b86a832008-03-24 15:01:56 -04004701 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004702 read_extent_buffer(leaf, dev_uuid,
4703 (unsigned long)btrfs_device_uuid(dev_item),
4704 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004705 read_extent_buffer(leaf, fs_uuid,
4706 (unsigned long)btrfs_device_fsid(dev_item),
4707 BTRFS_UUID_SIZE);
4708
4709 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4710 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004711 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004712 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004713 }
4714
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004715 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05004716 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004717 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004718 return -EIO;
4719
4720 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004721 printk(KERN_WARNING "warning devid %llu missing\n",
4722 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004723 device = add_missing_dev(root, devid, dev_uuid);
4724 if (!device)
4725 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004726 } else if (!device->missing) {
4727 /*
4728 * this happens when a device that was properly setup
4729 * in the device info lists suddenly goes bad.
4730 * device->bdev is NULL, and so we have to set
4731 * device->missing to one here
4732 */
4733 root->fs_info->fs_devices->missing_devices++;
4734 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004735 }
4736 }
4737
4738 if (device->fs_devices != root->fs_info->fs_devices) {
4739 BUG_ON(device->writeable);
4740 if (device->generation !=
4741 btrfs_device_generation(leaf, dev_item))
4742 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004743 }
Chris Mason0b86a832008-03-24 15:01:56 -04004744
4745 fill_device_from_item(leaf, dev_item, device);
4746 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004747 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01004748 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05004749 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004750 spin_lock(&root->fs_info->free_chunk_lock);
4751 root->fs_info->free_chunk_space += device->total_bytes -
4752 device->bytes_used;
4753 spin_unlock(&root->fs_info->free_chunk_lock);
4754 }
Chris Mason0b86a832008-03-24 15:01:56 -04004755 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004756 return ret;
4757}
4758
Yan Zhenge4404d62008-12-12 10:03:26 -05004759int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004760{
David Sterba6c417612011-04-13 15:41:04 +02004761 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004762 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004763 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004764 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004765 u8 *ptr;
4766 unsigned long sb_ptr;
4767 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004768 u32 num_stripes;
4769 u32 array_size;
4770 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004771 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004772 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004773
Yan Zhenge4404d62008-12-12 10:03:26 -05004774 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004775 BTRFS_SUPER_INFO_SIZE);
4776 if (!sb)
4777 return -ENOMEM;
4778 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004779 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004780 /*
4781 * The sb extent buffer is artifical and just used to read the system array.
4782 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4783 * pages up-to-date when the page is larger: extent does not cover the
4784 * whole page and consequently check_page_uptodate does not find all
4785 * the page's extents up-to-date (the hole beyond sb),
4786 * write_extent_buffer then triggers a WARN_ON.
4787 *
4788 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4789 * but sb spans only this function. Add an explicit SetPageUptodate call
4790 * to silence the warning eg. on PowerPC 64.
4791 */
4792 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004793 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004794
Chris Masona061fc82008-05-07 11:43:44 -04004795 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004796 array_size = btrfs_super_sys_array_size(super_copy);
4797
Chris Mason0b86a832008-03-24 15:01:56 -04004798 ptr = super_copy->sys_chunk_array;
4799 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4800 cur = 0;
4801
4802 while (cur < array_size) {
4803 disk_key = (struct btrfs_disk_key *)ptr;
4804 btrfs_disk_key_to_cpu(&key, disk_key);
4805
Chris Masona061fc82008-05-07 11:43:44 -04004806 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004807 sb_ptr += len;
4808 cur += len;
4809
Chris Mason0d81ba52008-03-24 15:02:07 -04004810 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004811 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004812 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004813 if (ret)
4814 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004815 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4816 len = btrfs_chunk_item_size(num_stripes);
4817 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004818 ret = -EIO;
4819 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004820 }
4821 ptr += len;
4822 sb_ptr += len;
4823 cur += len;
4824 }
Chris Masona061fc82008-05-07 11:43:44 -04004825 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004826 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004827}
4828
4829int btrfs_read_chunk_tree(struct btrfs_root *root)
4830{
4831 struct btrfs_path *path;
4832 struct extent_buffer *leaf;
4833 struct btrfs_key key;
4834 struct btrfs_key found_key;
4835 int ret;
4836 int slot;
4837
4838 root = root->fs_info->chunk_root;
4839
4840 path = btrfs_alloc_path();
4841 if (!path)
4842 return -ENOMEM;
4843
Li Zefanb367e472011-12-07 11:38:24 +08004844 mutex_lock(&uuid_mutex);
4845 lock_chunks(root);
4846
Chris Mason0b86a832008-03-24 15:01:56 -04004847 /* first we search for all of the device items, and then we
4848 * read in all of the chunk items. This way we can create chunk
4849 * mappings that reference all of the devices that are afound
4850 */
4851 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4852 key.offset = 0;
4853 key.type = 0;
4854again:
4855 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004856 if (ret < 0)
4857 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004858 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004859 leaf = path->nodes[0];
4860 slot = path->slots[0];
4861 if (slot >= btrfs_header_nritems(leaf)) {
4862 ret = btrfs_next_leaf(root, path);
4863 if (ret == 0)
4864 continue;
4865 if (ret < 0)
4866 goto error;
4867 break;
4868 }
4869 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4870 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4871 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4872 break;
4873 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4874 struct btrfs_dev_item *dev_item;
4875 dev_item = btrfs_item_ptr(leaf, slot,
4876 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004877 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004878 if (ret)
4879 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004880 }
4881 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4882 struct btrfs_chunk *chunk;
4883 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4884 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004885 if (ret)
4886 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004887 }
4888 path->slots[0]++;
4889 }
4890 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4891 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004892 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004893 goto again;
4894 }
Chris Mason0b86a832008-03-24 15:01:56 -04004895 ret = 0;
4896error:
Li Zefanb367e472011-12-07 11:38:24 +08004897 unlock_chunks(root);
4898 mutex_unlock(&uuid_mutex);
4899
Yan Zheng2b820322008-11-17 21:11:30 -05004900 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004901 return ret;
4902}
Stefan Behrens442a4f62012-05-25 16:06:08 +02004903
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004904static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
4905{
4906 int i;
4907
4908 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4909 btrfs_dev_stat_reset(dev, i);
4910}
4911
4912int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
4913{
4914 struct btrfs_key key;
4915 struct btrfs_key found_key;
4916 struct btrfs_root *dev_root = fs_info->dev_root;
4917 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4918 struct extent_buffer *eb;
4919 int slot;
4920 int ret = 0;
4921 struct btrfs_device *device;
4922 struct btrfs_path *path = NULL;
4923 int i;
4924
4925 path = btrfs_alloc_path();
4926 if (!path) {
4927 ret = -ENOMEM;
4928 goto out;
4929 }
4930
4931 mutex_lock(&fs_devices->device_list_mutex);
4932 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4933 int item_size;
4934 struct btrfs_dev_stats_item *ptr;
4935
4936 key.objectid = 0;
4937 key.type = BTRFS_DEV_STATS_KEY;
4938 key.offset = device->devid;
4939 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
4940 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004941 __btrfs_reset_dev_stats(device);
4942 device->dev_stats_valid = 1;
4943 btrfs_release_path(path);
4944 continue;
4945 }
4946 slot = path->slots[0];
4947 eb = path->nodes[0];
4948 btrfs_item_key_to_cpu(eb, &found_key, slot);
4949 item_size = btrfs_item_size_nr(eb, slot);
4950
4951 ptr = btrfs_item_ptr(eb, slot,
4952 struct btrfs_dev_stats_item);
4953
4954 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4955 if (item_size >= (1 + i) * sizeof(__le64))
4956 btrfs_dev_stat_set(device, i,
4957 btrfs_dev_stats_value(eb, ptr, i));
4958 else
4959 btrfs_dev_stat_reset(device, i);
4960 }
4961
4962 device->dev_stats_valid = 1;
4963 btrfs_dev_stat_print_on_load(device);
4964 btrfs_release_path(path);
4965 }
4966 mutex_unlock(&fs_devices->device_list_mutex);
4967
4968out:
4969 btrfs_free_path(path);
4970 return ret < 0 ? ret : 0;
4971}
4972
4973static int update_dev_stat_item(struct btrfs_trans_handle *trans,
4974 struct btrfs_root *dev_root,
4975 struct btrfs_device *device)
4976{
4977 struct btrfs_path *path;
4978 struct btrfs_key key;
4979 struct extent_buffer *eb;
4980 struct btrfs_dev_stats_item *ptr;
4981 int ret;
4982 int i;
4983
4984 key.objectid = 0;
4985 key.type = BTRFS_DEV_STATS_KEY;
4986 key.offset = device->devid;
4987
4988 path = btrfs_alloc_path();
4989 BUG_ON(!path);
4990 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
4991 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004992 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
4993 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004994 goto out;
4995 }
4996
4997 if (ret == 0 &&
4998 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
4999 /* need to delete old one and insert a new one */
5000 ret = btrfs_del_item(trans, dev_root, path);
5001 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005002 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5003 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005004 goto out;
5005 }
5006 ret = 1;
5007 }
5008
5009 if (ret == 1) {
5010 /* need to insert a new item */
5011 btrfs_release_path(path);
5012 ret = btrfs_insert_empty_item(trans, dev_root, path,
5013 &key, sizeof(*ptr));
5014 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005015 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5016 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005017 goto out;
5018 }
5019 }
5020
5021 eb = path->nodes[0];
5022 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5023 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5024 btrfs_set_dev_stats_value(eb, ptr, i,
5025 btrfs_dev_stat_read(device, i));
5026 btrfs_mark_buffer_dirty(eb);
5027
5028out:
5029 btrfs_free_path(path);
5030 return ret;
5031}
5032
5033/*
5034 * called from commit_transaction. Writes all changed device stats to disk.
5035 */
5036int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5037 struct btrfs_fs_info *fs_info)
5038{
5039 struct btrfs_root *dev_root = fs_info->dev_root;
5040 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5041 struct btrfs_device *device;
5042 int ret = 0;
5043
5044 mutex_lock(&fs_devices->device_list_mutex);
5045 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5046 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5047 continue;
5048
5049 ret = update_dev_stat_item(trans, dev_root, device);
5050 if (!ret)
5051 device->dev_stats_dirty = 0;
5052 }
5053 mutex_unlock(&fs_devices->device_list_mutex);
5054
5055 return ret;
5056}
5057
Stefan Behrens442a4f62012-05-25 16:06:08 +02005058void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5059{
5060 btrfs_dev_stat_inc(dev, index);
5061 btrfs_dev_stat_print_on_error(dev);
5062}
5063
5064void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5065{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005066 if (!dev->dev_stats_valid)
5067 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005068 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005069 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005070 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005071 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5072 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5073 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5074 btrfs_dev_stat_read(dev,
5075 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5076 btrfs_dev_stat_read(dev,
5077 BTRFS_DEV_STAT_GENERATION_ERRS));
5078}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005079
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005080static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5081{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005082 int i;
5083
5084 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5085 if (btrfs_dev_stat_read(dev, i) != 0)
5086 break;
5087 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5088 return; /* all values == 0, suppress message */
5089
Josef Bacik606686e2012-06-04 14:03:51 -04005090 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5091 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005092 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5093 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5094 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5095 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5096 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5097}
5098
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005099int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005100 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005101{
5102 struct btrfs_device *dev;
5103 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5104 int i;
5105
5106 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005107 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005108 mutex_unlock(&fs_devices->device_list_mutex);
5109
5110 if (!dev) {
5111 printk(KERN_WARNING
5112 "btrfs: get dev_stats failed, device not found\n");
5113 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005114 } else if (!dev->dev_stats_valid) {
5115 printk(KERN_WARNING
5116 "btrfs: get dev_stats failed, not yet valid\n");
5117 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005118 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005119 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5120 if (stats->nr_items > i)
5121 stats->values[i] =
5122 btrfs_dev_stat_read_and_reset(dev, i);
5123 else
5124 btrfs_dev_stat_reset(dev, i);
5125 }
5126 } else {
5127 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5128 if (stats->nr_items > i)
5129 stats->values[i] = btrfs_dev_stat_read(dev, i);
5130 }
5131 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5132 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5133 return 0;
5134}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005135
5136int btrfs_scratch_superblock(struct btrfs_device *device)
5137{
5138 struct buffer_head *bh;
5139 struct btrfs_super_block *disk_super;
5140
5141 bh = btrfs_read_dev_super(device->bdev);
5142 if (!bh)
5143 return -EINVAL;
5144 disk_super = (struct btrfs_super_block *)bh->b_data;
5145
5146 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5147 set_buffer_dirty(bh);
5148 sync_dirty_buffer(bh);
5149 brelse(bh);
5150
5151 return 0;
5152}