blob: 33ca36b37a6a9adde02869c233549fe6812f14a4 [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) {
521 if (!latest_transid ||
522 device->generation > latest_transid) {
523 latest_devid = device->devid;
524 latest_transid = device->generation;
525 latest_bdev = device->bdev;
526 }
Yan Zheng2b820322008-11-17 21:11:30 -0500527 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500528 }
Yan Zheng2b820322008-11-17 21:11:30 -0500529
530 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100531 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500532 device->bdev = NULL;
533 fs_devices->open_devices--;
534 }
535 if (device->writeable) {
536 list_del_init(&device->dev_alloc_list);
537 device->writeable = 0;
538 fs_devices->rw_devices--;
539 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500540 list_del_init(&device->dev_list);
541 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400542 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500543 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400544 }
Yan Zheng2b820322008-11-17 21:11:30 -0500545
546 if (fs_devices->seed) {
547 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500548 goto again;
549 }
550
Chris Masona6b0d5c2012-02-20 20:53:43 -0500551 fs_devices->latest_bdev = latest_bdev;
552 fs_devices->latest_devid = latest_devid;
553 fs_devices->latest_trans = latest_transid;
554
Chris Masondfe25022008-05-13 13:46:40 -0400555 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400556}
Chris Masona0af4692008-05-13 16:03:06 -0400557
Xiao Guangrong1f781602011-04-20 10:09:16 +0000558static void __free_device(struct work_struct *work)
559{
560 struct btrfs_device *device;
561
562 device = container_of(work, struct btrfs_device, rcu_work);
563
564 if (device->bdev)
565 blkdev_put(device->bdev, device->mode);
566
Josef Bacik606686e2012-06-04 14:03:51 -0400567 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000568 kfree(device);
569}
570
571static void free_device(struct rcu_head *head)
572{
573 struct btrfs_device *device;
574
575 device = container_of(head, struct btrfs_device, rcu);
576
577 INIT_WORK(&device->rcu_work, __free_device);
578 schedule_work(&device->rcu_work);
579}
580
Yan Zheng2b820322008-11-17 21:11:30 -0500581static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400582{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400583 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500584
Yan Zheng2b820322008-11-17 21:11:30 -0500585 if (--fs_devices->opened > 0)
586 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400587
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000588 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500589 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000590 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400591 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000592
593 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400594 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000595
Yan Zheng2b820322008-11-17 21:11:30 -0500596 if (device->writeable) {
597 list_del_init(&device->dev_alloc_list);
598 fs_devices->rw_devices--;
599 }
600
Josef Bacikd5e20032011-08-04 14:52:27 +0000601 if (device->can_discard)
602 fs_devices->num_can_discard--;
603
Xiao Guangrong1f781602011-04-20 10:09:16 +0000604 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100605 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000606 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400607
608 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400609 if (device->name) {
610 name = rcu_string_strdup(device->name->str, GFP_NOFS);
611 BUG_ON(device->name && !name); /* -ENOMEM */
612 rcu_assign_pointer(new_device->name, name);
613 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000614 new_device->bdev = NULL;
615 new_device->writeable = 0;
616 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000617 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000618 list_replace_rcu(&device->dev_list, &new_device->dev_list);
619
620 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000622 mutex_unlock(&fs_devices->device_list_mutex);
623
Yan Zhenge4404d62008-12-12 10:03:26 -0500624 WARN_ON(fs_devices->open_devices);
625 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500626 fs_devices->opened = 0;
627 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500628
Chris Mason8a4b83c2008-03-24 15:02:07 -0400629 return 0;
630}
631
Yan Zheng2b820322008-11-17 21:11:30 -0500632int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
633{
Yan Zhenge4404d62008-12-12 10:03:26 -0500634 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500635 int ret;
636
637 mutex_lock(&uuid_mutex);
638 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500639 if (!fs_devices->opened) {
640 seed_devices = fs_devices->seed;
641 fs_devices->seed = NULL;
642 }
Yan Zheng2b820322008-11-17 21:11:30 -0500643 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500644
645 while (seed_devices) {
646 fs_devices = seed_devices;
647 seed_devices = fs_devices->seed;
648 __btrfs_close_devices(fs_devices);
649 free_fs_devices(fs_devices);
650 }
Yan Zheng2b820322008-11-17 21:11:30 -0500651 return ret;
652}
653
Yan Zhenge4404d62008-12-12 10:03:26 -0500654static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
655 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400656{
Josef Bacikd5e20032011-08-04 14:52:27 +0000657 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400658 struct block_device *bdev;
659 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400660 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400661 struct block_device *latest_bdev = NULL;
662 struct buffer_head *bh;
663 struct btrfs_super_block *disk_super;
664 u64 latest_devid = 0;
665 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400666 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500667 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400668 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669
Tejun Heod4d77622010-11-13 11:55:18 +0100670 flags |= FMODE_EXCL;
671
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500672 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400673 if (device->bdev)
674 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400675 if (!device->name)
676 continue;
677
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100678 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
679 &bdev, &bh);
680 if (ret)
681 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400682
683 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000684 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400685 if (devid != device->devid)
686 goto error_brelse;
687
Yan Zheng2b820322008-11-17 21:11:30 -0500688 if (memcmp(device->uuid, disk_super->dev_item.uuid,
689 BTRFS_UUID_SIZE))
690 goto error_brelse;
691
692 device->generation = btrfs_super_generation(disk_super);
693 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400694 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500695 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400696 latest_bdev = bdev;
697 }
698
Yan Zheng2b820322008-11-17 21:11:30 -0500699 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
700 device->writeable = 0;
701 } else {
702 device->writeable = !bdev_read_only(bdev);
703 seeding = 0;
704 }
705
Josef Bacikd5e20032011-08-04 14:52:27 +0000706 q = bdev_get_queue(bdev);
707 if (blk_queue_discard(q)) {
708 device->can_discard = 1;
709 fs_devices->num_can_discard++;
710 }
711
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400713 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500714 device->mode = flags;
715
Chris Masonc2898112009-06-10 09:51:32 -0400716 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
717 fs_devices->rotating = 1;
718
Chris Masona0af4692008-05-13 16:03:06 -0400719 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500720 if (device->writeable) {
721 fs_devices->rw_devices++;
722 list_add(&device->dev_alloc_list,
723 &fs_devices->alloc_list);
724 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000725 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400726 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400727
Chris Masona0af4692008-05-13 16:03:06 -0400728error_brelse:
729 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100730 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400731 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 }
Chris Masona0af4692008-05-13 16:03:06 -0400733 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300734 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400735 goto out;
736 }
Yan Zheng2b820322008-11-17 21:11:30 -0500737 fs_devices->seeding = seeding;
738 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400739 fs_devices->latest_bdev = latest_bdev;
740 fs_devices->latest_devid = latest_devid;
741 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500742 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400743out:
Yan Zheng2b820322008-11-17 21:11:30 -0500744 return ret;
745}
746
747int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500748 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500749{
750 int ret;
751
752 mutex_lock(&uuid_mutex);
753 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500754 fs_devices->opened++;
755 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500756 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500757 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500758 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400759 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 return ret;
761}
762
Christoph Hellwig97288f22008-12-02 06:36:09 -0500763int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400764 struct btrfs_fs_devices **fs_devices_ret)
765{
766 struct btrfs_super_block *disk_super;
767 struct block_device *bdev;
768 struct buffer_head *bh;
769 int ret;
770 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400771 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400772 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400773
Tejun Heod4d77622010-11-13 11:55:18 +0100774 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500775 mutex_lock(&uuid_mutex);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100776 ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400777 if (ret)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100778 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400779 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000780 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400781 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400782 total_devices = btrfs_super_num_devices(disk_super);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000783 if (disk_super->label[0]) {
784 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
785 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500786 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000787 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200788 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000789 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500790 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500791 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400792 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400793 if (!ret && fs_devices_ret)
794 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100796 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400797error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100798 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400799 return ret;
800}
Chris Mason0b86a832008-03-24 15:01:56 -0400801
Miao Xie6d07bce2011-01-05 10:07:31 +0000802/* helper to account the used device space in the range */
803int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
804 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400805{
806 struct btrfs_key key;
807 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000808 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500809 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000810 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400811 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000812 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400813 struct extent_buffer *l;
814
Miao Xie6d07bce2011-01-05 10:07:31 +0000815 *length = 0;
816
817 if (start >= device->total_bytes)
818 return 0;
819
Yan Zheng2b820322008-11-17 21:11:30 -0500820 path = btrfs_alloc_path();
821 if (!path)
822 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400823 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400824
Chris Mason0b86a832008-03-24 15:01:56 -0400825 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000826 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400827 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000828
829 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400830 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000831 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400832 if (ret > 0) {
833 ret = btrfs_previous_item(root, path, key.objectid, key.type);
834 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000835 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400836 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000837
Chris Mason0b86a832008-03-24 15:01:56 -0400838 while (1) {
839 l = path->nodes[0];
840 slot = path->slots[0];
841 if (slot >= btrfs_header_nritems(l)) {
842 ret = btrfs_next_leaf(root, path);
843 if (ret == 0)
844 continue;
845 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000846 goto out;
847
848 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400849 }
850 btrfs_item_key_to_cpu(l, &key, slot);
851
852 if (key.objectid < device->devid)
853 goto next;
854
855 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000856 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400857
Chris Masond3977122009-01-05 21:25:51 -0500858 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400859 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400860
Chris Mason0b86a832008-03-24 15:01:56 -0400861 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000862 extent_end = key.offset + btrfs_dev_extent_length(l,
863 dev_extent);
864 if (key.offset <= start && extent_end > end) {
865 *length = end - start + 1;
866 break;
867 } else if (key.offset <= start && extent_end > start)
868 *length += extent_end - start;
869 else if (key.offset > start && extent_end <= end)
870 *length += extent_end - key.offset;
871 else if (key.offset > start && key.offset <= end) {
872 *length += end - key.offset + 1;
873 break;
874 } else if (key.offset > end)
875 break;
876
877next:
878 path->slots[0]++;
879 }
880 ret = 0;
881out:
882 btrfs_free_path(path);
883 return ret;
884}
885
Chris Mason0b86a832008-03-24 15:01:56 -0400886/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000887 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000888 * @device: the device which we search the free space in
889 * @num_bytes: the size of the free space that we need
890 * @start: store the start of the free space.
891 * @len: the size of the free space. that we find, or the size of the max
892 * free space if we don't find suitable free space
893 *
Chris Mason0b86a832008-03-24 15:01:56 -0400894 * this uses a pretty simple search, the expectation is that it is
895 * called very infrequently and that a given device has a small number
896 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000897 *
898 * @start is used to store the start of the free space if we find. But if we
899 * don't find suitable free space, it will be used to store the start position
900 * of the max free space.
901 *
902 * @len is used to store the size of the free space that we find.
903 * But if we don't find suitable free space, it is used to store the size of
904 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400905 */
Li Zefan125ccb02011-12-08 15:07:24 +0800906int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000907 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400908{
909 struct btrfs_key key;
910 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400912 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000913 u64 hole_size;
914 u64 max_hole_start;
915 u64 max_hole_size;
916 u64 extent_end;
917 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400918 u64 search_end = device->total_bytes;
919 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000920 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400921 struct extent_buffer *l;
922
Chris Mason0b86a832008-03-24 15:01:56 -0400923 /* FIXME use last free of some kind */
924
925 /* we don't want to overwrite the superblock on the drive,
926 * so we make sure to start at an offset of at least 1MB
927 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200928 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400929
Miao Xie7bfc8372011-01-05 10:07:26 +0000930 max_hole_start = search_start;
931 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000932 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000933
934 if (search_start >= search_end) {
935 ret = -ENOSPC;
936 goto error;
937 }
938
939 path = btrfs_alloc_path();
940 if (!path) {
941 ret = -ENOMEM;
942 goto error;
943 }
944 path->reada = 2;
945
Chris Mason0b86a832008-03-24 15:01:56 -0400946 key.objectid = device->devid;
947 key.offset = search_start;
948 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000949
Li Zefan125ccb02011-12-08 15:07:24 +0800950 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400951 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000952 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400953 if (ret > 0) {
954 ret = btrfs_previous_item(root, path, key.objectid, key.type);
955 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000956 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400957 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000958
Chris Mason0b86a832008-03-24 15:01:56 -0400959 while (1) {
960 l = path->nodes[0];
961 slot = path->slots[0];
962 if (slot >= btrfs_header_nritems(l)) {
963 ret = btrfs_next_leaf(root, path);
964 if (ret == 0)
965 continue;
966 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000967 goto out;
968
969 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400970 }
971 btrfs_item_key_to_cpu(l, &key, slot);
972
973 if (key.objectid < device->devid)
974 goto next;
975
976 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000977 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400978
Chris Mason0b86a832008-03-24 15:01:56 -0400979 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
980 goto next;
981
Miao Xie7bfc8372011-01-05 10:07:26 +0000982 if (key.offset > search_start) {
983 hole_size = key.offset - search_start;
984
985 if (hole_size > max_hole_size) {
986 max_hole_start = search_start;
987 max_hole_size = hole_size;
988 }
989
990 /*
991 * If this free space is greater than which we need,
992 * it must be the max free space that we have found
993 * until now, so max_hole_start must point to the start
994 * of this free space and the length of this free space
995 * is stored in max_hole_size. Thus, we return
996 * max_hole_start and max_hole_size and go back to the
997 * caller.
998 */
999 if (hole_size >= num_bytes) {
1000 ret = 0;
1001 goto out;
1002 }
1003 }
1004
Chris Mason0b86a832008-03-24 15:01:56 -04001005 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001006 extent_end = key.offset + btrfs_dev_extent_length(l,
1007 dev_extent);
1008 if (extent_end > search_start)
1009 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001010next:
1011 path->slots[0]++;
1012 cond_resched();
1013 }
Chris Mason0b86a832008-03-24 15:01:56 -04001014
liubo38c01b92011-08-02 02:39:03 +00001015 /*
1016 * At this point, search_start should be the end of
1017 * allocated dev extents, and when shrinking the device,
1018 * search_end may be smaller than search_start.
1019 */
1020 if (search_end > search_start)
1021 hole_size = search_end - search_start;
1022
Miao Xie7bfc8372011-01-05 10:07:26 +00001023 if (hole_size > max_hole_size) {
1024 max_hole_start = search_start;
1025 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001026 }
Chris Mason0b86a832008-03-24 15:01:56 -04001027
Miao Xie7bfc8372011-01-05 10:07:26 +00001028 /* See above. */
1029 if (hole_size < num_bytes)
1030 ret = -ENOSPC;
1031 else
1032 ret = 0;
1033
1034out:
Yan Zheng2b820322008-11-17 21:11:30 -05001035 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001036error:
1037 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001038 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001039 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001040 return ret;
1041}
1042
Christoph Hellwigb2950862008-12-02 09:54:17 -05001043static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001044 struct btrfs_device *device,
1045 u64 start)
1046{
1047 int ret;
1048 struct btrfs_path *path;
1049 struct btrfs_root *root = device->dev_root;
1050 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001051 struct btrfs_key found_key;
1052 struct extent_buffer *leaf = NULL;
1053 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001054
1055 path = btrfs_alloc_path();
1056 if (!path)
1057 return -ENOMEM;
1058
1059 key.objectid = device->devid;
1060 key.offset = start;
1061 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001062again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001063 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001064 if (ret > 0) {
1065 ret = btrfs_previous_item(root, path, key.objectid,
1066 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001067 if (ret)
1068 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001069 leaf = path->nodes[0];
1070 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1071 extent = btrfs_item_ptr(leaf, path->slots[0],
1072 struct btrfs_dev_extent);
1073 BUG_ON(found_key.offset > start || found_key.offset +
1074 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001075 key = found_key;
1076 btrfs_release_path(path);
1077 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001078 } else if (ret == 0) {
1079 leaf = path->nodes[0];
1080 extent = btrfs_item_ptr(leaf, path->slots[0],
1081 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001082 } else {
1083 btrfs_error(root->fs_info, ret, "Slot search failed");
1084 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001085 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001086
Josef Bacik2bf64752011-09-26 17:12:22 -04001087 if (device->bytes_used > 0) {
1088 u64 len = btrfs_dev_extent_length(leaf, extent);
1089 device->bytes_used -= len;
1090 spin_lock(&root->fs_info->free_chunk_lock);
1091 root->fs_info->free_chunk_space += len;
1092 spin_unlock(&root->fs_info->free_chunk_lock);
1093 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001094 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095 if (ret) {
1096 btrfs_error(root->fs_info, ret,
1097 "Failed to remove dev extent item");
1098 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001099out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001100 btrfs_free_path(path);
1101 return ret;
1102}
1103
Yan Zheng2b820322008-11-17 21:11:30 -05001104int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001105 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001106 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001107 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001108{
1109 int ret;
1110 struct btrfs_path *path;
1111 struct btrfs_root *root = device->dev_root;
1112 struct btrfs_dev_extent *extent;
1113 struct extent_buffer *leaf;
1114 struct btrfs_key key;
1115
Chris Masondfe25022008-05-13 13:46:40 -04001116 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001117 path = btrfs_alloc_path();
1118 if (!path)
1119 return -ENOMEM;
1120
Chris Mason0b86a832008-03-24 15:01:56 -04001121 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001122 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001123 key.type = BTRFS_DEV_EXTENT_KEY;
1124 ret = btrfs_insert_empty_item(trans, root, path, &key,
1125 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001126 if (ret)
1127 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001128
1129 leaf = path->nodes[0];
1130 extent = btrfs_item_ptr(leaf, path->slots[0],
1131 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001132 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1133 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1134 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1135
1136 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1137 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1138 BTRFS_UUID_SIZE);
1139
Chris Mason0b86a832008-03-24 15:01:56 -04001140 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1141 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001142out:
Chris Mason0b86a832008-03-24 15:01:56 -04001143 btrfs_free_path(path);
1144 return ret;
1145}
1146
Chris Masona1b32a52008-09-05 16:09:51 -04001147static noinline int find_next_chunk(struct btrfs_root *root,
1148 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001149{
1150 struct btrfs_path *path;
1151 int ret;
1152 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001153 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001154 struct btrfs_key found_key;
1155
1156 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001157 if (!path)
1158 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001159
Chris Masone17cade2008-04-15 15:41:47 -04001160 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001161 key.offset = (u64)-1;
1162 key.type = BTRFS_CHUNK_ITEM_KEY;
1163
1164 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1165 if (ret < 0)
1166 goto error;
1167
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001168 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001169
1170 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1171 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001172 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001173 } else {
1174 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1175 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001176 if (found_key.objectid != objectid)
1177 *offset = 0;
1178 else {
1179 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1180 struct btrfs_chunk);
1181 *offset = found_key.offset +
1182 btrfs_chunk_length(path->nodes[0], chunk);
1183 }
Chris Mason0b86a832008-03-24 15:01:56 -04001184 }
1185 ret = 0;
1186error:
1187 btrfs_free_path(path);
1188 return ret;
1189}
1190
Yan Zheng2b820322008-11-17 21:11:30 -05001191static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001192{
1193 int ret;
1194 struct btrfs_key key;
1195 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001196 struct btrfs_path *path;
1197
1198 root = root->fs_info->chunk_root;
1199
1200 path = btrfs_alloc_path();
1201 if (!path)
1202 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001203
1204 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1205 key.type = BTRFS_DEV_ITEM_KEY;
1206 key.offset = (u64)-1;
1207
1208 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1209 if (ret < 0)
1210 goto error;
1211
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001213
1214 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1215 BTRFS_DEV_ITEM_KEY);
1216 if (ret) {
1217 *objectid = 1;
1218 } else {
1219 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1220 path->slots[0]);
1221 *objectid = found_key.offset + 1;
1222 }
1223 ret = 0;
1224error:
Yan Zheng2b820322008-11-17 21:11:30 -05001225 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001226 return ret;
1227}
1228
1229/*
1230 * the device information is stored in the chunk root
1231 * the btrfs_device struct should be fully filled in
1232 */
1233int btrfs_add_device(struct btrfs_trans_handle *trans,
1234 struct btrfs_root *root,
1235 struct btrfs_device *device)
1236{
1237 int ret;
1238 struct btrfs_path *path;
1239 struct btrfs_dev_item *dev_item;
1240 struct extent_buffer *leaf;
1241 struct btrfs_key key;
1242 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001243
1244 root = root->fs_info->chunk_root;
1245
1246 path = btrfs_alloc_path();
1247 if (!path)
1248 return -ENOMEM;
1249
Chris Mason0b86a832008-03-24 15:01:56 -04001250 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1251 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001252 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001253
1254 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001255 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001256 if (ret)
1257 goto out;
1258
1259 leaf = path->nodes[0];
1260 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1261
1262 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001263 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001264 btrfs_set_device_type(leaf, dev_item, device->type);
1265 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1266 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1267 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001268 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1269 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001270 btrfs_set_device_group(leaf, dev_item, 0);
1271 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1272 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001273 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001274
Chris Mason0b86a832008-03-24 15:01:56 -04001275 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001276 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001277 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1278 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001279 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001280
Yan Zheng2b820322008-11-17 21:11:30 -05001281 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001282out:
1283 btrfs_free_path(path);
1284 return ret;
1285}
Chris Mason8f18cf12008-04-25 16:53:30 -04001286
Chris Masona061fc82008-05-07 11:43:44 -04001287static int btrfs_rm_dev_item(struct btrfs_root *root,
1288 struct btrfs_device *device)
1289{
1290 int ret;
1291 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001292 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001293 struct btrfs_trans_handle *trans;
1294
1295 root = root->fs_info->chunk_root;
1296
1297 path = btrfs_alloc_path();
1298 if (!path)
1299 return -ENOMEM;
1300
Yan, Zhenga22285a2010-05-16 10:48:46 -04001301 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001302 if (IS_ERR(trans)) {
1303 btrfs_free_path(path);
1304 return PTR_ERR(trans);
1305 }
Chris Masona061fc82008-05-07 11:43:44 -04001306 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1307 key.type = BTRFS_DEV_ITEM_KEY;
1308 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001309 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001310
1311 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1312 if (ret < 0)
1313 goto out;
1314
1315 if (ret > 0) {
1316 ret = -ENOENT;
1317 goto out;
1318 }
1319
1320 ret = btrfs_del_item(trans, root, path);
1321 if (ret)
1322 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001323out:
1324 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001325 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001326 btrfs_commit_transaction(trans, root);
1327 return ret;
1328}
1329
1330int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1331{
1332 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001333 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001334 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001335 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001336 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001337 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001338 u64 all_avail;
1339 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001340 u64 num_devices;
1341 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001342 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001343 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001344
Chris Masona061fc82008-05-07 11:43:44 -04001345 mutex_lock(&uuid_mutex);
1346
1347 all_avail = root->fs_info->avail_data_alloc_bits |
1348 root->fs_info->avail_system_alloc_bits |
1349 root->fs_info->avail_metadata_alloc_bits;
1350
1351 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001352 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001353 printk(KERN_ERR "btrfs: unable to go below four devices "
1354 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001355 ret = -EINVAL;
1356 goto out;
1357 }
1358
1359 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001360 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001361 printk(KERN_ERR "btrfs: unable to go below two "
1362 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001363 ret = -EINVAL;
1364 goto out;
1365 }
1366
Chris Masondfe25022008-05-13 13:46:40 -04001367 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001368 struct list_head *devices;
1369 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001370
Chris Masondfe25022008-05-13 13:46:40 -04001371 device = NULL;
1372 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001373 /*
1374 * It is safe to read the devices since the volume_mutex
1375 * is held.
1376 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001377 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001378 if (tmp->in_fs_metadata && !tmp->bdev) {
1379 device = tmp;
1380 break;
1381 }
1382 }
1383 bdev = NULL;
1384 bh = NULL;
1385 disk_super = NULL;
1386 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001387 printk(KERN_ERR "btrfs: no missing devices found to "
1388 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001389 goto out;
1390 }
Chris Masondfe25022008-05-13 13:46:40 -04001391 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001392 ret = btrfs_get_bdev_and_sb(device_path,
1393 FMODE_READ | FMODE_EXCL,
1394 root->fs_info->bdev_holder, 0,
1395 &bdev, &bh);
1396 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001397 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001398 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001399 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001400 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001401 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001402 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001403 if (!device) {
1404 ret = -ENOENT;
1405 goto error_brelse;
1406 }
Chris Masondfe25022008-05-13 13:46:40 -04001407 }
Yan Zheng2b820322008-11-17 21:11:30 -05001408
1409 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001410 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1411 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001412 ret = -EINVAL;
1413 goto error_brelse;
1414 }
1415
1416 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001417 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001418 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001419 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001420 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001421 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001422 }
Chris Masona061fc82008-05-07 11:43:44 -04001423
1424 ret = btrfs_shrink_device(device, 0);
1425 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001426 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001427
Chris Masona061fc82008-05-07 11:43:44 -04001428 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1429 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001430 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001431
Josef Bacik2bf64752011-09-26 17:12:22 -04001432 spin_lock(&root->fs_info->free_chunk_lock);
1433 root->fs_info->free_chunk_space = device->total_bytes -
1434 device->bytes_used;
1435 spin_unlock(&root->fs_info->free_chunk_lock);
1436
Yan Zheng2b820322008-11-17 21:11:30 -05001437 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001438 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001439
1440 /*
1441 * the device list mutex makes sure that we don't change
1442 * the device list while someone else is writing out all
1443 * the device supers.
1444 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001445
1446 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001447 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001448 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001449
Yan Zhenge4404d62008-12-12 10:03:26 -05001450 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001451 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001452
Chris Masoncd02dca2010-12-13 14:56:23 -05001453 if (device->missing)
1454 root->fs_info->fs_devices->missing_devices--;
1455
Yan Zheng2b820322008-11-17 21:11:30 -05001456 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1457 struct btrfs_device, dev_list);
1458 if (device->bdev == root->fs_info->sb->s_bdev)
1459 root->fs_info->sb->s_bdev = next_device->bdev;
1460 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1461 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1462
Xiao Guangrong1f781602011-04-20 10:09:16 +00001463 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001464 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001465
1466 call_rcu(&device->rcu, free_device);
1467 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001468
David Sterba6c417612011-04-13 15:41:04 +02001469 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1470 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001471
Xiao Guangrong1f781602011-04-20 10:09:16 +00001472 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001473 struct btrfs_fs_devices *fs_devices;
1474 fs_devices = root->fs_info->fs_devices;
1475 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001476 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001477 break;
1478 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001479 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001480 fs_devices->seed = cur_devices->seed;
1481 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001482 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001483 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001484 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001485 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001486 }
1487
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001488 root->fs_info->num_tolerated_disk_barrier_failures =
1489 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1490
Yan Zheng2b820322008-11-17 21:11:30 -05001491 /*
1492 * at this point, the device is zero sized. We want to
1493 * remove it from the devices list and zero out the old super
1494 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001495 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001496 /* make sure this device isn't detected as part of
1497 * the FS anymore
1498 */
1499 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1500 set_buffer_dirty(bh);
1501 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001502 }
Chris Masona061fc82008-05-07 11:43:44 -04001503
Chris Masona061fc82008-05-07 11:43:44 -04001504 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001505
1506error_brelse:
1507 brelse(bh);
1508error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001509 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001510 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001511out:
1512 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001513 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001514error_undo:
1515 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001516 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001517 list_add(&device->dev_alloc_list,
1518 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001519 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001520 root->fs_info->fs_devices->rw_devices++;
1521 }
1522 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001523}
1524
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001525int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1526 struct btrfs_device **device)
1527{
1528 int ret = 0;
1529 struct btrfs_super_block *disk_super;
1530 u64 devid;
1531 u8 *dev_uuid;
1532 struct block_device *bdev;
1533 struct buffer_head *bh;
1534
1535 *device = NULL;
1536 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1537 root->fs_info->bdev_holder, 0, &bdev, &bh);
1538 if (ret)
1539 return ret;
1540 disk_super = (struct btrfs_super_block *)bh->b_data;
1541 devid = btrfs_stack_device_id(&disk_super->dev_item);
1542 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001543 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001544 disk_super->fsid);
1545 brelse(bh);
1546 if (!*device)
1547 ret = -ENOENT;
1548 blkdev_put(bdev, FMODE_READ);
1549 return ret;
1550}
1551
1552int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1553 char *device_path,
1554 struct btrfs_device **device)
1555{
1556 *device = NULL;
1557 if (strcmp(device_path, "missing") == 0) {
1558 struct list_head *devices;
1559 struct btrfs_device *tmp;
1560
1561 devices = &root->fs_info->fs_devices->devices;
1562 /*
1563 * It is safe to read the devices since the volume_mutex
1564 * is held by the caller.
1565 */
1566 list_for_each_entry(tmp, devices, dev_list) {
1567 if (tmp->in_fs_metadata && !tmp->bdev) {
1568 *device = tmp;
1569 break;
1570 }
1571 }
1572
1573 if (!*device) {
1574 pr_err("btrfs: no missing device found\n");
1575 return -ENOENT;
1576 }
1577
1578 return 0;
1579 } else {
1580 return btrfs_find_device_by_path(root, device_path, device);
1581 }
1582}
1583
Yan Zheng2b820322008-11-17 21:11:30 -05001584/*
1585 * does all the dirty work required for changing file system's UUID.
1586 */
Li Zefan125ccb02011-12-08 15:07:24 +08001587static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001588{
1589 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1590 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001591 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001592 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001593 struct btrfs_device *device;
1594 u64 super_flags;
1595
1596 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001597 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001598 return -EINVAL;
1599
Yan Zhenge4404d62008-12-12 10:03:26 -05001600 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1601 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001602 return -ENOMEM;
1603
Yan Zhenge4404d62008-12-12 10:03:26 -05001604 old_devices = clone_fs_devices(fs_devices);
1605 if (IS_ERR(old_devices)) {
1606 kfree(seed_devices);
1607 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001608 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001609
Yan Zheng2b820322008-11-17 21:11:30 -05001610 list_add(&old_devices->list, &fs_uuids);
1611
Yan Zhenge4404d62008-12-12 10:03:26 -05001612 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1613 seed_devices->opened = 1;
1614 INIT_LIST_HEAD(&seed_devices->devices);
1615 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001616 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001617
1618 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001619 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1620 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001621 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1622
Yan Zhenge4404d62008-12-12 10:03:26 -05001623 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1624 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1625 device->fs_devices = seed_devices;
1626 }
1627
Yan Zheng2b820322008-11-17 21:11:30 -05001628 fs_devices->seeding = 0;
1629 fs_devices->num_devices = 0;
1630 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001631 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001632 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001633
1634 generate_random_uuid(fs_devices->fsid);
1635 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1636 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1637 super_flags = btrfs_super_flags(disk_super) &
1638 ~BTRFS_SUPER_FLAG_SEEDING;
1639 btrfs_set_super_flags(disk_super, super_flags);
1640
1641 return 0;
1642}
1643
1644/*
1645 * strore the expected generation for seed devices in device items.
1646 */
1647static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1648 struct btrfs_root *root)
1649{
1650 struct btrfs_path *path;
1651 struct extent_buffer *leaf;
1652 struct btrfs_dev_item *dev_item;
1653 struct btrfs_device *device;
1654 struct btrfs_key key;
1655 u8 fs_uuid[BTRFS_UUID_SIZE];
1656 u8 dev_uuid[BTRFS_UUID_SIZE];
1657 u64 devid;
1658 int ret;
1659
1660 path = btrfs_alloc_path();
1661 if (!path)
1662 return -ENOMEM;
1663
1664 root = root->fs_info->chunk_root;
1665 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1666 key.offset = 0;
1667 key.type = BTRFS_DEV_ITEM_KEY;
1668
1669 while (1) {
1670 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1671 if (ret < 0)
1672 goto error;
1673
1674 leaf = path->nodes[0];
1675next_slot:
1676 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1677 ret = btrfs_next_leaf(root, path);
1678 if (ret > 0)
1679 break;
1680 if (ret < 0)
1681 goto error;
1682 leaf = path->nodes[0];
1683 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001684 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001685 continue;
1686 }
1687
1688 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1689 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1690 key.type != BTRFS_DEV_ITEM_KEY)
1691 break;
1692
1693 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1694 struct btrfs_dev_item);
1695 devid = btrfs_device_id(leaf, dev_item);
1696 read_extent_buffer(leaf, dev_uuid,
1697 (unsigned long)btrfs_device_uuid(dev_item),
1698 BTRFS_UUID_SIZE);
1699 read_extent_buffer(leaf, fs_uuid,
1700 (unsigned long)btrfs_device_fsid(dev_item),
1701 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001702 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1703 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001704 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001705
1706 if (device->fs_devices->seeding) {
1707 btrfs_set_device_generation(leaf, dev_item,
1708 device->generation);
1709 btrfs_mark_buffer_dirty(leaf);
1710 }
1711
1712 path->slots[0]++;
1713 goto next_slot;
1714 }
1715 ret = 0;
1716error:
1717 btrfs_free_path(path);
1718 return ret;
1719}
1720
Chris Mason788f20e2008-04-28 15:29:42 -04001721int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1722{
Josef Bacikd5e20032011-08-04 14:52:27 +00001723 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001724 struct btrfs_trans_handle *trans;
1725 struct btrfs_device *device;
1726 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001727 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001728 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001729 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001730 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001731 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001732 int ret = 0;
1733
Yan Zheng2b820322008-11-17 21:11:30 -05001734 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001735 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001736
Li Zefana5d16332011-12-07 20:08:40 -05001737 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001738 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001739 if (IS_ERR(bdev))
1740 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001741
Yan Zheng2b820322008-11-17 21:11:30 -05001742 if (root->fs_info->fs_devices->seeding) {
1743 seeding_dev = 1;
1744 down_write(&sb->s_umount);
1745 mutex_lock(&uuid_mutex);
1746 }
1747
Chris Mason8c8bee12008-09-29 11:19:10 -04001748 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001749
Chris Mason788f20e2008-04-28 15:29:42 -04001750 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001751
1752 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001753 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001754 if (device->bdev == bdev) {
1755 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001756 mutex_unlock(
1757 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001758 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001759 }
1760 }
Liu Bod25628b2012-11-14 14:35:30 +00001761 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001762
1763 device = kzalloc(sizeof(*device), GFP_NOFS);
1764 if (!device) {
1765 /* we can safely leave the fs_devices entry around */
1766 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001767 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001768 }
1769
Josef Bacik606686e2012-06-04 14:03:51 -04001770 name = rcu_string_strdup(device_path, GFP_NOFS);
1771 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001772 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001773 ret = -ENOMEM;
1774 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001775 }
Josef Bacik606686e2012-06-04 14:03:51 -04001776 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001777
1778 ret = find_next_devid(root, &device->devid);
1779 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001780 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001781 kfree(device);
1782 goto error;
1783 }
1784
Yan, Zhenga22285a2010-05-16 10:48:46 -04001785 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001786 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001787 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001788 kfree(device);
1789 ret = PTR_ERR(trans);
1790 goto error;
1791 }
1792
Yan Zheng2b820322008-11-17 21:11:30 -05001793 lock_chunks(root);
1794
Josef Bacikd5e20032011-08-04 14:52:27 +00001795 q = bdev_get_queue(bdev);
1796 if (blk_queue_discard(q))
1797 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001798 device->writeable = 1;
1799 device->work.func = pending_bios_fn;
1800 generate_random_uuid(device->uuid);
1801 spin_lock_init(&device->io_lock);
1802 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001803 device->io_width = root->sectorsize;
1804 device->io_align = root->sectorsize;
1805 device->sector_size = root->sectorsize;
1806 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001807 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001808 device->dev_root = root->fs_info->dev_root;
1809 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001810 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001811 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001812 set_blocksize(device->bdev, 4096);
1813
Yan Zheng2b820322008-11-17 21:11:30 -05001814 if (seeding_dev) {
1815 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001816 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001817 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001818 }
1819
1820 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001821
Chris Masone5e9a522009-06-10 15:17:02 -04001822 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001823 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001824 list_add(&device->dev_alloc_list,
1825 &root->fs_info->fs_devices->alloc_list);
1826 root->fs_info->fs_devices->num_devices++;
1827 root->fs_info->fs_devices->open_devices++;
1828 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001829 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001830 if (device->can_discard)
1831 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001832 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1833
Josef Bacik2bf64752011-09-26 17:12:22 -04001834 spin_lock(&root->fs_info->free_chunk_lock);
1835 root->fs_info->free_chunk_space += device->total_bytes;
1836 spin_unlock(&root->fs_info->free_chunk_lock);
1837
Chris Masonc2898112009-06-10 09:51:32 -04001838 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1839 root->fs_info->fs_devices->rotating = 1;
1840
David Sterba6c417612011-04-13 15:41:04 +02001841 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1842 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001843 total_bytes + device->total_bytes);
1844
David Sterba6c417612011-04-13 15:41:04 +02001845 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1846 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001847 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001848 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001849
Yan Zheng2b820322008-11-17 21:11:30 -05001850 if (seeding_dev) {
1851 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001852 if (ret) {
1853 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001854 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001855 }
Yan Zheng2b820322008-11-17 21:11:30 -05001856 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06001857 if (ret) {
1858 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001859 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001860 }
Yan Zheng2b820322008-11-17 21:11:30 -05001861 } else {
1862 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001863 if (ret) {
1864 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001865 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001866 }
Yan Zheng2b820322008-11-17 21:11:30 -05001867 }
1868
Chris Mason913d9522009-03-10 13:17:18 -04001869 /*
1870 * we've got more storage, clear any full flags on the space
1871 * infos
1872 */
1873 btrfs_clear_space_info_full(root->fs_info);
1874
Chris Mason7d9eb122008-07-08 14:19:17 -04001875 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001876 root->fs_info->num_tolerated_disk_barrier_failures =
1877 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001878 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001879
1880 if (seeding_dev) {
1881 mutex_unlock(&uuid_mutex);
1882 up_write(&sb->s_umount);
1883
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001884 if (ret) /* transaction commit */
1885 return ret;
1886
Yan Zheng2b820322008-11-17 21:11:30 -05001887 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001888 if (ret < 0)
1889 btrfs_error(root->fs_info, ret,
1890 "Failed to relocate sys chunks after "
1891 "device initialization. This can be fixed "
1892 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00001893 trans = btrfs_attach_transaction(root);
1894 if (IS_ERR(trans)) {
1895 if (PTR_ERR(trans) == -ENOENT)
1896 return 0;
1897 return PTR_ERR(trans);
1898 }
1899 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001900 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001901
Chris Mason788f20e2008-04-28 15:29:42 -04001902 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001903
1904error_trans:
1905 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001906 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001907 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001908 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001909error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001910 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001911 if (seeding_dev) {
1912 mutex_unlock(&uuid_mutex);
1913 up_write(&sb->s_umount);
1914 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001915 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001916}
1917
Chris Masond3977122009-01-05 21:25:51 -05001918static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1919 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001920{
1921 int ret;
1922 struct btrfs_path *path;
1923 struct btrfs_root *root;
1924 struct btrfs_dev_item *dev_item;
1925 struct extent_buffer *leaf;
1926 struct btrfs_key key;
1927
1928 root = device->dev_root->fs_info->chunk_root;
1929
1930 path = btrfs_alloc_path();
1931 if (!path)
1932 return -ENOMEM;
1933
1934 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1935 key.type = BTRFS_DEV_ITEM_KEY;
1936 key.offset = device->devid;
1937
1938 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1939 if (ret < 0)
1940 goto out;
1941
1942 if (ret > 0) {
1943 ret = -ENOENT;
1944 goto out;
1945 }
1946
1947 leaf = path->nodes[0];
1948 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1949
1950 btrfs_set_device_id(leaf, dev_item, device->devid);
1951 btrfs_set_device_type(leaf, dev_item, device->type);
1952 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1953 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1954 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001955 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001956 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1957 btrfs_mark_buffer_dirty(leaf);
1958
1959out:
1960 btrfs_free_path(path);
1961 return ret;
1962}
1963
Chris Mason7d9eb122008-07-08 14:19:17 -04001964static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001965 struct btrfs_device *device, u64 new_size)
1966{
1967 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001968 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001969 u64 old_total = btrfs_super_total_bytes(super_copy);
1970 u64 diff = new_size - device->total_bytes;
1971
Yan Zheng2b820322008-11-17 21:11:30 -05001972 if (!device->writeable)
1973 return -EACCES;
1974 if (new_size <= device->total_bytes)
1975 return -EINVAL;
1976
Chris Mason8f18cf12008-04-25 16:53:30 -04001977 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001978 device->fs_devices->total_rw_bytes += diff;
1979
1980 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001981 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001982 btrfs_clear_space_info_full(device->dev_root->fs_info);
1983
Chris Mason8f18cf12008-04-25 16:53:30 -04001984 return btrfs_update_device(trans, device);
1985}
1986
Chris Mason7d9eb122008-07-08 14:19:17 -04001987int btrfs_grow_device(struct btrfs_trans_handle *trans,
1988 struct btrfs_device *device, u64 new_size)
1989{
1990 int ret;
1991 lock_chunks(device->dev_root);
1992 ret = __btrfs_grow_device(trans, device, new_size);
1993 unlock_chunks(device->dev_root);
1994 return ret;
1995}
1996
Chris Mason8f18cf12008-04-25 16:53:30 -04001997static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1998 struct btrfs_root *root,
1999 u64 chunk_tree, u64 chunk_objectid,
2000 u64 chunk_offset)
2001{
2002 int ret;
2003 struct btrfs_path *path;
2004 struct btrfs_key key;
2005
2006 root = root->fs_info->chunk_root;
2007 path = btrfs_alloc_path();
2008 if (!path)
2009 return -ENOMEM;
2010
2011 key.objectid = chunk_objectid;
2012 key.offset = chunk_offset;
2013 key.type = BTRFS_CHUNK_ITEM_KEY;
2014
2015 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002016 if (ret < 0)
2017 goto out;
2018 else if (ret > 0) { /* Logic error or corruption */
2019 btrfs_error(root->fs_info, -ENOENT,
2020 "Failed lookup while freeing chunk.");
2021 ret = -ENOENT;
2022 goto out;
2023 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002024
2025 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002026 if (ret < 0)
2027 btrfs_error(root->fs_info, ret,
2028 "Failed to delete chunk item.");
2029out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002030 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002031 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002032}
2033
Christoph Hellwigb2950862008-12-02 09:54:17 -05002034static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002035 chunk_offset)
2036{
David Sterba6c417612011-04-13 15:41:04 +02002037 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002038 struct btrfs_disk_key *disk_key;
2039 struct btrfs_chunk *chunk;
2040 u8 *ptr;
2041 int ret = 0;
2042 u32 num_stripes;
2043 u32 array_size;
2044 u32 len = 0;
2045 u32 cur;
2046 struct btrfs_key key;
2047
2048 array_size = btrfs_super_sys_array_size(super_copy);
2049
2050 ptr = super_copy->sys_chunk_array;
2051 cur = 0;
2052
2053 while (cur < array_size) {
2054 disk_key = (struct btrfs_disk_key *)ptr;
2055 btrfs_disk_key_to_cpu(&key, disk_key);
2056
2057 len = sizeof(*disk_key);
2058
2059 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2060 chunk = (struct btrfs_chunk *)(ptr + len);
2061 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2062 len += btrfs_chunk_item_size(num_stripes);
2063 } else {
2064 ret = -EIO;
2065 break;
2066 }
2067 if (key.objectid == chunk_objectid &&
2068 key.offset == chunk_offset) {
2069 memmove(ptr, ptr + len, array_size - (cur + len));
2070 array_size -= len;
2071 btrfs_set_super_sys_array_size(super_copy, array_size);
2072 } else {
2073 ptr += len;
2074 cur += len;
2075 }
2076 }
2077 return ret;
2078}
2079
Christoph Hellwigb2950862008-12-02 09:54:17 -05002080static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002081 u64 chunk_tree, u64 chunk_objectid,
2082 u64 chunk_offset)
2083{
2084 struct extent_map_tree *em_tree;
2085 struct btrfs_root *extent_root;
2086 struct btrfs_trans_handle *trans;
2087 struct extent_map *em;
2088 struct map_lookup *map;
2089 int ret;
2090 int i;
2091
2092 root = root->fs_info->chunk_root;
2093 extent_root = root->fs_info->extent_root;
2094 em_tree = &root->fs_info->mapping_tree.map_tree;
2095
Josef Bacikba1bf482009-09-11 16:11:19 -04002096 ret = btrfs_can_relocate(extent_root, chunk_offset);
2097 if (ret)
2098 return -ENOSPC;
2099
Chris Mason8f18cf12008-04-25 16:53:30 -04002100 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002101 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002102 if (ret)
2103 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002104
Yan, Zhenga22285a2010-05-16 10:48:46 -04002105 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002106 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002107
Chris Mason7d9eb122008-07-08 14:19:17 -04002108 lock_chunks(root);
2109
Chris Mason8f18cf12008-04-25 16:53:30 -04002110 /*
2111 * step two, delete the device extents and the
2112 * chunk tree entries
2113 */
Chris Mason890871b2009-09-02 16:24:52 -04002114 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002115 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002116 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002117
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002118 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002119 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002120 map = (struct map_lookup *)em->bdev;
2121
2122 for (i = 0; i < map->num_stripes; i++) {
2123 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2124 map->stripes[i].physical);
2125 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002126
Chris Masondfe25022008-05-13 13:46:40 -04002127 if (map->stripes[i].dev) {
2128 ret = btrfs_update_device(trans, map->stripes[i].dev);
2129 BUG_ON(ret);
2130 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002131 }
2132 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2133 chunk_offset);
2134
2135 BUG_ON(ret);
2136
liubo1abe9b82011-03-24 11:18:59 +00002137 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2138
Chris Mason8f18cf12008-04-25 16:53:30 -04002139 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2140 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2141 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002142 }
2143
Zheng Yan1a40e232008-09-26 10:09:34 -04002144 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2145 BUG_ON(ret);
2146
Chris Mason890871b2009-09-02 16:24:52 -04002147 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002148 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002149 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002150
Chris Mason8f18cf12008-04-25 16:53:30 -04002151 kfree(map);
2152 em->bdev = NULL;
2153
2154 /* once for the tree */
2155 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002156 /* once for us */
2157 free_extent_map(em);
2158
Chris Mason7d9eb122008-07-08 14:19:17 -04002159 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002160 btrfs_end_transaction(trans, root);
2161 return 0;
2162}
2163
Yan Zheng2b820322008-11-17 21:11:30 -05002164static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2165{
2166 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2167 struct btrfs_path *path;
2168 struct extent_buffer *leaf;
2169 struct btrfs_chunk *chunk;
2170 struct btrfs_key key;
2171 struct btrfs_key found_key;
2172 u64 chunk_tree = chunk_root->root_key.objectid;
2173 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002174 bool retried = false;
2175 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002176 int ret;
2177
2178 path = btrfs_alloc_path();
2179 if (!path)
2180 return -ENOMEM;
2181
Josef Bacikba1bf482009-09-11 16:11:19 -04002182again:
Yan Zheng2b820322008-11-17 21:11:30 -05002183 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2184 key.offset = (u64)-1;
2185 key.type = BTRFS_CHUNK_ITEM_KEY;
2186
2187 while (1) {
2188 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2189 if (ret < 0)
2190 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002191 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002192
2193 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2194 key.type);
2195 if (ret < 0)
2196 goto error;
2197 if (ret > 0)
2198 break;
2199
2200 leaf = path->nodes[0];
2201 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2202
2203 chunk = btrfs_item_ptr(leaf, path->slots[0],
2204 struct btrfs_chunk);
2205 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002206 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002207
2208 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2209 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2210 found_key.objectid,
2211 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002212 if (ret == -ENOSPC)
2213 failed++;
2214 else if (ret)
2215 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002216 }
2217
2218 if (found_key.offset == 0)
2219 break;
2220 key.offset = found_key.offset - 1;
2221 }
2222 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002223 if (failed && !retried) {
2224 failed = 0;
2225 retried = true;
2226 goto again;
2227 } else if (failed && retried) {
2228 WARN_ON(1);
2229 ret = -ENOSPC;
2230 }
Yan Zheng2b820322008-11-17 21:11:30 -05002231error:
2232 btrfs_free_path(path);
2233 return ret;
2234}
2235
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002236static int insert_balance_item(struct btrfs_root *root,
2237 struct btrfs_balance_control *bctl)
2238{
2239 struct btrfs_trans_handle *trans;
2240 struct btrfs_balance_item *item;
2241 struct btrfs_disk_balance_args disk_bargs;
2242 struct btrfs_path *path;
2243 struct extent_buffer *leaf;
2244 struct btrfs_key key;
2245 int ret, err;
2246
2247 path = btrfs_alloc_path();
2248 if (!path)
2249 return -ENOMEM;
2250
2251 trans = btrfs_start_transaction(root, 0);
2252 if (IS_ERR(trans)) {
2253 btrfs_free_path(path);
2254 return PTR_ERR(trans);
2255 }
2256
2257 key.objectid = BTRFS_BALANCE_OBJECTID;
2258 key.type = BTRFS_BALANCE_ITEM_KEY;
2259 key.offset = 0;
2260
2261 ret = btrfs_insert_empty_item(trans, root, path, &key,
2262 sizeof(*item));
2263 if (ret)
2264 goto out;
2265
2266 leaf = path->nodes[0];
2267 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2268
2269 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2270
2271 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2272 btrfs_set_balance_data(leaf, item, &disk_bargs);
2273 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2274 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2275 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2276 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2277
2278 btrfs_set_balance_flags(leaf, item, bctl->flags);
2279
2280 btrfs_mark_buffer_dirty(leaf);
2281out:
2282 btrfs_free_path(path);
2283 err = btrfs_commit_transaction(trans, root);
2284 if (err && !ret)
2285 ret = err;
2286 return ret;
2287}
2288
2289static int del_balance_item(struct btrfs_root *root)
2290{
2291 struct btrfs_trans_handle *trans;
2292 struct btrfs_path *path;
2293 struct btrfs_key key;
2294 int ret, err;
2295
2296 path = btrfs_alloc_path();
2297 if (!path)
2298 return -ENOMEM;
2299
2300 trans = btrfs_start_transaction(root, 0);
2301 if (IS_ERR(trans)) {
2302 btrfs_free_path(path);
2303 return PTR_ERR(trans);
2304 }
2305
2306 key.objectid = BTRFS_BALANCE_OBJECTID;
2307 key.type = BTRFS_BALANCE_ITEM_KEY;
2308 key.offset = 0;
2309
2310 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2311 if (ret < 0)
2312 goto out;
2313 if (ret > 0) {
2314 ret = -ENOENT;
2315 goto out;
2316 }
2317
2318 ret = btrfs_del_item(trans, root, path);
2319out:
2320 btrfs_free_path(path);
2321 err = btrfs_commit_transaction(trans, root);
2322 if (err && !ret)
2323 ret = err;
2324 return ret;
2325}
2326
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002327/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002328 * This is a heuristic used to reduce the number of chunks balanced on
2329 * resume after balance was interrupted.
2330 */
2331static void update_balance_args(struct btrfs_balance_control *bctl)
2332{
2333 /*
2334 * Turn on soft mode for chunk types that were being converted.
2335 */
2336 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2337 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2338 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2339 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2340 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2341 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2342
2343 /*
2344 * Turn on usage filter if is not already used. The idea is
2345 * that chunks that we have already balanced should be
2346 * reasonably full. Don't do it for chunks that are being
2347 * converted - that will keep us from relocating unconverted
2348 * (albeit full) chunks.
2349 */
2350 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2351 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2352 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2353 bctl->data.usage = 90;
2354 }
2355 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2356 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2357 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2358 bctl->sys.usage = 90;
2359 }
2360 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2361 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2362 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2363 bctl->meta.usage = 90;
2364 }
2365}
2366
2367/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002368 * Should be called with both balance and volume mutexes held to
2369 * serialize other volume operations (add_dev/rm_dev/resize) with
2370 * restriper. Same goes for unset_balance_control.
2371 */
2372static void set_balance_control(struct btrfs_balance_control *bctl)
2373{
2374 struct btrfs_fs_info *fs_info = bctl->fs_info;
2375
2376 BUG_ON(fs_info->balance_ctl);
2377
2378 spin_lock(&fs_info->balance_lock);
2379 fs_info->balance_ctl = bctl;
2380 spin_unlock(&fs_info->balance_lock);
2381}
2382
2383static void unset_balance_control(struct btrfs_fs_info *fs_info)
2384{
2385 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2386
2387 BUG_ON(!fs_info->balance_ctl);
2388
2389 spin_lock(&fs_info->balance_lock);
2390 fs_info->balance_ctl = NULL;
2391 spin_unlock(&fs_info->balance_lock);
2392
2393 kfree(bctl);
2394}
2395
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002396/*
2397 * Balance filters. Return 1 if chunk should be filtered out
2398 * (should not be balanced).
2399 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002400static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002401 struct btrfs_balance_args *bargs)
2402{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002403 chunk_type = chunk_to_extended(chunk_type) &
2404 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002405
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002406 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002407 return 0;
2408
2409 return 1;
2410}
2411
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002412static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2413 struct btrfs_balance_args *bargs)
2414{
2415 struct btrfs_block_group_cache *cache;
2416 u64 chunk_used, user_thresh;
2417 int ret = 1;
2418
2419 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2420 chunk_used = btrfs_block_group_used(&cache->item);
2421
2422 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2423 if (chunk_used < user_thresh)
2424 ret = 0;
2425
2426 btrfs_put_block_group(cache);
2427 return ret;
2428}
2429
Ilya Dryomov409d4042012-01-16 22:04:47 +02002430static int chunk_devid_filter(struct extent_buffer *leaf,
2431 struct btrfs_chunk *chunk,
2432 struct btrfs_balance_args *bargs)
2433{
2434 struct btrfs_stripe *stripe;
2435 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2436 int i;
2437
2438 for (i = 0; i < num_stripes; i++) {
2439 stripe = btrfs_stripe_nr(chunk, i);
2440 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2441 return 0;
2442 }
2443
2444 return 1;
2445}
2446
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002447/* [pstart, pend) */
2448static int chunk_drange_filter(struct extent_buffer *leaf,
2449 struct btrfs_chunk *chunk,
2450 u64 chunk_offset,
2451 struct btrfs_balance_args *bargs)
2452{
2453 struct btrfs_stripe *stripe;
2454 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2455 u64 stripe_offset;
2456 u64 stripe_length;
2457 int factor;
2458 int i;
2459
2460 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2461 return 0;
2462
2463 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2464 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2465 factor = 2;
2466 else
2467 factor = 1;
2468 factor = num_stripes / factor;
2469
2470 for (i = 0; i < num_stripes; i++) {
2471 stripe = btrfs_stripe_nr(chunk, i);
2472 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2473 continue;
2474
2475 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2476 stripe_length = btrfs_chunk_length(leaf, chunk);
2477 do_div(stripe_length, factor);
2478
2479 if (stripe_offset < bargs->pend &&
2480 stripe_offset + stripe_length > bargs->pstart)
2481 return 0;
2482 }
2483
2484 return 1;
2485}
2486
Ilya Dryomovea671762012-01-16 22:04:48 +02002487/* [vstart, vend) */
2488static int chunk_vrange_filter(struct extent_buffer *leaf,
2489 struct btrfs_chunk *chunk,
2490 u64 chunk_offset,
2491 struct btrfs_balance_args *bargs)
2492{
2493 if (chunk_offset < bargs->vend &&
2494 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2495 /* at least part of the chunk is inside this vrange */
2496 return 0;
2497
2498 return 1;
2499}
2500
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002501static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002502 struct btrfs_balance_args *bargs)
2503{
2504 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2505 return 0;
2506
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002507 chunk_type = chunk_to_extended(chunk_type) &
2508 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002509
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002510 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002511 return 1;
2512
2513 return 0;
2514}
2515
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002516static int should_balance_chunk(struct btrfs_root *root,
2517 struct extent_buffer *leaf,
2518 struct btrfs_chunk *chunk, u64 chunk_offset)
2519{
2520 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2521 struct btrfs_balance_args *bargs = NULL;
2522 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2523
2524 /* type filter */
2525 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2526 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2527 return 0;
2528 }
2529
2530 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2531 bargs = &bctl->data;
2532 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2533 bargs = &bctl->sys;
2534 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2535 bargs = &bctl->meta;
2536
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002537 /* profiles filter */
2538 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2539 chunk_profiles_filter(chunk_type, bargs)) {
2540 return 0;
2541 }
2542
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002543 /* usage filter */
2544 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2545 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2546 return 0;
2547 }
2548
Ilya Dryomov409d4042012-01-16 22:04:47 +02002549 /* devid filter */
2550 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2551 chunk_devid_filter(leaf, chunk, bargs)) {
2552 return 0;
2553 }
2554
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002555 /* drange filter, makes sense only with devid filter */
2556 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2557 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2558 return 0;
2559 }
2560
Ilya Dryomovea671762012-01-16 22:04:48 +02002561 /* vrange filter */
2562 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2563 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2564 return 0;
2565 }
2566
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002567 /* soft profile changing mode */
2568 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2569 chunk_soft_convert_filter(chunk_type, bargs)) {
2570 return 0;
2571 }
2572
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002573 return 1;
2574}
2575
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002576static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002577{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002578 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002579 struct btrfs_root *chunk_root = fs_info->chunk_root;
2580 struct btrfs_root *dev_root = fs_info->dev_root;
2581 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002582 struct btrfs_device *device;
2583 u64 old_size;
2584 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002585 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002586 struct btrfs_path *path;
2587 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002588 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002589 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002590 struct extent_buffer *leaf;
2591 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002592 int ret;
2593 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002594 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002595
Chris Masonec44a352008-04-28 15:29:52 -04002596 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002597 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002598 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002599 old_size = device->total_bytes;
2600 size_to_free = div_factor(old_size, 1);
2601 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002602 if (!device->writeable ||
2603 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002604 continue;
2605
2606 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002607 if (ret == -ENOSPC)
2608 break;
Chris Masonec44a352008-04-28 15:29:52 -04002609 BUG_ON(ret);
2610
Yan, Zhenga22285a2010-05-16 10:48:46 -04002611 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002612 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002613
2614 ret = btrfs_grow_device(trans, device, old_size);
2615 BUG_ON(ret);
2616
2617 btrfs_end_transaction(trans, dev_root);
2618 }
2619
2620 /* step two, relocate all the chunks */
2621 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002622 if (!path) {
2623 ret = -ENOMEM;
2624 goto error;
2625 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002626
2627 /* zero out stat counters */
2628 spin_lock(&fs_info->balance_lock);
2629 memset(&bctl->stat, 0, sizeof(bctl->stat));
2630 spin_unlock(&fs_info->balance_lock);
2631again:
Chris Masonec44a352008-04-28 15:29:52 -04002632 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2633 key.offset = (u64)-1;
2634 key.type = BTRFS_CHUNK_ITEM_KEY;
2635
Chris Masond3977122009-01-05 21:25:51 -05002636 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002637 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002638 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002639 ret = -ECANCELED;
2640 goto error;
2641 }
2642
Chris Masonec44a352008-04-28 15:29:52 -04002643 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2644 if (ret < 0)
2645 goto error;
2646
2647 /*
2648 * this shouldn't happen, it means the last relocate
2649 * failed
2650 */
2651 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002652 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002653
2654 ret = btrfs_previous_item(chunk_root, path, 0,
2655 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002656 if (ret) {
2657 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002658 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002659 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002660
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002661 leaf = path->nodes[0];
2662 slot = path->slots[0];
2663 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2664
Chris Masonec44a352008-04-28 15:29:52 -04002665 if (found_key.objectid != key.objectid)
2666 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002667
Chris Masonec44a352008-04-28 15:29:52 -04002668 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002669 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002670 break;
2671
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002672 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2673
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002674 if (!counting) {
2675 spin_lock(&fs_info->balance_lock);
2676 bctl->stat.considered++;
2677 spin_unlock(&fs_info->balance_lock);
2678 }
2679
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002680 ret = should_balance_chunk(chunk_root, leaf, chunk,
2681 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002682 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002683 if (!ret)
2684 goto loop;
2685
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002686 if (counting) {
2687 spin_lock(&fs_info->balance_lock);
2688 bctl->stat.expected++;
2689 spin_unlock(&fs_info->balance_lock);
2690 goto loop;
2691 }
2692
Chris Masonec44a352008-04-28 15:29:52 -04002693 ret = btrfs_relocate_chunk(chunk_root,
2694 chunk_root->root_key.objectid,
2695 found_key.objectid,
2696 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002697 if (ret && ret != -ENOSPC)
2698 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002699 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002700 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002701 } else {
2702 spin_lock(&fs_info->balance_lock);
2703 bctl->stat.completed++;
2704 spin_unlock(&fs_info->balance_lock);
2705 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002706loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002707 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002708 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002709
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002710 if (counting) {
2711 btrfs_release_path(path);
2712 counting = false;
2713 goto again;
2714 }
Chris Masonec44a352008-04-28 15:29:52 -04002715error:
2716 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002717 if (enospc_errors) {
2718 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2719 enospc_errors);
2720 if (!ret)
2721 ret = -ENOSPC;
2722 }
2723
Chris Masonec44a352008-04-28 15:29:52 -04002724 return ret;
2725}
2726
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002727/**
2728 * alloc_profile_is_valid - see if a given profile is valid and reduced
2729 * @flags: profile to validate
2730 * @extended: if true @flags is treated as an extended profile
2731 */
2732static int alloc_profile_is_valid(u64 flags, int extended)
2733{
2734 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2735 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2736
2737 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2738
2739 /* 1) check that all other bits are zeroed */
2740 if (flags & ~mask)
2741 return 0;
2742
2743 /* 2) see if profile is reduced */
2744 if (flags == 0)
2745 return !extended; /* "0" is valid for usual profiles */
2746
2747 /* true if exactly one bit set */
2748 return (flags & (flags - 1)) == 0;
2749}
2750
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002751static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2752{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002753 /* cancel requested || normal exit path */
2754 return atomic_read(&fs_info->balance_cancel_req) ||
2755 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2756 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002757}
2758
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002759static void __cancel_balance(struct btrfs_fs_info *fs_info)
2760{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002761 int ret;
2762
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002763 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002764 ret = del_balance_item(fs_info->tree_root);
2765 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002766}
2767
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002768void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002769 struct btrfs_ioctl_balance_args *bargs);
2770
2771/*
2772 * Should be called with both balance and volume mutexes held
2773 */
2774int btrfs_balance(struct btrfs_balance_control *bctl,
2775 struct btrfs_ioctl_balance_args *bargs)
2776{
2777 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002778 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002779 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002780 int ret;
2781
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002782 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002783 atomic_read(&fs_info->balance_pause_req) ||
2784 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002785 ret = -EINVAL;
2786 goto out;
2787 }
2788
Ilya Dryomove4837f82012-03-27 17:09:17 +03002789 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2790 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2791 mixed = 1;
2792
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002793 /*
2794 * In case of mixed groups both data and meta should be picked,
2795 * and identical options should be given for both of them.
2796 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002797 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2798 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002799 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2800 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2801 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2802 printk(KERN_ERR "btrfs: with mixed groups data and "
2803 "metadata balance options must be the same\n");
2804 ret = -EINVAL;
2805 goto out;
2806 }
2807 }
2808
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002809 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2810 if (fs_info->fs_devices->num_devices == 1)
2811 allowed |= BTRFS_BLOCK_GROUP_DUP;
2812 else if (fs_info->fs_devices->num_devices < 4)
2813 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2814 else
2815 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2816 BTRFS_BLOCK_GROUP_RAID10);
2817
Ilya Dryomov6728b192012-03-27 17:09:17 +03002818 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2819 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2820 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002821 printk(KERN_ERR "btrfs: unable to start balance with target "
2822 "data profile %llu\n",
2823 (unsigned long long)bctl->data.target);
2824 ret = -EINVAL;
2825 goto out;
2826 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002827 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2828 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2829 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002830 printk(KERN_ERR "btrfs: unable to start balance with target "
2831 "metadata profile %llu\n",
2832 (unsigned long long)bctl->meta.target);
2833 ret = -EINVAL;
2834 goto out;
2835 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002836 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2837 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2838 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002839 printk(KERN_ERR "btrfs: unable to start balance with target "
2840 "system profile %llu\n",
2841 (unsigned long long)bctl->sys.target);
2842 ret = -EINVAL;
2843 goto out;
2844 }
2845
Ilya Dryomove4837f82012-03-27 17:09:17 +03002846 /* allow dup'ed data chunks only in mixed mode */
2847 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002848 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002849 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2850 ret = -EINVAL;
2851 goto out;
2852 }
2853
2854 /* allow to reduce meta or sys integrity only if force set */
2855 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2856 BTRFS_BLOCK_GROUP_RAID10;
2857 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2858 (fs_info->avail_system_alloc_bits & allowed) &&
2859 !(bctl->sys.target & allowed)) ||
2860 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2861 (fs_info->avail_metadata_alloc_bits & allowed) &&
2862 !(bctl->meta.target & allowed))) {
2863 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2864 printk(KERN_INFO "btrfs: force reducing metadata "
2865 "integrity\n");
2866 } else {
2867 printk(KERN_ERR "btrfs: balance will reduce metadata "
2868 "integrity, use force if you want this\n");
2869 ret = -EINVAL;
2870 goto out;
2871 }
2872 }
2873
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002874 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
2875 int num_tolerated_disk_barrier_failures;
2876 u64 target = bctl->sys.target;
2877
2878 num_tolerated_disk_barrier_failures =
2879 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2880 if (num_tolerated_disk_barrier_failures > 0 &&
2881 (target &
2882 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
2883 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
2884 num_tolerated_disk_barrier_failures = 0;
2885 else if (num_tolerated_disk_barrier_failures > 1 &&
2886 (target &
2887 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
2888 num_tolerated_disk_barrier_failures = 1;
2889
2890 fs_info->num_tolerated_disk_barrier_failures =
2891 num_tolerated_disk_barrier_failures;
2892 }
2893
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002894 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002895 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002896 goto out;
2897
Ilya Dryomov59641012012-01-16 22:04:48 +02002898 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2899 BUG_ON(ret == -EEXIST);
2900 set_balance_control(bctl);
2901 } else {
2902 BUG_ON(ret != -EEXIST);
2903 spin_lock(&fs_info->balance_lock);
2904 update_balance_args(bctl);
2905 spin_unlock(&fs_info->balance_lock);
2906 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002907
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002908 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002909 mutex_unlock(&fs_info->balance_mutex);
2910
2911 ret = __btrfs_balance(fs_info);
2912
2913 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002914 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002915
2916 if (bargs) {
2917 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002918 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002919 }
2920
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002921 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2922 balance_need_close(fs_info)) {
2923 __cancel_balance(fs_info);
2924 }
2925
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002926 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
2927 fs_info->num_tolerated_disk_barrier_failures =
2928 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2929 }
2930
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002931 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002932
2933 return ret;
2934out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002935 if (bctl->flags & BTRFS_BALANCE_RESUME)
2936 __cancel_balance(fs_info);
2937 else
2938 kfree(bctl);
2939 return ret;
2940}
2941
2942static int balance_kthread(void *data)
2943{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002944 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002945 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002946
2947 mutex_lock(&fs_info->volume_mutex);
2948 mutex_lock(&fs_info->balance_mutex);
2949
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002950 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002951 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002952 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002953 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002954
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002955 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov59641012012-01-16 22:04:48 +02002956 mutex_unlock(&fs_info->balance_mutex);
2957 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002958
Ilya Dryomov59641012012-01-16 22:04:48 +02002959 return ret;
2960}
2961
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002962int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
2963{
2964 struct task_struct *tsk;
2965
2966 spin_lock(&fs_info->balance_lock);
2967 if (!fs_info->balance_ctl) {
2968 spin_unlock(&fs_info->balance_lock);
2969 return 0;
2970 }
2971 spin_unlock(&fs_info->balance_lock);
2972
2973 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2974 printk(KERN_INFO "btrfs: force skipping balance\n");
2975 return 0;
2976 }
2977
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002978 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002979 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
2980 if (IS_ERR(tsk))
2981 return PTR_ERR(tsk);
2982
2983 return 0;
2984}
2985
Ilya Dryomov68310a52012-06-22 12:24:12 -06002986int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02002987{
Ilya Dryomov59641012012-01-16 22:04:48 +02002988 struct btrfs_balance_control *bctl;
2989 struct btrfs_balance_item *item;
2990 struct btrfs_disk_balance_args disk_bargs;
2991 struct btrfs_path *path;
2992 struct extent_buffer *leaf;
2993 struct btrfs_key key;
2994 int ret;
2995
2996 path = btrfs_alloc_path();
2997 if (!path)
2998 return -ENOMEM;
2999
Ilya Dryomov68310a52012-06-22 12:24:12 -06003000 key.objectid = BTRFS_BALANCE_OBJECTID;
3001 key.type = BTRFS_BALANCE_ITEM_KEY;
3002 key.offset = 0;
3003
3004 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3005 if (ret < 0)
3006 goto out;
3007 if (ret > 0) { /* ret = -ENOENT; */
3008 ret = 0;
3009 goto out;
3010 }
3011
Ilya Dryomov59641012012-01-16 22:04:48 +02003012 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3013 if (!bctl) {
3014 ret = -ENOMEM;
3015 goto out;
3016 }
3017
Ilya Dryomov59641012012-01-16 22:04:48 +02003018 leaf = path->nodes[0];
3019 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3020
Ilya Dryomov68310a52012-06-22 12:24:12 -06003021 bctl->fs_info = fs_info;
3022 bctl->flags = btrfs_balance_flags(leaf, item);
3023 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003024
3025 btrfs_balance_data(leaf, item, &disk_bargs);
3026 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3027 btrfs_balance_meta(leaf, item, &disk_bargs);
3028 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3029 btrfs_balance_sys(leaf, item, &disk_bargs);
3030 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3031
Ilya Dryomov68310a52012-06-22 12:24:12 -06003032 mutex_lock(&fs_info->volume_mutex);
3033 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003034
Ilya Dryomov68310a52012-06-22 12:24:12 -06003035 set_balance_control(bctl);
3036
3037 mutex_unlock(&fs_info->balance_mutex);
3038 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003039out:
3040 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003041 return ret;
3042}
3043
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003044int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3045{
3046 int ret = 0;
3047
3048 mutex_lock(&fs_info->balance_mutex);
3049 if (!fs_info->balance_ctl) {
3050 mutex_unlock(&fs_info->balance_mutex);
3051 return -ENOTCONN;
3052 }
3053
3054 if (atomic_read(&fs_info->balance_running)) {
3055 atomic_inc(&fs_info->balance_pause_req);
3056 mutex_unlock(&fs_info->balance_mutex);
3057
3058 wait_event(fs_info->balance_wait_q,
3059 atomic_read(&fs_info->balance_running) == 0);
3060
3061 mutex_lock(&fs_info->balance_mutex);
3062 /* we are good with balance_ctl ripped off from under us */
3063 BUG_ON(atomic_read(&fs_info->balance_running));
3064 atomic_dec(&fs_info->balance_pause_req);
3065 } else {
3066 ret = -ENOTCONN;
3067 }
3068
3069 mutex_unlock(&fs_info->balance_mutex);
3070 return ret;
3071}
3072
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003073int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3074{
3075 mutex_lock(&fs_info->balance_mutex);
3076 if (!fs_info->balance_ctl) {
3077 mutex_unlock(&fs_info->balance_mutex);
3078 return -ENOTCONN;
3079 }
3080
3081 atomic_inc(&fs_info->balance_cancel_req);
3082 /*
3083 * if we are running just wait and return, balance item is
3084 * deleted in btrfs_balance in this case
3085 */
3086 if (atomic_read(&fs_info->balance_running)) {
3087 mutex_unlock(&fs_info->balance_mutex);
3088 wait_event(fs_info->balance_wait_q,
3089 atomic_read(&fs_info->balance_running) == 0);
3090 mutex_lock(&fs_info->balance_mutex);
3091 } else {
3092 /* __cancel_balance needs volume_mutex */
3093 mutex_unlock(&fs_info->balance_mutex);
3094 mutex_lock(&fs_info->volume_mutex);
3095 mutex_lock(&fs_info->balance_mutex);
3096
3097 if (fs_info->balance_ctl)
3098 __cancel_balance(fs_info);
3099
3100 mutex_unlock(&fs_info->volume_mutex);
3101 }
3102
3103 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3104 atomic_dec(&fs_info->balance_cancel_req);
3105 mutex_unlock(&fs_info->balance_mutex);
3106 return 0;
3107}
3108
Chris Mason8f18cf12008-04-25 16:53:30 -04003109/*
3110 * shrinking a device means finding all of the device extents past
3111 * the new size, and then following the back refs to the chunks.
3112 * The chunk relocation code actually frees the device extent
3113 */
3114int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3115{
3116 struct btrfs_trans_handle *trans;
3117 struct btrfs_root *root = device->dev_root;
3118 struct btrfs_dev_extent *dev_extent = NULL;
3119 struct btrfs_path *path;
3120 u64 length;
3121 u64 chunk_tree;
3122 u64 chunk_objectid;
3123 u64 chunk_offset;
3124 int ret;
3125 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003126 int failed = 0;
3127 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003128 struct extent_buffer *l;
3129 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003130 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003131 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003132 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003133 u64 diff = device->total_bytes - new_size;
3134
Chris Mason8f18cf12008-04-25 16:53:30 -04003135 path = btrfs_alloc_path();
3136 if (!path)
3137 return -ENOMEM;
3138
Chris Mason8f18cf12008-04-25 16:53:30 -04003139 path->reada = 2;
3140
Chris Mason7d9eb122008-07-08 14:19:17 -04003141 lock_chunks(root);
3142
Chris Mason8f18cf12008-04-25 16:53:30 -04003143 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003144 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003145 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003146 spin_lock(&root->fs_info->free_chunk_lock);
3147 root->fs_info->free_chunk_space -= diff;
3148 spin_unlock(&root->fs_info->free_chunk_lock);
3149 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003150 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003151
Josef Bacikba1bf482009-09-11 16:11:19 -04003152again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003153 key.objectid = device->devid;
3154 key.offset = (u64)-1;
3155 key.type = BTRFS_DEV_EXTENT_KEY;
3156
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003157 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003158 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3159 if (ret < 0)
3160 goto done;
3161
3162 ret = btrfs_previous_item(root, path, 0, key.type);
3163 if (ret < 0)
3164 goto done;
3165 if (ret) {
3166 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003167 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003168 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003169 }
3170
3171 l = path->nodes[0];
3172 slot = path->slots[0];
3173 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3174
Josef Bacikba1bf482009-09-11 16:11:19 -04003175 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003176 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003177 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003178 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003179
3180 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3181 length = btrfs_dev_extent_length(l, dev_extent);
3182
Josef Bacikba1bf482009-09-11 16:11:19 -04003183 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003184 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003185 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003186 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003187
3188 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3189 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3190 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003191 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003192
3193 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3194 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003195 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003196 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003197 if (ret == -ENOSPC)
3198 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003199 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003200
3201 if (failed && !retried) {
3202 failed = 0;
3203 retried = true;
3204 goto again;
3205 } else if (failed && retried) {
3206 ret = -ENOSPC;
3207 lock_chunks(root);
3208
3209 device->total_bytes = old_size;
3210 if (device->writeable)
3211 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003212 spin_lock(&root->fs_info->free_chunk_lock);
3213 root->fs_info->free_chunk_space += diff;
3214 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003215 unlock_chunks(root);
3216 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003217 }
3218
Chris Balld6397ba2009-04-27 07:29:03 -04003219 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003220 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003221 if (IS_ERR(trans)) {
3222 ret = PTR_ERR(trans);
3223 goto done;
3224 }
3225
Chris Balld6397ba2009-04-27 07:29:03 -04003226 lock_chunks(root);
3227
3228 device->disk_total_bytes = new_size;
3229 /* Now btrfs_update_device() will change the on-disk size. */
3230 ret = btrfs_update_device(trans, device);
3231 if (ret) {
3232 unlock_chunks(root);
3233 btrfs_end_transaction(trans, root);
3234 goto done;
3235 }
3236 WARN_ON(diff > old_total);
3237 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3238 unlock_chunks(root);
3239 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003240done:
3241 btrfs_free_path(path);
3242 return ret;
3243}
3244
Li Zefan125ccb02011-12-08 15:07:24 +08003245static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003246 struct btrfs_key *key,
3247 struct btrfs_chunk *chunk, int item_size)
3248{
David Sterba6c417612011-04-13 15:41:04 +02003249 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003250 struct btrfs_disk_key disk_key;
3251 u32 array_size;
3252 u8 *ptr;
3253
3254 array_size = btrfs_super_sys_array_size(super_copy);
3255 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3256 return -EFBIG;
3257
3258 ptr = super_copy->sys_chunk_array + array_size;
3259 btrfs_cpu_key_to_disk(&disk_key, key);
3260 memcpy(ptr, &disk_key, sizeof(disk_key));
3261 ptr += sizeof(disk_key);
3262 memcpy(ptr, chunk, item_size);
3263 item_size += sizeof(disk_key);
3264 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3265 return 0;
3266}
3267
Miao Xieb2117a32011-01-05 10:07:28 +00003268/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003269 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003270 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003271static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003272{
Arne Jansen73c5de02011-04-12 12:07:57 +02003273 const struct btrfs_device_info *di_a = a;
3274 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003275
Arne Jansen73c5de02011-04-12 12:07:57 +02003276 if (di_a->max_avail > di_b->max_avail)
3277 return -1;
3278 if (di_a->max_avail < di_b->max_avail)
3279 return 1;
3280 if (di_a->total_avail > di_b->total_avail)
3281 return -1;
3282 if (di_a->total_avail < di_b->total_avail)
3283 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003284 return 0;
3285}
3286
3287static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3288 struct btrfs_root *extent_root,
3289 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003290 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003291 u64 start, u64 type)
3292{
3293 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003294 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3295 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003296 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003297 struct extent_map_tree *em_tree;
3298 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003299 struct btrfs_device_info *devices_info = NULL;
3300 u64 total_avail;
3301 int num_stripes; /* total number of stripes to allocate */
3302 int sub_stripes; /* sub_stripes info for map */
3303 int dev_stripes; /* stripes per dev */
3304 int devs_max; /* max devs to use */
3305 int devs_min; /* min devs needed */
3306 int devs_increment; /* ndevs has to be a multiple of this */
3307 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003308 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003309 u64 max_stripe_size;
3310 u64 max_chunk_size;
3311 u64 stripe_size;
3312 u64 num_bytes;
3313 int ndevs;
3314 int i;
3315 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003316
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003317 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003318
Miao Xieb2117a32011-01-05 10:07:28 +00003319 if (list_empty(&fs_devices->alloc_list))
3320 return -ENOSPC;
3321
Arne Jansen73c5de02011-04-12 12:07:57 +02003322 sub_stripes = 1;
3323 dev_stripes = 1;
3324 devs_increment = 1;
3325 ncopies = 1;
3326 devs_max = 0; /* 0 == as many as possible */
3327 devs_min = 1;
3328
3329 /*
3330 * define the properties of each RAID type.
3331 * FIXME: move this to a global table and use it in all RAID
3332 * calculation code
3333 */
3334 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3335 dev_stripes = 2;
3336 ncopies = 2;
3337 devs_max = 1;
3338 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3339 devs_min = 2;
3340 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3341 devs_increment = 2;
3342 ncopies = 2;
3343 devs_max = 2;
3344 devs_min = 2;
3345 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3346 sub_stripes = 2;
3347 devs_increment = 2;
3348 ncopies = 2;
3349 devs_min = 4;
3350 } else {
3351 devs_max = 1;
3352 }
3353
3354 if (type & BTRFS_BLOCK_GROUP_DATA) {
3355 max_stripe_size = 1024 * 1024 * 1024;
3356 max_chunk_size = 10 * max_stripe_size;
3357 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003358 /* for larger filesystems, use larger metadata chunks */
3359 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3360 max_stripe_size = 1024 * 1024 * 1024;
3361 else
3362 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003363 max_chunk_size = max_stripe_size;
3364 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003365 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003366 max_chunk_size = 2 * max_stripe_size;
3367 } else {
3368 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3369 type);
3370 BUG_ON(1);
3371 }
3372
3373 /* we don't want a chunk larger than 10% of writeable space */
3374 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3375 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003376
3377 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3378 GFP_NOFS);
3379 if (!devices_info)
3380 return -ENOMEM;
3381
Arne Jansen73c5de02011-04-12 12:07:57 +02003382 cur = fs_devices->alloc_list.next;
3383
3384 /*
3385 * in the first pass through the devices list, we gather information
3386 * about the available holes on each device.
3387 */
3388 ndevs = 0;
3389 while (cur != &fs_devices->alloc_list) {
3390 struct btrfs_device *device;
3391 u64 max_avail;
3392 u64 dev_offset;
3393
3394 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3395
3396 cur = cur->next;
3397
3398 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003399 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003400 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003401 continue;
3402 }
3403
3404 if (!device->in_fs_metadata)
3405 continue;
3406
3407 if (device->total_bytes > device->bytes_used)
3408 total_avail = device->total_bytes - device->bytes_used;
3409 else
3410 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003411
3412 /* If there is no space on this device, skip it. */
3413 if (total_avail == 0)
3414 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003415
Li Zefan125ccb02011-12-08 15:07:24 +08003416 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003417 max_stripe_size * dev_stripes,
3418 &dev_offset, &max_avail);
3419 if (ret && ret != -ENOSPC)
3420 goto error;
3421
3422 if (ret == 0)
3423 max_avail = max_stripe_size * dev_stripes;
3424
3425 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3426 continue;
3427
3428 devices_info[ndevs].dev_offset = dev_offset;
3429 devices_info[ndevs].max_avail = max_avail;
3430 devices_info[ndevs].total_avail = total_avail;
3431 devices_info[ndevs].dev = device;
3432 ++ndevs;
3433 }
3434
3435 /*
3436 * now sort the devices by hole size / available space
3437 */
3438 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3439 btrfs_cmp_device_info, NULL);
3440
3441 /* round down to number of usable stripes */
3442 ndevs -= ndevs % devs_increment;
3443
3444 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3445 ret = -ENOSPC;
3446 goto error;
3447 }
3448
3449 if (devs_max && ndevs > devs_max)
3450 ndevs = devs_max;
3451 /*
3452 * the primary goal is to maximize the number of stripes, so use as many
3453 * devices as possible, even if the stripes are not maximum sized.
3454 */
3455 stripe_size = devices_info[ndevs-1].max_avail;
3456 num_stripes = ndevs * dev_stripes;
3457
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003458 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003459 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003460 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003461 }
3462
3463 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003464
3465 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003466 do_div(stripe_size, BTRFS_STRIPE_LEN);
3467 stripe_size *= BTRFS_STRIPE_LEN;
3468
Miao Xieb2117a32011-01-05 10:07:28 +00003469 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3470 if (!map) {
3471 ret = -ENOMEM;
3472 goto error;
3473 }
3474 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003475
Arne Jansen73c5de02011-04-12 12:07:57 +02003476 for (i = 0; i < ndevs; ++i) {
3477 for (j = 0; j < dev_stripes; ++j) {
3478 int s = i * dev_stripes + j;
3479 map->stripes[s].dev = devices_info[i].dev;
3480 map->stripes[s].physical = devices_info[i].dev_offset +
3481 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003482 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003483 }
Chris Mason593060d2008-03-25 16:50:33 -04003484 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003485 map->stripe_len = BTRFS_STRIPE_LEN;
3486 map->io_align = BTRFS_STRIPE_LEN;
3487 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003488 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003489 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003490
Yan Zheng2b820322008-11-17 21:11:30 -05003491 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003492 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003493
Arne Jansen73c5de02011-04-12 12:07:57 +02003494 *stripe_size_out = stripe_size;
3495 *num_bytes_out = num_bytes;
3496
3497 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003498
David Sterba172ddd62011-04-21 00:48:27 +02003499 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003500 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003501 ret = -ENOMEM;
3502 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003503 }
Chris Mason0b86a832008-03-24 15:01:56 -04003504 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003505 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003506 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003507 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003508 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003509
Chris Mason0b86a832008-03-24 15:01:56 -04003510 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003511 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003512 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003513 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003514 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003515 if (ret)
3516 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003517
3518 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3519 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003520 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003521 if (ret)
3522 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003523
Arne Jansen73c5de02011-04-12 12:07:57 +02003524 for (i = 0; i < map->num_stripes; ++i) {
3525 struct btrfs_device *device;
3526 u64 dev_offset;
3527
3528 device = map->stripes[i].dev;
3529 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003530
3531 ret = btrfs_alloc_dev_extent(trans, device,
3532 info->chunk_root->root_key.objectid,
3533 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003534 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003535 if (ret) {
3536 btrfs_abort_transaction(trans, extent_root, ret);
3537 goto error;
3538 }
Yan Zheng2b820322008-11-17 21:11:30 -05003539 }
3540
Miao Xieb2117a32011-01-05 10:07:28 +00003541 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003542 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003543
3544error:
3545 kfree(map);
3546 kfree(devices_info);
3547 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003548}
3549
3550static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3551 struct btrfs_root *extent_root,
3552 struct map_lookup *map, u64 chunk_offset,
3553 u64 chunk_size, u64 stripe_size)
3554{
3555 u64 dev_offset;
3556 struct btrfs_key key;
3557 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3558 struct btrfs_device *device;
3559 struct btrfs_chunk *chunk;
3560 struct btrfs_stripe *stripe;
3561 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3562 int index = 0;
3563 int ret;
3564
3565 chunk = kzalloc(item_size, GFP_NOFS);
3566 if (!chunk)
3567 return -ENOMEM;
3568
3569 index = 0;
3570 while (index < map->num_stripes) {
3571 device = map->stripes[index].dev;
3572 device->bytes_used += stripe_size;
3573 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003574 if (ret)
3575 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003576 index++;
3577 }
3578
Josef Bacik2bf64752011-09-26 17:12:22 -04003579 spin_lock(&extent_root->fs_info->free_chunk_lock);
3580 extent_root->fs_info->free_chunk_space -= (stripe_size *
3581 map->num_stripes);
3582 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3583
Yan Zheng2b820322008-11-17 21:11:30 -05003584 index = 0;
3585 stripe = &chunk->stripe;
3586 while (index < map->num_stripes) {
3587 device = map->stripes[index].dev;
3588 dev_offset = map->stripes[index].physical;
3589
3590 btrfs_set_stack_stripe_devid(stripe, device->devid);
3591 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3592 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3593 stripe++;
3594 index++;
3595 }
3596
3597 btrfs_set_stack_chunk_length(chunk, chunk_size);
3598 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3599 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3600 btrfs_set_stack_chunk_type(chunk, map->type);
3601 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3602 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3603 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3604 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3605 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3606
3607 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3608 key.type = BTRFS_CHUNK_ITEM_KEY;
3609 key.offset = chunk_offset;
3610
3611 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003612
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003613 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3614 /*
3615 * TODO: Cleanup of inserted chunk root in case of
3616 * failure.
3617 */
Li Zefan125ccb02011-12-08 15:07:24 +08003618 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003619 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003620 }
liubo1abe9b82011-03-24 11:18:59 +00003621
Mark Fasheh3acd3952011-09-08 17:40:01 -07003622out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003623 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003624 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003625}
3626
3627/*
3628 * Chunk allocation falls into two parts. The first part does works
3629 * that make the new allocated chunk useable, but not do any operation
3630 * that modifies the chunk tree. The second part does the works that
3631 * require modifying the chunk tree. This division is important for the
3632 * bootstrap process of adding storage to a seed btrfs.
3633 */
3634int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3635 struct btrfs_root *extent_root, u64 type)
3636{
3637 u64 chunk_offset;
3638 u64 chunk_size;
3639 u64 stripe_size;
3640 struct map_lookup *map;
3641 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3642 int ret;
3643
3644 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3645 &chunk_offset);
3646 if (ret)
3647 return ret;
3648
3649 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3650 &stripe_size, chunk_offset, type);
3651 if (ret)
3652 return ret;
3653
3654 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3655 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003656 if (ret)
3657 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003658 return 0;
3659}
3660
Chris Masond3977122009-01-05 21:25:51 -05003661static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003662 struct btrfs_root *root,
3663 struct btrfs_device *device)
3664{
3665 u64 chunk_offset;
3666 u64 sys_chunk_offset;
3667 u64 chunk_size;
3668 u64 sys_chunk_size;
3669 u64 stripe_size;
3670 u64 sys_stripe_size;
3671 u64 alloc_profile;
3672 struct map_lookup *map;
3673 struct map_lookup *sys_map;
3674 struct btrfs_fs_info *fs_info = root->fs_info;
3675 struct btrfs_root *extent_root = fs_info->extent_root;
3676 int ret;
3677
3678 ret = find_next_chunk(fs_info->chunk_root,
3679 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003680 if (ret)
3681 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003682
3683 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003684 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003685 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3686
3687 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3688 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003689 if (ret)
3690 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003691
3692 sys_chunk_offset = chunk_offset + chunk_size;
3693
3694 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003695 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003696 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3697
3698 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3699 &sys_chunk_size, &sys_stripe_size,
3700 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06003701 if (ret) {
3702 btrfs_abort_transaction(trans, root, ret);
3703 goto out;
3704 }
Yan Zheng2b820322008-11-17 21:11:30 -05003705
3706 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06003707 if (ret) {
3708 btrfs_abort_transaction(trans, root, ret);
3709 goto out;
3710 }
Yan Zheng2b820322008-11-17 21:11:30 -05003711
3712 /*
3713 * Modifying chunk tree needs allocating new blocks from both
3714 * system block group and metadata block group. So we only can
3715 * do operations require modifying the chunk tree after both
3716 * block groups were created.
3717 */
3718 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3719 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06003720 if (ret) {
3721 btrfs_abort_transaction(trans, root, ret);
3722 goto out;
3723 }
Yan Zheng2b820322008-11-17 21:11:30 -05003724
3725 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3726 sys_chunk_offset, sys_chunk_size,
3727 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003728 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06003729 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003730
David Sterba005d6422012-09-18 07:52:32 -06003731out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003732
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003733 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003734}
3735
3736int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3737{
3738 struct extent_map *em;
3739 struct map_lookup *map;
3740 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3741 int readonly = 0;
3742 int i;
3743
Chris Mason890871b2009-09-02 16:24:52 -04003744 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003745 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003746 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003747 if (!em)
3748 return 1;
3749
Josef Bacikf48b9072010-01-27 02:07:59 +00003750 if (btrfs_test_opt(root, DEGRADED)) {
3751 free_extent_map(em);
3752 return 0;
3753 }
3754
Yan Zheng2b820322008-11-17 21:11:30 -05003755 map = (struct map_lookup *)em->bdev;
3756 for (i = 0; i < map->num_stripes; i++) {
3757 if (!map->stripes[i].dev->writeable) {
3758 readonly = 1;
3759 break;
3760 }
3761 }
3762 free_extent_map(em);
3763 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003764}
3765
3766void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3767{
David Sterbaa8067e02011-04-21 00:34:43 +02003768 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003769}
3770
3771void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3772{
3773 struct extent_map *em;
3774
Chris Masond3977122009-01-05 21:25:51 -05003775 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003776 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003777 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3778 if (em)
3779 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003780 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003781 if (!em)
3782 break;
3783 kfree(em->bdev);
3784 /* once for us */
3785 free_extent_map(em);
3786 /* once for the tree */
3787 free_extent_map(em);
3788 }
3789}
3790
Stefan Behrens5d964052012-11-05 14:59:07 +01003791int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04003792{
Stefan Behrens5d964052012-11-05 14:59:07 +01003793 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04003794 struct extent_map *em;
3795 struct map_lookup *map;
3796 struct extent_map_tree *em_tree = &map_tree->map_tree;
3797 int ret;
3798
Chris Mason890871b2009-09-02 16:24:52 -04003799 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003800 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003801 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003802 BUG_ON(!em);
3803
3804 BUG_ON(em->start > logical || em->start + em->len < logical);
3805 map = (struct map_lookup *)em->bdev;
3806 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3807 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003808 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3809 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003810 else
3811 ret = 1;
3812 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003813 return ret;
3814}
3815
Chris Masondfe25022008-05-13 13:46:40 -04003816static int find_live_mirror(struct map_lookup *map, int first, int num,
3817 int optimal)
3818{
3819 int i;
3820 if (map->stripes[optimal].dev->bdev)
3821 return optimal;
3822 for (i = first; i < first + num; i++) {
3823 if (map->stripes[i].dev->bdev)
3824 return i;
3825 }
3826 /* we couldn't find one that doesn't fail. Just return something
3827 * and the io error handling code will clean up eventually
3828 */
3829 return optimal;
3830}
3831
Stefan Behrens3ec706c2012-11-05 15:46:42 +01003832static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04003833 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003834 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003835 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003836{
3837 struct extent_map *em;
3838 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01003839 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04003840 struct extent_map_tree *em_tree = &map_tree->map_tree;
3841 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003842 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003843 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003844 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003845 u64 stripe_nr_orig;
3846 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003847 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003848 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003849 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003850 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003851 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003852 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003853
Chris Mason890871b2009-09-02 16:24:52 -04003854 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003855 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003856 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003857
Chris Mason3b951512008-04-17 11:29:12 -04003858 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06003859 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003860 (unsigned long long)logical,
3861 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003862 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003863 }
Chris Mason0b86a832008-03-24 15:01:56 -04003864
3865 BUG_ON(em->start > logical || em->start + em->len < logical);
3866 map = (struct map_lookup *)em->bdev;
3867 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003868
Chris Masonf1885912008-04-09 16:28:12 -04003869 if (mirror_num > map->num_stripes)
3870 mirror_num = 0;
3871
Chris Mason593060d2008-03-25 16:50:33 -04003872 stripe_nr = offset;
3873 /*
3874 * stripe_nr counts the total number of stripes we have to stride
3875 * to get to this block
3876 */
3877 do_div(stripe_nr, map->stripe_len);
3878
3879 stripe_offset = stripe_nr * map->stripe_len;
3880 BUG_ON(offset < stripe_offset);
3881
3882 /* stripe_offset is the offset of this block in its stripe*/
3883 stripe_offset = offset - stripe_offset;
3884
Li Dongyangfce3bb92011-03-24 10:24:26 +00003885 if (rw & REQ_DISCARD)
3886 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003887 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003888 /* we limit the length of each bio to what fits in a stripe */
3889 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003890 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003891 } else {
3892 *length = em->len - offset;
3893 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003894
Jan Schmidta1d3c472011-08-04 17:15:33 +02003895 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003896 goto out;
3897
Chris Masonf2d8d742008-04-21 10:03:05 -04003898 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003899 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003900 stripe_nr_orig = stripe_nr;
3901 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3902 (~(map->stripe_len - 1));
3903 do_div(stripe_nr_end, map->stripe_len);
3904 stripe_end_offset = stripe_nr_end * map->stripe_len -
3905 (offset + *length);
3906 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3907 if (rw & REQ_DISCARD)
3908 num_stripes = min_t(u64, map->num_stripes,
3909 stripe_nr_end - stripe_nr_orig);
3910 stripe_index = do_div(stripe_nr, map->num_stripes);
3911 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003912 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003913 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003914 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003915 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003916 else {
3917 stripe_index = find_live_mirror(map, 0,
3918 map->num_stripes,
3919 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003920 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003921 }
Chris Mason2fff7342008-04-29 14:12:09 -04003922
Chris Mason611f0e02008-04-03 16:29:03 -04003923 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003924 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003925 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003926 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003927 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003928 } else {
3929 mirror_num = 1;
3930 }
Chris Mason2fff7342008-04-29 14:12:09 -04003931
Chris Mason321aecc2008-04-16 10:49:51 -04003932 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3933 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003934
3935 stripe_index = do_div(stripe_nr, factor);
3936 stripe_index *= map->sub_stripes;
3937
Jens Axboe7eaceac2011-03-10 08:52:07 +01003938 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003939 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003940 else if (rw & REQ_DISCARD)
3941 num_stripes = min_t(u64, map->sub_stripes *
3942 (stripe_nr_end - stripe_nr_orig),
3943 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003944 else if (mirror_num)
3945 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003946 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003947 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003948 stripe_index = find_live_mirror(map, stripe_index,
3949 map->sub_stripes, stripe_index +
3950 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003951 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003952 }
Chris Mason8790d502008-04-03 16:29:03 -04003953 } else {
3954 /*
3955 * after this do_div call, stripe_nr is the number of stripes
3956 * on this device we have to walk to find the data, and
3957 * stripe_index is the number of our device in the stripe array
3958 */
3959 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003960 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003961 }
Chris Mason593060d2008-03-25 16:50:33 -04003962 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003963
Li Zefande11cc12011-12-01 12:55:47 +08003964 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3965 if (!bbio) {
3966 ret = -ENOMEM;
3967 goto out;
3968 }
3969 atomic_set(&bbio->error, 0);
3970
Li Dongyangfce3bb92011-03-24 10:24:26 +00003971 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003972 int factor = 0;
3973 int sub_stripes = 0;
3974 u64 stripes_per_dev = 0;
3975 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003976 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003977
3978 if (map->type &
3979 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3980 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3981 sub_stripes = 1;
3982 else
3983 sub_stripes = map->sub_stripes;
3984
3985 factor = map->num_stripes / sub_stripes;
3986 stripes_per_dev = div_u64_rem(stripe_nr_end -
3987 stripe_nr_orig,
3988 factor,
3989 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003990 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3991 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003992 }
3993
Li Dongyangfce3bb92011-03-24 10:24:26 +00003994 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003995 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003996 map->stripes[stripe_index].physical +
3997 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003998 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003999
Li Zefanec9ef7a2011-12-01 14:06:42 +08004000 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4001 BTRFS_BLOCK_GROUP_RAID10)) {
4002 bbio->stripes[i].length = stripes_per_dev *
4003 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004004
Li Zefanec9ef7a2011-12-01 14:06:42 +08004005 if (i / sub_stripes < remaining_stripes)
4006 bbio->stripes[i].length +=
4007 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004008
4009 /*
4010 * Special for the first stripe and
4011 * the last stripe:
4012 *
4013 * |-------|...|-------|
4014 * |----------|
4015 * off end_off
4016 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004017 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004018 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004019 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004020
4021 if (stripe_index >= last_stripe &&
4022 stripe_index <= (last_stripe +
4023 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004024 bbio->stripes[i].length -=
4025 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004026
Li Zefanec9ef7a2011-12-01 14:06:42 +08004027 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004028 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004029 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004030 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004031
4032 stripe_index++;
4033 if (stripe_index == map->num_stripes) {
4034 /* This could only happen for RAID0/10 */
4035 stripe_index = 0;
4036 stripe_nr++;
4037 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004038 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004039 } else {
4040 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004041 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004042 map->stripes[stripe_index].physical +
4043 stripe_offset +
4044 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004045 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004046 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004047 stripe_index++;
4048 }
Chris Mason593060d2008-03-25 16:50:33 -04004049 }
Li Zefande11cc12011-12-01 12:55:47 +08004050
4051 if (rw & REQ_WRITE) {
4052 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4053 BTRFS_BLOCK_GROUP_RAID10 |
4054 BTRFS_BLOCK_GROUP_DUP)) {
4055 max_errors = 1;
4056 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004057 }
Li Zefande11cc12011-12-01 12:55:47 +08004058
4059 *bbio_ret = bbio;
4060 bbio->num_stripes = num_stripes;
4061 bbio->max_errors = max_errors;
4062 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04004063out:
Chris Mason0b86a832008-03-24 15:01:56 -04004064 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004065 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004066}
4067
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004068int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004069 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004070 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004071{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004072 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004073 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04004074}
4075
Yan Zhenga512bbf2008-12-08 16:46:26 -05004076int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4077 u64 chunk_start, u64 physical, u64 devid,
4078 u64 **logical, int *naddrs, int *stripe_len)
4079{
4080 struct extent_map_tree *em_tree = &map_tree->map_tree;
4081 struct extent_map *em;
4082 struct map_lookup *map;
4083 u64 *buf;
4084 u64 bytenr;
4085 u64 length;
4086 u64 stripe_nr;
4087 int i, j, nr = 0;
4088
Chris Mason890871b2009-09-02 16:24:52 -04004089 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004090 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004091 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004092
4093 BUG_ON(!em || em->start != chunk_start);
4094 map = (struct map_lookup *)em->bdev;
4095
4096 length = em->len;
4097 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4098 do_div(length, map->num_stripes / map->sub_stripes);
4099 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4100 do_div(length, map->num_stripes);
4101
4102 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004104
4105 for (i = 0; i < map->num_stripes; i++) {
4106 if (devid && map->stripes[i].dev->devid != devid)
4107 continue;
4108 if (map->stripes[i].physical > physical ||
4109 map->stripes[i].physical + length <= physical)
4110 continue;
4111
4112 stripe_nr = physical - map->stripes[i].physical;
4113 do_div(stripe_nr, map->stripe_len);
4114
4115 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4116 stripe_nr = stripe_nr * map->num_stripes + i;
4117 do_div(stripe_nr, map->sub_stripes);
4118 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4119 stripe_nr = stripe_nr * map->num_stripes + i;
4120 }
4121 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004122 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004123 for (j = 0; j < nr; j++) {
4124 if (buf[j] == bytenr)
4125 break;
4126 }
Chris Mason934d3752008-12-08 16:43:10 -05004127 if (j == nr) {
4128 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004129 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004130 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004131 }
4132
Yan Zhenga512bbf2008-12-08 16:46:26 -05004133 *logical = buf;
4134 *naddrs = nr;
4135 *stripe_len = map->stripe_len;
4136
4137 free_extent_map(em);
4138 return 0;
4139}
4140
Stefan Behrens442a4f62012-05-25 16:06:08 +02004141static void *merge_stripe_index_into_bio_private(void *bi_private,
4142 unsigned int stripe_index)
4143{
4144 /*
4145 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4146 * at most 1.
4147 * The alternative solution (instead of stealing bits from the
4148 * pointer) would be to allocate an intermediate structure
4149 * that contains the old private pointer plus the stripe_index.
4150 */
4151 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4152 BUG_ON(stripe_index > 3);
4153 return (void *)(((uintptr_t)bi_private) | stripe_index);
4154}
4155
4156static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4157{
4158 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4159}
4160
4161static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4162{
4163 return (unsigned int)((uintptr_t)bi_private) & 3;
4164}
4165
Jan Schmidta1d3c472011-08-04 17:15:33 +02004166static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004167{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004168 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004169 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004170
Stefan Behrens442a4f62012-05-25 16:06:08 +02004171 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004172 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004173 if (err == -EIO || err == -EREMOTEIO) {
4174 unsigned int stripe_index =
4175 extract_stripe_index_from_bio_private(
4176 bio->bi_private);
4177 struct btrfs_device *dev;
4178
4179 BUG_ON(stripe_index >= bbio->num_stripes);
4180 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004181 if (dev->bdev) {
4182 if (bio->bi_rw & WRITE)
4183 btrfs_dev_stat_inc(dev,
4184 BTRFS_DEV_STAT_WRITE_ERRS);
4185 else
4186 btrfs_dev_stat_inc(dev,
4187 BTRFS_DEV_STAT_READ_ERRS);
4188 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4189 btrfs_dev_stat_inc(dev,
4190 BTRFS_DEV_STAT_FLUSH_ERRS);
4191 btrfs_dev_stat_print_on_error(dev);
4192 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004193 }
4194 }
Chris Mason8790d502008-04-03 16:29:03 -04004195
Jan Schmidta1d3c472011-08-04 17:15:33 +02004196 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004197 is_orig_bio = 1;
4198
Jan Schmidta1d3c472011-08-04 17:15:33 +02004199 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004200 if (!is_orig_bio) {
4201 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004202 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004203 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004204 bio->bi_private = bbio->private;
4205 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004206 bio->bi_bdev = (struct block_device *)
4207 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004208 /* only send an error to the higher layers if it is
4209 * beyond the tolerance of the multi-bio
4210 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004211 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004212 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004213 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004214 /*
4215 * this bio is actually up to date, we didn't
4216 * go over the max number of errors
4217 */
4218 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004219 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004220 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004221 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004222
4223 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004224 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004225 bio_put(bio);
4226 }
Chris Mason8790d502008-04-03 16:29:03 -04004227}
4228
Chris Mason8b712842008-06-11 16:50:36 -04004229struct async_sched {
4230 struct bio *bio;
4231 int rw;
4232 struct btrfs_fs_info *info;
4233 struct btrfs_work work;
4234};
4235
4236/*
4237 * see run_scheduled_bios for a description of why bios are collected for
4238 * async submit.
4239 *
4240 * This will add one bio to the pending list for a device and make sure
4241 * the work struct is scheduled.
4242 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004243static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004244 struct btrfs_device *device,
4245 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004246{
4247 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004248 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004249
4250 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004251 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004252 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004253 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004254 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004255 return;
Chris Mason8b712842008-06-11 16:50:36 -04004256 }
4257
4258 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004259 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004260 * higher layers. Otherwise, the async bio makes it appear we have
4261 * made progress against dirty pages when we've really just put it
4262 * on a queue for later
4263 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004264 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004265 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004266 bio->bi_next = NULL;
4267 bio->bi_rw |= rw;
4268
4269 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004270 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004271 pending_bios = &device->pending_sync_bios;
4272 else
4273 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004274
Chris Masonffbd5172009-04-20 15:50:09 -04004275 if (pending_bios->tail)
4276 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004277
Chris Masonffbd5172009-04-20 15:50:09 -04004278 pending_bios->tail = bio;
4279 if (!pending_bios->head)
4280 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004281 if (device->running_pending)
4282 should_queue = 0;
4283
4284 spin_unlock(&device->io_lock);
4285
4286 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004287 btrfs_queue_worker(&root->fs_info->submit_workers,
4288 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004289}
4290
Josef Bacikde1ee922012-10-19 16:50:56 -04004291static int bio_size_ok(struct block_device *bdev, struct bio *bio,
4292 sector_t sector)
4293{
4294 struct bio_vec *prev;
4295 struct request_queue *q = bdev_get_queue(bdev);
4296 unsigned short max_sectors = queue_max_sectors(q);
4297 struct bvec_merge_data bvm = {
4298 .bi_bdev = bdev,
4299 .bi_sector = sector,
4300 .bi_rw = bio->bi_rw,
4301 };
4302
4303 if (bio->bi_vcnt == 0) {
4304 WARN_ON(1);
4305 return 1;
4306 }
4307
4308 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
4309 if ((bio->bi_size >> 9) > max_sectors)
4310 return 0;
4311
4312 if (!q->merge_bvec_fn)
4313 return 1;
4314
4315 bvm.bi_size = bio->bi_size - prev->bv_len;
4316 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
4317 return 0;
4318 return 1;
4319}
4320
4321static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4322 struct bio *bio, u64 physical, int dev_nr,
4323 int rw, int async)
4324{
4325 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
4326
4327 bio->bi_private = bbio;
4328 bio->bi_private = merge_stripe_index_into_bio_private(
4329 bio->bi_private, (unsigned int)dev_nr);
4330 bio->bi_end_io = btrfs_end_bio;
4331 bio->bi_sector = physical >> 9;
4332#ifdef DEBUG
4333 {
4334 struct rcu_string *name;
4335
4336 rcu_read_lock();
4337 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00004338 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04004339 "(%s id %llu), size=%u\n", rw,
4340 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4341 name->str, dev->devid, bio->bi_size);
4342 rcu_read_unlock();
4343 }
4344#endif
4345 bio->bi_bdev = dev->bdev;
4346 if (async)
4347 schedule_bio(root, dev, rw, bio);
4348 else
4349 btrfsic_submit_bio(rw, bio);
4350}
4351
4352static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4353 struct bio *first_bio, struct btrfs_device *dev,
4354 int dev_nr, int rw, int async)
4355{
4356 struct bio_vec *bvec = first_bio->bi_io_vec;
4357 struct bio *bio;
4358 int nr_vecs = bio_get_nr_vecs(dev->bdev);
4359 u64 physical = bbio->stripes[dev_nr].physical;
4360
4361again:
4362 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
4363 if (!bio)
4364 return -ENOMEM;
4365
4366 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
4367 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
4368 bvec->bv_offset) < bvec->bv_len) {
4369 u64 len = bio->bi_size;
4370
4371 atomic_inc(&bbio->stripes_pending);
4372 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
4373 rw, async);
4374 physical += len;
4375 goto again;
4376 }
4377 bvec++;
4378 }
4379
4380 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
4381 return 0;
4382}
4383
4384static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
4385{
4386 atomic_inc(&bbio->error);
4387 if (atomic_dec_and_test(&bbio->stripes_pending)) {
4388 bio->bi_private = bbio->private;
4389 bio->bi_end_io = bbio->end_io;
4390 bio->bi_bdev = (struct block_device *)
4391 (unsigned long)bbio->mirror_num;
4392 bio->bi_sector = logical >> 9;
4393 kfree(bbio);
4394 bio_endio(bio, -EIO);
4395 }
4396}
4397
Chris Masonf1885912008-04-09 16:28:12 -04004398int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004399 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004400{
Chris Mason0b86a832008-03-24 15:01:56 -04004401 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004402 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004403 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004404 u64 length = 0;
4405 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004406 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004407 int dev_nr = 0;
4408 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004409 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004410
Chris Masonf2d8d742008-04-21 10:03:05 -04004411 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004412 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004413
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004414 ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004415 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004416 if (ret) /* -ENOMEM */
4417 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004418
Jan Schmidta1d3c472011-08-04 17:15:33 +02004419 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004420 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004421 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05004422 "len %llu\n", (unsigned long long)logical,
4423 (unsigned long long)length,
4424 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004425 BUG();
4426 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004427
4428 bbio->orig_bio = first_bio;
4429 bbio->private = first_bio->bi_private;
4430 bbio->end_io = first_bio->bi_end_io;
4431 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004432
Chris Masond3977122009-01-05 21:25:51 -05004433 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04004434 dev = bbio->stripes[dev_nr].dev;
4435 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
4436 bbio_error(bbio, first_bio, logical);
4437 dev_nr++;
4438 continue;
4439 }
4440
4441 /*
4442 * Check and see if we're ok with this bio based on it's size
4443 * and offset with the given device.
4444 */
4445 if (!bio_size_ok(dev->bdev, first_bio,
4446 bbio->stripes[dev_nr].physical >> 9)) {
4447 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
4448 dev_nr, rw, async_submit);
4449 BUG_ON(ret);
4450 dev_nr++;
4451 continue;
4452 }
4453
Jan Schmidta1d3c472011-08-04 17:15:33 +02004454 if (dev_nr < total_devs - 1) {
4455 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004456 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004457 } else {
4458 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004459 }
Josef Bacik606686e2012-06-04 14:03:51 -04004460
Josef Bacikde1ee922012-10-19 16:50:56 -04004461 submit_stripe_bio(root, bbio, bio,
4462 bbio->stripes[dev_nr].physical, dev_nr, rw,
4463 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04004464 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004465 }
Chris Mason0b86a832008-03-24 15:01:56 -04004466 return 0;
4467}
4468
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004469struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004470 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004471{
Yan Zheng2b820322008-11-17 21:11:30 -05004472 struct btrfs_device *device;
4473 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004474
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004475 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004476 while (cur_devices) {
4477 if (!fsid ||
4478 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4479 device = __find_device(&cur_devices->devices,
4480 devid, uuid);
4481 if (device)
4482 return device;
4483 }
4484 cur_devices = cur_devices->seed;
4485 }
4486 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004487}
4488
Chris Masondfe25022008-05-13 13:46:40 -04004489static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4490 u64 devid, u8 *dev_uuid)
4491{
4492 struct btrfs_device *device;
4493 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4494
4495 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004496 if (!device)
4497 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004498 list_add(&device->dev_list,
4499 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004500 device->dev_root = root->fs_info->dev_root;
4501 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004502 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004503 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004504 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004505 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004506 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004507 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004508 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004509 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4510 return device;
4511}
4512
Chris Mason0b86a832008-03-24 15:01:56 -04004513static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4514 struct extent_buffer *leaf,
4515 struct btrfs_chunk *chunk)
4516{
4517 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4518 struct map_lookup *map;
4519 struct extent_map *em;
4520 u64 logical;
4521 u64 length;
4522 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004523 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004524 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004525 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004526 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004527
Chris Masone17cade2008-04-15 15:41:47 -04004528 logical = key->offset;
4529 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004530
Chris Mason890871b2009-09-02 16:24:52 -04004531 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004532 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004533 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004534
4535 /* already mapped? */
4536 if (em && em->start <= logical && em->start + em->len > logical) {
4537 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004538 return 0;
4539 } else if (em) {
4540 free_extent_map(em);
4541 }
Chris Mason0b86a832008-03-24 15:01:56 -04004542
David Sterba172ddd62011-04-21 00:48:27 +02004543 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004544 if (!em)
4545 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004546 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4547 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004548 if (!map) {
4549 free_extent_map(em);
4550 return -ENOMEM;
4551 }
4552
4553 em->bdev = (struct block_device *)map;
4554 em->start = logical;
4555 em->len = length;
4556 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004557 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004558
Chris Mason593060d2008-03-25 16:50:33 -04004559 map->num_stripes = num_stripes;
4560 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4561 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4562 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4563 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4564 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004565 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004566 for (i = 0; i < num_stripes; i++) {
4567 map->stripes[i].physical =
4568 btrfs_stripe_offset_nr(leaf, chunk, i);
4569 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004570 read_extent_buffer(leaf, uuid, (unsigned long)
4571 btrfs_stripe_dev_uuid_nr(chunk, i),
4572 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004573 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
4574 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004575 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004576 kfree(map);
4577 free_extent_map(em);
4578 return -EIO;
4579 }
Chris Masondfe25022008-05-13 13:46:40 -04004580 if (!map->stripes[i].dev) {
4581 map->stripes[i].dev =
4582 add_missing_dev(root, devid, uuid);
4583 if (!map->stripes[i].dev) {
4584 kfree(map);
4585 free_extent_map(em);
4586 return -EIO;
4587 }
4588 }
4589 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004590 }
4591
Chris Mason890871b2009-09-02 16:24:52 -04004592 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004593 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004594 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004595 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004596 free_extent_map(em);
4597
4598 return 0;
4599}
4600
Jeff Mahoney143bede2012-03-01 14:56:26 +01004601static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004602 struct btrfs_dev_item *dev_item,
4603 struct btrfs_device *device)
4604{
4605 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004606
4607 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004608 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4609 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004610 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4611 device->type = btrfs_device_type(leaf, dev_item);
4612 device->io_align = btrfs_device_io_align(leaf, dev_item);
4613 device->io_width = btrfs_device_io_width(leaf, dev_item);
4614 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004615
4616 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004617 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004618}
4619
Yan Zheng2b820322008-11-17 21:11:30 -05004620static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4621{
4622 struct btrfs_fs_devices *fs_devices;
4623 int ret;
4624
Li Zefanb367e472011-12-07 11:38:24 +08004625 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004626
4627 fs_devices = root->fs_info->fs_devices->seed;
4628 while (fs_devices) {
4629 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4630 ret = 0;
4631 goto out;
4632 }
4633 fs_devices = fs_devices->seed;
4634 }
4635
4636 fs_devices = find_fsid(fsid);
4637 if (!fs_devices) {
4638 ret = -ENOENT;
4639 goto out;
4640 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004641
4642 fs_devices = clone_fs_devices(fs_devices);
4643 if (IS_ERR(fs_devices)) {
4644 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004645 goto out;
4646 }
4647
Christoph Hellwig97288f22008-12-02 06:36:09 -05004648 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004649 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004650 if (ret) {
4651 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004652 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004653 }
Yan Zheng2b820322008-11-17 21:11:30 -05004654
4655 if (!fs_devices->seeding) {
4656 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004657 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004658 ret = -EINVAL;
4659 goto out;
4660 }
4661
4662 fs_devices->seed = root->fs_info->fs_devices->seed;
4663 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004664out:
Yan Zheng2b820322008-11-17 21:11:30 -05004665 return ret;
4666}
4667
Chris Mason0d81ba52008-03-24 15:02:07 -04004668static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004669 struct extent_buffer *leaf,
4670 struct btrfs_dev_item *dev_item)
4671{
4672 struct btrfs_device *device;
4673 u64 devid;
4674 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004675 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004676 u8 dev_uuid[BTRFS_UUID_SIZE];
4677
Chris Mason0b86a832008-03-24 15:01:56 -04004678 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004679 read_extent_buffer(leaf, dev_uuid,
4680 (unsigned long)btrfs_device_uuid(dev_item),
4681 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004682 read_extent_buffer(leaf, fs_uuid,
4683 (unsigned long)btrfs_device_fsid(dev_item),
4684 BTRFS_UUID_SIZE);
4685
4686 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4687 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004688 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004689 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004690 }
4691
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004692 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05004693 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004694 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004695 return -EIO;
4696
4697 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004698 printk(KERN_WARNING "warning devid %llu missing\n",
4699 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004700 device = add_missing_dev(root, devid, dev_uuid);
4701 if (!device)
4702 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004703 } else if (!device->missing) {
4704 /*
4705 * this happens when a device that was properly setup
4706 * in the device info lists suddenly goes bad.
4707 * device->bdev is NULL, and so we have to set
4708 * device->missing to one here
4709 */
4710 root->fs_info->fs_devices->missing_devices++;
4711 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004712 }
4713 }
4714
4715 if (device->fs_devices != root->fs_info->fs_devices) {
4716 BUG_ON(device->writeable);
4717 if (device->generation !=
4718 btrfs_device_generation(leaf, dev_item))
4719 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004720 }
Chris Mason0b86a832008-03-24 15:01:56 -04004721
4722 fill_device_from_item(leaf, dev_item, device);
4723 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004724 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004725 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004726 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004727 spin_lock(&root->fs_info->free_chunk_lock);
4728 root->fs_info->free_chunk_space += device->total_bytes -
4729 device->bytes_used;
4730 spin_unlock(&root->fs_info->free_chunk_lock);
4731 }
Chris Mason0b86a832008-03-24 15:01:56 -04004732 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004733 return ret;
4734}
4735
Yan Zhenge4404d62008-12-12 10:03:26 -05004736int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004737{
David Sterba6c417612011-04-13 15:41:04 +02004738 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004739 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004740 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004741 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004742 u8 *ptr;
4743 unsigned long sb_ptr;
4744 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004745 u32 num_stripes;
4746 u32 array_size;
4747 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004748 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004749 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004750
Yan Zhenge4404d62008-12-12 10:03:26 -05004751 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004752 BTRFS_SUPER_INFO_SIZE);
4753 if (!sb)
4754 return -ENOMEM;
4755 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004756 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004757 /*
4758 * The sb extent buffer is artifical and just used to read the system array.
4759 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4760 * pages up-to-date when the page is larger: extent does not cover the
4761 * whole page and consequently check_page_uptodate does not find all
4762 * the page's extents up-to-date (the hole beyond sb),
4763 * write_extent_buffer then triggers a WARN_ON.
4764 *
4765 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4766 * but sb spans only this function. Add an explicit SetPageUptodate call
4767 * to silence the warning eg. on PowerPC 64.
4768 */
4769 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004770 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004771
Chris Masona061fc82008-05-07 11:43:44 -04004772 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004773 array_size = btrfs_super_sys_array_size(super_copy);
4774
Chris Mason0b86a832008-03-24 15:01:56 -04004775 ptr = super_copy->sys_chunk_array;
4776 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4777 cur = 0;
4778
4779 while (cur < array_size) {
4780 disk_key = (struct btrfs_disk_key *)ptr;
4781 btrfs_disk_key_to_cpu(&key, disk_key);
4782
Chris Masona061fc82008-05-07 11:43:44 -04004783 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004784 sb_ptr += len;
4785 cur += len;
4786
Chris Mason0d81ba52008-03-24 15:02:07 -04004787 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004788 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004789 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004790 if (ret)
4791 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004792 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4793 len = btrfs_chunk_item_size(num_stripes);
4794 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004795 ret = -EIO;
4796 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004797 }
4798 ptr += len;
4799 sb_ptr += len;
4800 cur += len;
4801 }
Chris Masona061fc82008-05-07 11:43:44 -04004802 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004803 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004804}
4805
4806int btrfs_read_chunk_tree(struct btrfs_root *root)
4807{
4808 struct btrfs_path *path;
4809 struct extent_buffer *leaf;
4810 struct btrfs_key key;
4811 struct btrfs_key found_key;
4812 int ret;
4813 int slot;
4814
4815 root = root->fs_info->chunk_root;
4816
4817 path = btrfs_alloc_path();
4818 if (!path)
4819 return -ENOMEM;
4820
Li Zefanb367e472011-12-07 11:38:24 +08004821 mutex_lock(&uuid_mutex);
4822 lock_chunks(root);
4823
Chris Mason0b86a832008-03-24 15:01:56 -04004824 /* first we search for all of the device items, and then we
4825 * read in all of the chunk items. This way we can create chunk
4826 * mappings that reference all of the devices that are afound
4827 */
4828 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4829 key.offset = 0;
4830 key.type = 0;
4831again:
4832 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004833 if (ret < 0)
4834 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004835 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004836 leaf = path->nodes[0];
4837 slot = path->slots[0];
4838 if (slot >= btrfs_header_nritems(leaf)) {
4839 ret = btrfs_next_leaf(root, path);
4840 if (ret == 0)
4841 continue;
4842 if (ret < 0)
4843 goto error;
4844 break;
4845 }
4846 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4847 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4848 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4849 break;
4850 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4851 struct btrfs_dev_item *dev_item;
4852 dev_item = btrfs_item_ptr(leaf, slot,
4853 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004854 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004855 if (ret)
4856 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004857 }
4858 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4859 struct btrfs_chunk *chunk;
4860 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4861 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004862 if (ret)
4863 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004864 }
4865 path->slots[0]++;
4866 }
4867 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4868 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004869 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004870 goto again;
4871 }
Chris Mason0b86a832008-03-24 15:01:56 -04004872 ret = 0;
4873error:
Li Zefanb367e472011-12-07 11:38:24 +08004874 unlock_chunks(root);
4875 mutex_unlock(&uuid_mutex);
4876
Yan Zheng2b820322008-11-17 21:11:30 -05004877 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004878 return ret;
4879}
Stefan Behrens442a4f62012-05-25 16:06:08 +02004880
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004881static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
4882{
4883 int i;
4884
4885 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4886 btrfs_dev_stat_reset(dev, i);
4887}
4888
4889int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
4890{
4891 struct btrfs_key key;
4892 struct btrfs_key found_key;
4893 struct btrfs_root *dev_root = fs_info->dev_root;
4894 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4895 struct extent_buffer *eb;
4896 int slot;
4897 int ret = 0;
4898 struct btrfs_device *device;
4899 struct btrfs_path *path = NULL;
4900 int i;
4901
4902 path = btrfs_alloc_path();
4903 if (!path) {
4904 ret = -ENOMEM;
4905 goto out;
4906 }
4907
4908 mutex_lock(&fs_devices->device_list_mutex);
4909 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4910 int item_size;
4911 struct btrfs_dev_stats_item *ptr;
4912
4913 key.objectid = 0;
4914 key.type = BTRFS_DEV_STATS_KEY;
4915 key.offset = device->devid;
4916 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
4917 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004918 __btrfs_reset_dev_stats(device);
4919 device->dev_stats_valid = 1;
4920 btrfs_release_path(path);
4921 continue;
4922 }
4923 slot = path->slots[0];
4924 eb = path->nodes[0];
4925 btrfs_item_key_to_cpu(eb, &found_key, slot);
4926 item_size = btrfs_item_size_nr(eb, slot);
4927
4928 ptr = btrfs_item_ptr(eb, slot,
4929 struct btrfs_dev_stats_item);
4930
4931 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4932 if (item_size >= (1 + i) * sizeof(__le64))
4933 btrfs_dev_stat_set(device, i,
4934 btrfs_dev_stats_value(eb, ptr, i));
4935 else
4936 btrfs_dev_stat_reset(device, i);
4937 }
4938
4939 device->dev_stats_valid = 1;
4940 btrfs_dev_stat_print_on_load(device);
4941 btrfs_release_path(path);
4942 }
4943 mutex_unlock(&fs_devices->device_list_mutex);
4944
4945out:
4946 btrfs_free_path(path);
4947 return ret < 0 ? ret : 0;
4948}
4949
4950static int update_dev_stat_item(struct btrfs_trans_handle *trans,
4951 struct btrfs_root *dev_root,
4952 struct btrfs_device *device)
4953{
4954 struct btrfs_path *path;
4955 struct btrfs_key key;
4956 struct extent_buffer *eb;
4957 struct btrfs_dev_stats_item *ptr;
4958 int ret;
4959 int i;
4960
4961 key.objectid = 0;
4962 key.type = BTRFS_DEV_STATS_KEY;
4963 key.offset = device->devid;
4964
4965 path = btrfs_alloc_path();
4966 BUG_ON(!path);
4967 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
4968 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004969 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
4970 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004971 goto out;
4972 }
4973
4974 if (ret == 0 &&
4975 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
4976 /* need to delete old one and insert a new one */
4977 ret = btrfs_del_item(trans, dev_root, path);
4978 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004979 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
4980 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004981 goto out;
4982 }
4983 ret = 1;
4984 }
4985
4986 if (ret == 1) {
4987 /* need to insert a new item */
4988 btrfs_release_path(path);
4989 ret = btrfs_insert_empty_item(trans, dev_root, path,
4990 &key, sizeof(*ptr));
4991 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004992 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
4993 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004994 goto out;
4995 }
4996 }
4997
4998 eb = path->nodes[0];
4999 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5000 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5001 btrfs_set_dev_stats_value(eb, ptr, i,
5002 btrfs_dev_stat_read(device, i));
5003 btrfs_mark_buffer_dirty(eb);
5004
5005out:
5006 btrfs_free_path(path);
5007 return ret;
5008}
5009
5010/*
5011 * called from commit_transaction. Writes all changed device stats to disk.
5012 */
5013int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5014 struct btrfs_fs_info *fs_info)
5015{
5016 struct btrfs_root *dev_root = fs_info->dev_root;
5017 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5018 struct btrfs_device *device;
5019 int ret = 0;
5020
5021 mutex_lock(&fs_devices->device_list_mutex);
5022 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5023 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5024 continue;
5025
5026 ret = update_dev_stat_item(trans, dev_root, device);
5027 if (!ret)
5028 device->dev_stats_dirty = 0;
5029 }
5030 mutex_unlock(&fs_devices->device_list_mutex);
5031
5032 return ret;
5033}
5034
Stefan Behrens442a4f62012-05-25 16:06:08 +02005035void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5036{
5037 btrfs_dev_stat_inc(dev, index);
5038 btrfs_dev_stat_print_on_error(dev);
5039}
5040
5041void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5042{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005043 if (!dev->dev_stats_valid)
5044 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005045 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005046 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005047 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005048 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5049 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5050 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5051 btrfs_dev_stat_read(dev,
5052 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5053 btrfs_dev_stat_read(dev,
5054 BTRFS_DEV_STAT_GENERATION_ERRS));
5055}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005056
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005057static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5058{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005059 int i;
5060
5061 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5062 if (btrfs_dev_stat_read(dev, i) != 0)
5063 break;
5064 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5065 return; /* all values == 0, suppress message */
5066
Josef Bacik606686e2012-06-04 14:03:51 -04005067 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5068 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005069 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5070 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5071 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5072 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5073 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5074}
5075
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005076int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005077 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005078{
5079 struct btrfs_device *dev;
5080 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5081 int i;
5082
5083 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005084 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005085 mutex_unlock(&fs_devices->device_list_mutex);
5086
5087 if (!dev) {
5088 printk(KERN_WARNING
5089 "btrfs: get dev_stats failed, device not found\n");
5090 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005091 } else if (!dev->dev_stats_valid) {
5092 printk(KERN_WARNING
5093 "btrfs: get dev_stats failed, not yet valid\n");
5094 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005095 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005096 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5097 if (stats->nr_items > i)
5098 stats->values[i] =
5099 btrfs_dev_stat_read_and_reset(dev, i);
5100 else
5101 btrfs_dev_stat_reset(dev, i);
5102 }
5103 } else {
5104 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5105 if (stats->nr_items > i)
5106 stats->values[i] = btrfs_dev_stat_read(dev, i);
5107 }
5108 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5109 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5110 return 0;
5111}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005112
5113int btrfs_scratch_superblock(struct btrfs_device *device)
5114{
5115 struct buffer_head *bh;
5116 struct btrfs_super_block *disk_super;
5117
5118 bh = btrfs_read_dev_super(device->bdev);
5119 if (!bh)
5120 return -EINVAL;
5121 disk_super = (struct btrfs_super_block *)bh->b_data;
5122
5123 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5124 set_buffer_dirty(bh);
5125 sync_dirty_buffer(bh);
5126 brelse(bh);
5127
5128 return 0;
5129}