blob: 5777e6a9aab17f3c0e9b2d5c620ed4817dd3dac6 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050028#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040029#include "ctree.h"
30#include "extent_map.h"
31#include "disk-io.h"
32#include "transaction.h"
33#include "print-tree.h"
34#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040035#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010036#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040037#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060038#include "math.h"
Chris Mason0b86a832008-03-24 15:01:56 -040039
Yan Zheng2b820322008-11-17 21:11:30 -050040static int init_first_rw_device(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 struct btrfs_device *device);
43static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020044static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
45static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050046
Chris Mason8a4b83c2008-03-24 15:02:07 -040047static DEFINE_MUTEX(uuid_mutex);
48static LIST_HEAD(fs_uuids);
49
Chris Mason7d9eb122008-07-08 14:19:17 -040050static void lock_chunks(struct btrfs_root *root)
51{
Chris Mason7d9eb122008-07-08 14:19:17 -040052 mutex_lock(&root->fs_info->chunk_mutex);
53}
54
55static void unlock_chunks(struct btrfs_root *root)
56{
Chris Mason7d9eb122008-07-08 14:19:17 -040057 mutex_unlock(&root->fs_info->chunk_mutex);
58}
59
Yan Zhenge4404d62008-12-12 10:03:26 -050060static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
61{
62 struct btrfs_device *device;
63 WARN_ON(fs_devices->opened);
64 while (!list_empty(&fs_devices->devices)) {
65 device = list_entry(fs_devices->devices.next,
66 struct btrfs_device, dev_list);
67 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040068 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050069 kfree(device);
70 }
71 kfree(fs_devices);
72}
73
Jeff Mahoney143bede2012-03-01 14:56:26 +010074void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040075{
76 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040077
Yan Zheng2b820322008-11-17 21:11:30 -050078 while (!list_empty(&fs_uuids)) {
79 fs_devices = list_entry(fs_uuids.next,
80 struct btrfs_fs_devices, list);
81 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050082 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040083 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040084}
85
Chris Masona1b32a52008-09-05 16:09:51 -040086static noinline struct btrfs_device *__find_device(struct list_head *head,
87 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040088{
89 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040090
Qinghuang Fengc6e30872009-01-21 10:59:08 -050091 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040092 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040093 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040094 return dev;
Chris Masona4437552008-04-18 10:29:38 -040095 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040096 }
97 return NULL;
98}
99
Chris Masona1b32a52008-09-05 16:09:51 -0400100static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 struct btrfs_fs_devices *fs_devices;
103
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500104 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
106 return fs_devices;
107 }
108 return NULL;
109}
110
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100111static int
112btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
113 int flush, struct block_device **bdev,
114 struct buffer_head **bh)
115{
116 int ret;
117
118 *bdev = blkdev_get_by_path(device_path, flags, holder);
119
120 if (IS_ERR(*bdev)) {
121 ret = PTR_ERR(*bdev);
122 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
123 goto error;
124 }
125
126 if (flush)
127 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
128 ret = set_blocksize(*bdev, 4096);
129 if (ret) {
130 blkdev_put(*bdev, flags);
131 goto error;
132 }
133 invalidate_bdev(*bdev);
134 *bh = btrfs_read_dev_super(*bdev);
135 if (!*bh) {
136 ret = -EINVAL;
137 blkdev_put(*bdev, flags);
138 goto error;
139 }
140
141 return 0;
142
143error:
144 *bdev = NULL;
145 *bh = NULL;
146 return ret;
147}
148
Chris Masonffbd5172009-04-20 15:50:09 -0400149static void requeue_list(struct btrfs_pending_bios *pending_bios,
150 struct bio *head, struct bio *tail)
151{
152
153 struct bio *old_head;
154
155 old_head = pending_bios->head;
156 pending_bios->head = head;
157 if (pending_bios->tail)
158 tail->bi_next = old_head;
159 else
160 pending_bios->tail = tail;
161}
162
Chris Mason8b712842008-06-11 16:50:36 -0400163/*
164 * we try to collect pending bios for a device so we don't get a large
165 * number of procs sending bios down to the same device. This greatly
166 * improves the schedulers ability to collect and merge the bios.
167 *
168 * But, it also turns into a long list of bios to process and that is sure
169 * to eventually make the worker thread block. The solution here is to
170 * make some progress and then put this work struct back at the end of
171 * the list if the block device is congested. This way, multiple devices
172 * can make progress from a single worker thread.
173 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100174static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400175{
176 struct bio *pending;
177 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400178 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400179 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400180 struct bio *tail;
181 struct bio *cur;
182 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400183 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400184 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400185 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400186 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400187 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000188 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400189 struct blk_plug plug;
190
191 /*
192 * this function runs all the bios we've collected for
193 * a particular device. We don't want to wander off to
194 * another device without first sending all of these down.
195 * So, setup a plug here and finish it off before we return
196 */
197 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400198
Chris Masonbedf7622009-04-03 10:32:58 -0400199 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400200 fs_info = device->dev_root->fs_info;
201 limit = btrfs_async_submit_limit(fs_info);
202 limit = limit * 2 / 3;
203
Chris Mason8b712842008-06-11 16:50:36 -0400204loop:
205 spin_lock(&device->io_lock);
206
Chris Masona6837052009-02-04 09:19:41 -0500207loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400208 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400209
Chris Mason8b712842008-06-11 16:50:36 -0400210 /* take all the bios off the list at once and process them
211 * later on (without the lock held). But, remember the
212 * tail and other pointers so the bios can be properly reinserted
213 * into the list if we hit congestion
214 */
Chris Masond84275c2009-06-09 15:39:08 -0400215 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400216 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400217 force_reg = 1;
218 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400219 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400220 force_reg = 0;
221 }
Chris Masonffbd5172009-04-20 15:50:09 -0400222
223 pending = pending_bios->head;
224 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400225 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400226
227 /*
228 * if pending was null this time around, no bios need processing
229 * at all and we can stop. Otherwise it'll loop back up again
230 * and do an additional check so no bios are missed.
231 *
232 * device->running_pending is used to synchronize with the
233 * schedule_bio code.
234 */
Chris Masonffbd5172009-04-20 15:50:09 -0400235 if (device->pending_sync_bios.head == NULL &&
236 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400237 again = 0;
238 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400239 } else {
240 again = 1;
241 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400242 }
Chris Masonffbd5172009-04-20 15:50:09 -0400243
244 pending_bios->head = NULL;
245 pending_bios->tail = NULL;
246
Chris Mason8b712842008-06-11 16:50:36 -0400247 spin_unlock(&device->io_lock);
248
Chris Masond3977122009-01-05 21:25:51 -0500249 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400250
251 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400252 /* we want to work on both lists, but do more bios on the
253 * sync list than the regular list
254 */
255 if ((num_run > 32 &&
256 pending_bios != &device->pending_sync_bios &&
257 device->pending_sync_bios.head) ||
258 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
259 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400260 spin_lock(&device->io_lock);
261 requeue_list(pending_bios, pending, tail);
262 goto loop_lock;
263 }
264
Chris Mason8b712842008-06-11 16:50:36 -0400265 cur = pending;
266 pending = pending->bi_next;
267 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400268
Josef Bacik66657b32012-08-01 15:36:24 -0400269 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400270 waitqueue_active(&fs_info->async_submit_wait))
271 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400272
273 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400274
Chris Mason2ab1ba62011-08-04 14:28:36 -0400275 /*
276 * if we're doing the sync list, record that our
277 * plug has some sync requests on it
278 *
279 * If we're doing the regular list and there are
280 * sync requests sitting around, unplug before
281 * we add more
282 */
283 if (pending_bios == &device->pending_sync_bios) {
284 sync_pending = 1;
285 } else if (sync_pending) {
286 blk_finish_plug(&plug);
287 blk_start_plug(&plug);
288 sync_pending = 0;
289 }
290
Stefan Behrens21adbd52011-11-09 13:44:05 +0100291 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400292 num_run++;
293 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100294 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400295 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400296
297 /*
298 * we made progress, there is more work to do and the bdi
299 * is now congested. Back off and let other work structs
300 * run instead
301 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400302 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500303 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400304 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400305
Chris Masonb765ead2009-04-03 10:27:10 -0400306 ioc = current->io_context;
307
308 /*
309 * the main goal here is that we don't want to
310 * block if we're going to be able to submit
311 * more requests without blocking.
312 *
313 * This code does two great things, it pokes into
314 * the elevator code from a filesystem _and_
315 * it makes assumptions about how batching works.
316 */
317 if (ioc && ioc->nr_batch_requests > 0 &&
318 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
319 (last_waited == 0 ||
320 ioc->last_waited == last_waited)) {
321 /*
322 * we want to go through our batch of
323 * requests and stop. So, we copy out
324 * the ioc->last_waited time and test
325 * against it before looping
326 */
327 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100328 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400329 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400330 continue;
331 }
Chris Mason8b712842008-06-11 16:50:36 -0400332 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400333 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500334 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400335
336 spin_unlock(&device->io_lock);
337 btrfs_requeue_work(&device->work);
338 goto done;
339 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500340 /* unplug every 64 requests just for good measure */
341 if (batch_run % 64 == 0) {
342 blk_finish_plug(&plug);
343 blk_start_plug(&plug);
344 sync_pending = 0;
345 }
Chris Mason8b712842008-06-11 16:50:36 -0400346 }
Chris Masonffbd5172009-04-20 15:50:09 -0400347
Chris Mason51684082010-03-10 15:33:32 -0500348 cond_resched();
349 if (again)
350 goto loop;
351
352 spin_lock(&device->io_lock);
353 if (device->pending_bios.head || device->pending_sync_bios.head)
354 goto loop_lock;
355 spin_unlock(&device->io_lock);
356
Chris Mason8b712842008-06-11 16:50:36 -0400357done:
Chris Mason211588a2011-04-19 20:12:40 -0400358 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400359}
360
Christoph Hellwigb2950862008-12-02 09:54:17 -0500361static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400362{
363 struct btrfs_device *device;
364
365 device = container_of(work, struct btrfs_device, work);
366 run_scheduled_bios(device);
367}
368
Chris Masona1b32a52008-09-05 16:09:51 -0400369static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 struct btrfs_super_block *disk_super,
371 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
372{
373 struct btrfs_device *device;
374 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400375 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400376 u64 found_transid = btrfs_super_generation(disk_super);
377
378 fs_devices = find_fsid(disk_super->fsid);
379 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400380 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400381 if (!fs_devices)
382 return -ENOMEM;
383 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400384 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400385 list_add(&fs_devices->list, &fs_uuids);
386 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
387 fs_devices->latest_devid = devid;
388 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400389 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400390 device = NULL;
391 } else {
Chris Masona4437552008-04-18 10:29:38 -0400392 device = __find_device(&fs_devices->devices, devid,
393 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400394 }
395 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500396 if (fs_devices->opened)
397 return -EBUSY;
398
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 device = kzalloc(sizeof(*device), GFP_NOFS);
400 if (!device) {
401 /* we can safely leave the fs_devices entry around */
402 return -ENOMEM;
403 }
404 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200405 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400406 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400407 memcpy(device->uuid, disk_super->dev_item.uuid,
408 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400409 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400410
411 name = rcu_string_strdup(path, GFP_NOFS);
412 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400413 kfree(device);
414 return -ENOMEM;
415 }
Josef Bacik606686e2012-06-04 14:03:51 -0400416 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500417 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400418
Arne Jansen90519d62011-05-23 14:30:00 +0200419 /* init readahead state */
420 spin_lock_init(&device->reada_lock);
421 device->reada_curr_zone = NULL;
422 atomic_set(&device->reada_in_flight, 0);
423 device->reada_next = 0;
424 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
425 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
426
Chris Masone5e9a522009-06-10 15:17:02 -0400427 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000428 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400429 mutex_unlock(&fs_devices->device_list_mutex);
430
Yan Zheng2b820322008-11-17 21:11:30 -0500431 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400432 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400433 } else if (!device->name || strcmp(device->name->str, path)) {
434 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000435 if (!name)
436 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400437 rcu_string_free(device->name);
438 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500439 if (device->missing) {
440 fs_devices->missing_devices--;
441 device->missing = 0;
442 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400443 }
444
445 if (found_transid > fs_devices->latest_trans) {
446 fs_devices->latest_devid = devid;
447 fs_devices->latest_trans = found_transid;
448 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400449 *fs_devices_ret = fs_devices;
450 return 0;
451}
452
Yan Zhenge4404d62008-12-12 10:03:26 -0500453static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
454{
455 struct btrfs_fs_devices *fs_devices;
456 struct btrfs_device *device;
457 struct btrfs_device *orig_dev;
458
459 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
460 if (!fs_devices)
461 return ERR_PTR(-ENOMEM);
462
463 INIT_LIST_HEAD(&fs_devices->devices);
464 INIT_LIST_HEAD(&fs_devices->alloc_list);
465 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400466 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500467 fs_devices->latest_devid = orig->latest_devid;
468 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400469 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500470 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
471
Xiao Guangrong46224702011-04-20 10:08:47 +0000472 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500473 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400474 struct rcu_string *name;
475
Yan Zhenge4404d62008-12-12 10:03:26 -0500476 device = kzalloc(sizeof(*device), GFP_NOFS);
477 if (!device)
478 goto error;
479
Josef Bacik606686e2012-06-04 14:03:51 -0400480 /*
481 * This is ok to do without rcu read locked because we hold the
482 * uuid mutex so nothing we touch in here is going to disappear.
483 */
484 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
485 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400486 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500487 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400488 }
Josef Bacik606686e2012-06-04 14:03:51 -0400489 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500490
491 device->devid = orig_dev->devid;
492 device->work.func = pending_bios_fn;
493 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500494 spin_lock_init(&device->io_lock);
495 INIT_LIST_HEAD(&device->dev_list);
496 INIT_LIST_HEAD(&device->dev_alloc_list);
497
498 list_add(&device->dev_list, &fs_devices->devices);
499 device->fs_devices = fs_devices;
500 fs_devices->num_devices++;
501 }
502 return fs_devices;
503error:
504 free_fs_devices(fs_devices);
505 return ERR_PTR(-ENOMEM);
506}
507
Jeff Mahoney143bede2012-03-01 14:56:26 +0100508void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400509{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500510 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400511
Chris Masona6b0d5c2012-02-20 20:53:43 -0500512 struct block_device *latest_bdev = NULL;
513 u64 latest_devid = 0;
514 u64 latest_transid = 0;
515
Chris Masondfe25022008-05-13 13:46:40 -0400516 mutex_lock(&uuid_mutex);
517again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000518 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500519 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500520 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100521 if (!device->is_tgtdev_for_dev_replace &&
522 (!latest_transid ||
523 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500524 latest_devid = device->devid;
525 latest_transid = device->generation;
526 latest_bdev = device->bdev;
527 }
Yan Zheng2b820322008-11-17 21:11:30 -0500528 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500529 }
Yan Zheng2b820322008-11-17 21:11:30 -0500530
531 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100532 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500533 device->bdev = NULL;
534 fs_devices->open_devices--;
535 }
536 if (device->writeable) {
537 list_del_init(&device->dev_alloc_list);
538 device->writeable = 0;
539 fs_devices->rw_devices--;
540 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500541 list_del_init(&device->dev_list);
542 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400543 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500544 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400545 }
Yan Zheng2b820322008-11-17 21:11:30 -0500546
547 if (fs_devices->seed) {
548 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500549 goto again;
550 }
551
Chris Masona6b0d5c2012-02-20 20:53:43 -0500552 fs_devices->latest_bdev = latest_bdev;
553 fs_devices->latest_devid = latest_devid;
554 fs_devices->latest_trans = latest_transid;
555
Chris Masondfe25022008-05-13 13:46:40 -0400556 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400557}
Chris Masona0af4692008-05-13 16:03:06 -0400558
Xiao Guangrong1f781602011-04-20 10:09:16 +0000559static void __free_device(struct work_struct *work)
560{
561 struct btrfs_device *device;
562
563 device = container_of(work, struct btrfs_device, rcu_work);
564
565 if (device->bdev)
566 blkdev_put(device->bdev, device->mode);
567
Josef Bacik606686e2012-06-04 14:03:51 -0400568 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000569 kfree(device);
570}
571
572static void free_device(struct rcu_head *head)
573{
574 struct btrfs_device *device;
575
576 device = container_of(head, struct btrfs_device, rcu);
577
578 INIT_WORK(&device->rcu_work, __free_device);
579 schedule_work(&device->rcu_work);
580}
581
Yan Zheng2b820322008-11-17 21:11:30 -0500582static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400583{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400584 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500585
Yan Zheng2b820322008-11-17 21:11:30 -0500586 if (--fs_devices->opened > 0)
587 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000589 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500590 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000591 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400592 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000593
594 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400595 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000596
Yan Zheng2b820322008-11-17 21:11:30 -0500597 if (device->writeable) {
598 list_del_init(&device->dev_alloc_list);
599 fs_devices->rw_devices--;
600 }
601
Josef Bacikd5e20032011-08-04 14:52:27 +0000602 if (device->can_discard)
603 fs_devices->num_can_discard--;
604
Xiao Guangrong1f781602011-04-20 10:09:16 +0000605 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100606 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000607 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400608
609 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400610 if (device->name) {
611 name = rcu_string_strdup(device->name->str, GFP_NOFS);
612 BUG_ON(device->name && !name); /* -ENOMEM */
613 rcu_assign_pointer(new_device->name, name);
614 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000615 new_device->bdev = NULL;
616 new_device->writeable = 0;
617 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000618 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000619 list_replace_rcu(&device->dev_list, &new_device->dev_list);
620
621 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000623 mutex_unlock(&fs_devices->device_list_mutex);
624
Yan Zhenge4404d62008-12-12 10:03:26 -0500625 WARN_ON(fs_devices->open_devices);
626 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500627 fs_devices->opened = 0;
628 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500629
Chris Mason8a4b83c2008-03-24 15:02:07 -0400630 return 0;
631}
632
Yan Zheng2b820322008-11-17 21:11:30 -0500633int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
634{
Yan Zhenge4404d62008-12-12 10:03:26 -0500635 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500636 int ret;
637
638 mutex_lock(&uuid_mutex);
639 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500640 if (!fs_devices->opened) {
641 seed_devices = fs_devices->seed;
642 fs_devices->seed = NULL;
643 }
Yan Zheng2b820322008-11-17 21:11:30 -0500644 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500645
646 while (seed_devices) {
647 fs_devices = seed_devices;
648 seed_devices = fs_devices->seed;
649 __btrfs_close_devices(fs_devices);
650 free_fs_devices(fs_devices);
651 }
Yan Zheng2b820322008-11-17 21:11:30 -0500652 return ret;
653}
654
Yan Zhenge4404d62008-12-12 10:03:26 -0500655static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
656 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400657{
Josef Bacikd5e20032011-08-04 14:52:27 +0000658 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400659 struct block_device *bdev;
660 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400661 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400662 struct block_device *latest_bdev = NULL;
663 struct buffer_head *bh;
664 struct btrfs_super_block *disk_super;
665 u64 latest_devid = 0;
666 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400667 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500668 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400669 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400670
Tejun Heod4d77622010-11-13 11:55:18 +0100671 flags |= FMODE_EXCL;
672
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500673 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400674 if (device->bdev)
675 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400676 if (!device->name)
677 continue;
678
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100679 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
680 &bdev, &bh);
681 if (ret)
682 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400683
684 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000685 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400686 if (devid != device->devid)
687 goto error_brelse;
688
Yan Zheng2b820322008-11-17 21:11:30 -0500689 if (memcmp(device->uuid, disk_super->dev_item.uuid,
690 BTRFS_UUID_SIZE))
691 goto error_brelse;
692
693 device->generation = btrfs_super_generation(disk_super);
694 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400695 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500696 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400697 latest_bdev = bdev;
698 }
699
Yan Zheng2b820322008-11-17 21:11:30 -0500700 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
701 device->writeable = 0;
702 } else {
703 device->writeable = !bdev_read_only(bdev);
704 seeding = 0;
705 }
706
Josef Bacikd5e20032011-08-04 14:52:27 +0000707 q = bdev_get_queue(bdev);
708 if (blk_queue_discard(q)) {
709 device->can_discard = 1;
710 fs_devices->num_can_discard++;
711 }
712
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400714 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500715 device->mode = flags;
716
Chris Masonc289811c2009-06-10 09:51:32 -0400717 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
718 fs_devices->rotating = 1;
719
Chris Masona0af4692008-05-13 16:03:06 -0400720 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500721 if (device->writeable) {
722 fs_devices->rw_devices++;
723 list_add(&device->dev_alloc_list,
724 &fs_devices->alloc_list);
725 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000726 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400727 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400728
Chris Masona0af4692008-05-13 16:03:06 -0400729error_brelse:
730 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100731 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400732 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400733 }
Chris Masona0af4692008-05-13 16:03:06 -0400734 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300735 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400736 goto out;
737 }
Yan Zheng2b820322008-11-17 21:11:30 -0500738 fs_devices->seeding = seeding;
739 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400740 fs_devices->latest_bdev = latest_bdev;
741 fs_devices->latest_devid = latest_devid;
742 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500743 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400744out:
Yan Zheng2b820322008-11-17 21:11:30 -0500745 return ret;
746}
747
748int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500749 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500750{
751 int ret;
752
753 mutex_lock(&uuid_mutex);
754 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500755 fs_devices->opened++;
756 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500757 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500758 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500759 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400761 return ret;
762}
763
Christoph Hellwig97288f22008-12-02 06:36:09 -0500764int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765 struct btrfs_fs_devices **fs_devices_ret)
766{
767 struct btrfs_super_block *disk_super;
768 struct block_device *bdev;
769 struct buffer_head *bh;
770 int ret;
771 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400772 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400773 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400774
Tejun Heod4d77622010-11-13 11:55:18 +0100775 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500776 mutex_lock(&uuid_mutex);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100777 ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400778 if (ret)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100779 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400780 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000781 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400782 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400783 total_devices = btrfs_super_num_devices(disk_super);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000784 if (disk_super->label[0]) {
785 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
786 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500787 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000788 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200789 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000790 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500791 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500792 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400793 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400794 if (!ret && fs_devices_ret)
795 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400796 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100797 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400798error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100799 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400800 return ret;
801}
Chris Mason0b86a832008-03-24 15:01:56 -0400802
Miao Xie6d07bce2011-01-05 10:07:31 +0000803/* helper to account the used device space in the range */
804int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
805 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400806{
807 struct btrfs_key key;
808 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000809 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500810 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000811 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400812 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000813 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400814 struct extent_buffer *l;
815
Miao Xie6d07bce2011-01-05 10:07:31 +0000816 *length = 0;
817
Stefan Behrens63a212a2012-11-05 18:29:28 +0100818 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000819 return 0;
820
Yan Zheng2b820322008-11-17 21:11:30 -0500821 path = btrfs_alloc_path();
822 if (!path)
823 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400824 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400825
Chris Mason0b86a832008-03-24 15:01:56 -0400826 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000827 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400828 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000829
830 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400831 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000832 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400833 if (ret > 0) {
834 ret = btrfs_previous_item(root, path, key.objectid, key.type);
835 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000836 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400837 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000838
Chris Mason0b86a832008-03-24 15:01:56 -0400839 while (1) {
840 l = path->nodes[0];
841 slot = path->slots[0];
842 if (slot >= btrfs_header_nritems(l)) {
843 ret = btrfs_next_leaf(root, path);
844 if (ret == 0)
845 continue;
846 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000847 goto out;
848
849 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400850 }
851 btrfs_item_key_to_cpu(l, &key, slot);
852
853 if (key.objectid < device->devid)
854 goto next;
855
856 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000857 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400858
Chris Masond3977122009-01-05 21:25:51 -0500859 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400860 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400861
Chris Mason0b86a832008-03-24 15:01:56 -0400862 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000863 extent_end = key.offset + btrfs_dev_extent_length(l,
864 dev_extent);
865 if (key.offset <= start && extent_end > end) {
866 *length = end - start + 1;
867 break;
868 } else if (key.offset <= start && extent_end > start)
869 *length += extent_end - start;
870 else if (key.offset > start && extent_end <= end)
871 *length += extent_end - key.offset;
872 else if (key.offset > start && key.offset <= end) {
873 *length += end - key.offset + 1;
874 break;
875 } else if (key.offset > end)
876 break;
877
878next:
879 path->slots[0]++;
880 }
881 ret = 0;
882out:
883 btrfs_free_path(path);
884 return ret;
885}
886
Chris Mason0b86a832008-03-24 15:01:56 -0400887/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000888 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000889 * @device: the device which we search the free space in
890 * @num_bytes: the size of the free space that we need
891 * @start: store the start of the free space.
892 * @len: the size of the free space. that we find, or the size of the max
893 * free space if we don't find suitable free space
894 *
Chris Mason0b86a832008-03-24 15:01:56 -0400895 * this uses a pretty simple search, the expectation is that it is
896 * called very infrequently and that a given device has a small number
897 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000898 *
899 * @start is used to store the start of the free space if we find. But if we
900 * don't find suitable free space, it will be used to store the start position
901 * of the max free space.
902 *
903 * @len is used to store the size of the free space that we find.
904 * But if we don't find suitable free space, it is used to store the size of
905 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400906 */
Li Zefan125ccb02011-12-08 15:07:24 +0800907int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000908 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400909{
910 struct btrfs_key key;
911 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000912 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400913 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000914 u64 hole_size;
915 u64 max_hole_start;
916 u64 max_hole_size;
917 u64 extent_end;
918 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400919 u64 search_end = device->total_bytes;
920 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000921 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400922 struct extent_buffer *l;
923
Chris Mason0b86a832008-03-24 15:01:56 -0400924 /* FIXME use last free of some kind */
925
926 /* we don't want to overwrite the superblock on the drive,
927 * so we make sure to start at an offset of at least 1MB
928 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200929 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400930
Miao Xie7bfc8372011-01-05 10:07:26 +0000931 max_hole_start = search_start;
932 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000933 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000934
Stefan Behrens63a212a2012-11-05 18:29:28 +0100935 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +0000936 ret = -ENOSPC;
937 goto error;
938 }
939
940 path = btrfs_alloc_path();
941 if (!path) {
942 ret = -ENOMEM;
943 goto error;
944 }
945 path->reada = 2;
946
Chris Mason0b86a832008-03-24 15:01:56 -0400947 key.objectid = device->devid;
948 key.offset = search_start;
949 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000950
Li Zefan125ccb02011-12-08 15:07:24 +0800951 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400952 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000953 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400954 if (ret > 0) {
955 ret = btrfs_previous_item(root, path, key.objectid, key.type);
956 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000957 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400958 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000959
Chris Mason0b86a832008-03-24 15:01:56 -0400960 while (1) {
961 l = path->nodes[0];
962 slot = path->slots[0];
963 if (slot >= btrfs_header_nritems(l)) {
964 ret = btrfs_next_leaf(root, path);
965 if (ret == 0)
966 continue;
967 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000968 goto out;
969
970 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400971 }
972 btrfs_item_key_to_cpu(l, &key, slot);
973
974 if (key.objectid < device->devid)
975 goto next;
976
977 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000978 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400979
Chris Mason0b86a832008-03-24 15:01:56 -0400980 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
981 goto next;
982
Miao Xie7bfc8372011-01-05 10:07:26 +0000983 if (key.offset > search_start) {
984 hole_size = key.offset - search_start;
985
986 if (hole_size > max_hole_size) {
987 max_hole_start = search_start;
988 max_hole_size = hole_size;
989 }
990
991 /*
992 * If this free space is greater than which we need,
993 * it must be the max free space that we have found
994 * until now, so max_hole_start must point to the start
995 * of this free space and the length of this free space
996 * is stored in max_hole_size. Thus, we return
997 * max_hole_start and max_hole_size and go back to the
998 * caller.
999 */
1000 if (hole_size >= num_bytes) {
1001 ret = 0;
1002 goto out;
1003 }
1004 }
1005
Chris Mason0b86a832008-03-24 15:01:56 -04001006 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001007 extent_end = key.offset + btrfs_dev_extent_length(l,
1008 dev_extent);
1009 if (extent_end > search_start)
1010 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001011next:
1012 path->slots[0]++;
1013 cond_resched();
1014 }
Chris Mason0b86a832008-03-24 15:01:56 -04001015
liubo38c01b92011-08-02 02:39:03 +00001016 /*
1017 * At this point, search_start should be the end of
1018 * allocated dev extents, and when shrinking the device,
1019 * search_end may be smaller than search_start.
1020 */
1021 if (search_end > search_start)
1022 hole_size = search_end - search_start;
1023
Miao Xie7bfc8372011-01-05 10:07:26 +00001024 if (hole_size > max_hole_size) {
1025 max_hole_start = search_start;
1026 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001027 }
Chris Mason0b86a832008-03-24 15:01:56 -04001028
Miao Xie7bfc8372011-01-05 10:07:26 +00001029 /* See above. */
1030 if (hole_size < num_bytes)
1031 ret = -ENOSPC;
1032 else
1033 ret = 0;
1034
1035out:
Yan Zheng2b820322008-11-17 21:11:30 -05001036 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001037error:
1038 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001039 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001040 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001041 return ret;
1042}
1043
Christoph Hellwigb2950862008-12-02 09:54:17 -05001044static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001045 struct btrfs_device *device,
1046 u64 start)
1047{
1048 int ret;
1049 struct btrfs_path *path;
1050 struct btrfs_root *root = device->dev_root;
1051 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001052 struct btrfs_key found_key;
1053 struct extent_buffer *leaf = NULL;
1054 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001055
1056 path = btrfs_alloc_path();
1057 if (!path)
1058 return -ENOMEM;
1059
1060 key.objectid = device->devid;
1061 key.offset = start;
1062 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001063again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001064 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001065 if (ret > 0) {
1066 ret = btrfs_previous_item(root, path, key.objectid,
1067 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001068 if (ret)
1069 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001070 leaf = path->nodes[0];
1071 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1072 extent = btrfs_item_ptr(leaf, path->slots[0],
1073 struct btrfs_dev_extent);
1074 BUG_ON(found_key.offset > start || found_key.offset +
1075 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001076 key = found_key;
1077 btrfs_release_path(path);
1078 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001079 } else if (ret == 0) {
1080 leaf = path->nodes[0];
1081 extent = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001083 } else {
1084 btrfs_error(root->fs_info, ret, "Slot search failed");
1085 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001086 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001087
Josef Bacik2bf64752011-09-26 17:12:22 -04001088 if (device->bytes_used > 0) {
1089 u64 len = btrfs_dev_extent_length(leaf, extent);
1090 device->bytes_used -= len;
1091 spin_lock(&root->fs_info->free_chunk_lock);
1092 root->fs_info->free_chunk_space += len;
1093 spin_unlock(&root->fs_info->free_chunk_lock);
1094 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001095 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001096 if (ret) {
1097 btrfs_error(root->fs_info, ret,
1098 "Failed to remove dev extent item");
1099 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001100out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001101 btrfs_free_path(path);
1102 return ret;
1103}
1104
Yan Zheng2b820322008-11-17 21:11:30 -05001105int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001106 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001107 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001108 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001109{
1110 int ret;
1111 struct btrfs_path *path;
1112 struct btrfs_root *root = device->dev_root;
1113 struct btrfs_dev_extent *extent;
1114 struct extent_buffer *leaf;
1115 struct btrfs_key key;
1116
Chris Masondfe25022008-05-13 13:46:40 -04001117 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001118 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001119 path = btrfs_alloc_path();
1120 if (!path)
1121 return -ENOMEM;
1122
Chris Mason0b86a832008-03-24 15:01:56 -04001123 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001124 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001125 key.type = BTRFS_DEV_EXTENT_KEY;
1126 ret = btrfs_insert_empty_item(trans, root, path, &key,
1127 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001128 if (ret)
1129 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001130
1131 leaf = path->nodes[0];
1132 extent = btrfs_item_ptr(leaf, path->slots[0],
1133 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001134 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1135 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1136 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1137
1138 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1139 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1140 BTRFS_UUID_SIZE);
1141
Chris Mason0b86a832008-03-24 15:01:56 -04001142 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1143 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001144out:
Chris Mason0b86a832008-03-24 15:01:56 -04001145 btrfs_free_path(path);
1146 return ret;
1147}
1148
Chris Masona1b32a52008-09-05 16:09:51 -04001149static noinline int find_next_chunk(struct btrfs_root *root,
1150 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001151{
1152 struct btrfs_path *path;
1153 int ret;
1154 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001155 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001156 struct btrfs_key found_key;
1157
1158 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001159 if (!path)
1160 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001161
Chris Masone17cade2008-04-15 15:41:47 -04001162 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001163 key.offset = (u64)-1;
1164 key.type = BTRFS_CHUNK_ITEM_KEY;
1165
1166 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1167 if (ret < 0)
1168 goto error;
1169
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001170 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001171
1172 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1173 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001174 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001175 } else {
1176 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1177 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001178 if (found_key.objectid != objectid)
1179 *offset = 0;
1180 else {
1181 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1182 struct btrfs_chunk);
1183 *offset = found_key.offset +
1184 btrfs_chunk_length(path->nodes[0], chunk);
1185 }
Chris Mason0b86a832008-03-24 15:01:56 -04001186 }
1187 ret = 0;
1188error:
1189 btrfs_free_path(path);
1190 return ret;
1191}
1192
Yan Zheng2b820322008-11-17 21:11:30 -05001193static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001194{
1195 int ret;
1196 struct btrfs_key key;
1197 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001198 struct btrfs_path *path;
1199
1200 root = root->fs_info->chunk_root;
1201
1202 path = btrfs_alloc_path();
1203 if (!path)
1204 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001205
1206 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1207 key.type = BTRFS_DEV_ITEM_KEY;
1208 key.offset = (u64)-1;
1209
1210 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1211 if (ret < 0)
1212 goto error;
1213
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001214 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001215
1216 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1217 BTRFS_DEV_ITEM_KEY);
1218 if (ret) {
1219 *objectid = 1;
1220 } else {
1221 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1222 path->slots[0]);
1223 *objectid = found_key.offset + 1;
1224 }
1225 ret = 0;
1226error:
Yan Zheng2b820322008-11-17 21:11:30 -05001227 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001228 return ret;
1229}
1230
1231/*
1232 * the device information is stored in the chunk root
1233 * the btrfs_device struct should be fully filled in
1234 */
1235int btrfs_add_device(struct btrfs_trans_handle *trans,
1236 struct btrfs_root *root,
1237 struct btrfs_device *device)
1238{
1239 int ret;
1240 struct btrfs_path *path;
1241 struct btrfs_dev_item *dev_item;
1242 struct extent_buffer *leaf;
1243 struct btrfs_key key;
1244 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001245
1246 root = root->fs_info->chunk_root;
1247
1248 path = btrfs_alloc_path();
1249 if (!path)
1250 return -ENOMEM;
1251
Chris Mason0b86a832008-03-24 15:01:56 -04001252 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1253 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001254 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001255
1256 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001257 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001258 if (ret)
1259 goto out;
1260
1261 leaf = path->nodes[0];
1262 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1263
1264 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001265 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001266 btrfs_set_device_type(leaf, dev_item, device->type);
1267 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1268 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1269 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001270 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1271 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001272 btrfs_set_device_group(leaf, dev_item, 0);
1273 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1274 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001275 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001276
Chris Mason0b86a832008-03-24 15:01:56 -04001277 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001278 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001279 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1280 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001281 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001282
Yan Zheng2b820322008-11-17 21:11:30 -05001283 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001284out:
1285 btrfs_free_path(path);
1286 return ret;
1287}
Chris Mason8f18cf12008-04-25 16:53:30 -04001288
Chris Masona061fc82008-05-07 11:43:44 -04001289static int btrfs_rm_dev_item(struct btrfs_root *root,
1290 struct btrfs_device *device)
1291{
1292 int ret;
1293 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001294 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001295 struct btrfs_trans_handle *trans;
1296
1297 root = root->fs_info->chunk_root;
1298
1299 path = btrfs_alloc_path();
1300 if (!path)
1301 return -ENOMEM;
1302
Yan, Zhenga22285a2010-05-16 10:48:46 -04001303 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001304 if (IS_ERR(trans)) {
1305 btrfs_free_path(path);
1306 return PTR_ERR(trans);
1307 }
Chris Masona061fc82008-05-07 11:43:44 -04001308 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1309 key.type = BTRFS_DEV_ITEM_KEY;
1310 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001311 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001312
1313 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1314 if (ret < 0)
1315 goto out;
1316
1317 if (ret > 0) {
1318 ret = -ENOENT;
1319 goto out;
1320 }
1321
1322 ret = btrfs_del_item(trans, root, path);
1323 if (ret)
1324 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001325out:
1326 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001327 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001328 btrfs_commit_transaction(trans, root);
1329 return ret;
1330}
1331
1332int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1333{
1334 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001335 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001336 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001337 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001338 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001339 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001340 u64 all_avail;
1341 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001342 u64 num_devices;
1343 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001344 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001345 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001346
Chris Masona061fc82008-05-07 11:43:44 -04001347 mutex_lock(&uuid_mutex);
1348
1349 all_avail = root->fs_info->avail_data_alloc_bits |
1350 root->fs_info->avail_system_alloc_bits |
1351 root->fs_info->avail_metadata_alloc_bits;
1352
1353 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001354 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001355 printk(KERN_ERR "btrfs: unable to go below four devices "
1356 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001357 ret = -EINVAL;
1358 goto out;
1359 }
1360
1361 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001362 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001363 printk(KERN_ERR "btrfs: unable to go below two "
1364 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001365 ret = -EINVAL;
1366 goto out;
1367 }
1368
Chris Masondfe25022008-05-13 13:46:40 -04001369 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001370 struct list_head *devices;
1371 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001372
Chris Masondfe25022008-05-13 13:46:40 -04001373 device = NULL;
1374 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001375 /*
1376 * It is safe to read the devices since the volume_mutex
1377 * is held.
1378 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001379 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001380 if (tmp->in_fs_metadata &&
1381 !tmp->is_tgtdev_for_dev_replace &&
1382 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001383 device = tmp;
1384 break;
1385 }
1386 }
1387 bdev = NULL;
1388 bh = NULL;
1389 disk_super = NULL;
1390 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001391 printk(KERN_ERR "btrfs: no missing devices found to "
1392 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001393 goto out;
1394 }
Chris Masondfe25022008-05-13 13:46:40 -04001395 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001396 ret = btrfs_get_bdev_and_sb(device_path,
1397 FMODE_READ | FMODE_EXCL,
1398 root->fs_info->bdev_holder, 0,
1399 &bdev, &bh);
1400 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001401 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001402 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001403 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001404 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001405 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001406 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001407 if (!device) {
1408 ret = -ENOENT;
1409 goto error_brelse;
1410 }
Chris Masondfe25022008-05-13 13:46:40 -04001411 }
Yan Zheng2b820322008-11-17 21:11:30 -05001412
Stefan Behrens63a212a2012-11-05 18:29:28 +01001413 if (device->is_tgtdev_for_dev_replace) {
1414 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1415 ret = -EINVAL;
1416 goto error_brelse;
1417 }
1418
Yan Zheng2b820322008-11-17 21:11:30 -05001419 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001420 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1421 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001422 ret = -EINVAL;
1423 goto error_brelse;
1424 }
1425
1426 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001427 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001428 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001429 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001430 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001431 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001432 }
Chris Masona061fc82008-05-07 11:43:44 -04001433
1434 ret = btrfs_shrink_device(device, 0);
1435 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001436 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001437
Stefan Behrens63a212a2012-11-05 18:29:28 +01001438 /*
1439 * TODO: the superblock still includes this device in its num_devices
1440 * counter although write_all_supers() is not locked out. This
1441 * could give a filesystem state which requires a degraded mount.
1442 */
Chris Masona061fc82008-05-07 11:43:44 -04001443 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1444 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001445 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001446
Josef Bacik2bf64752011-09-26 17:12:22 -04001447 spin_lock(&root->fs_info->free_chunk_lock);
1448 root->fs_info->free_chunk_space = device->total_bytes -
1449 device->bytes_used;
1450 spin_unlock(&root->fs_info->free_chunk_lock);
1451
Yan Zheng2b820322008-11-17 21:11:30 -05001452 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001453 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001454
1455 /*
1456 * the device list mutex makes sure that we don't change
1457 * the device list while someone else is writing out all
1458 * the device supers.
1459 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001460
1461 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001462 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001463 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001464
Yan Zhenge4404d62008-12-12 10:03:26 -05001465 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001466 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001467
Chris Masoncd02dca2010-12-13 14:56:23 -05001468 if (device->missing)
1469 root->fs_info->fs_devices->missing_devices--;
1470
Yan Zheng2b820322008-11-17 21:11:30 -05001471 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1472 struct btrfs_device, dev_list);
1473 if (device->bdev == root->fs_info->sb->s_bdev)
1474 root->fs_info->sb->s_bdev = next_device->bdev;
1475 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1476 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1477
Xiao Guangrong1f781602011-04-20 10:09:16 +00001478 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001479 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001480
1481 call_rcu(&device->rcu, free_device);
1482 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001483
David Sterba6c417612011-04-13 15:41:04 +02001484 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1485 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001486
Xiao Guangrong1f781602011-04-20 10:09:16 +00001487 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001488 struct btrfs_fs_devices *fs_devices;
1489 fs_devices = root->fs_info->fs_devices;
1490 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001491 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001492 break;
1493 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001494 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001495 fs_devices->seed = cur_devices->seed;
1496 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001497 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001498 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001499 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001500 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001501 }
1502
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001503 root->fs_info->num_tolerated_disk_barrier_failures =
1504 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1505
Yan Zheng2b820322008-11-17 21:11:30 -05001506 /*
1507 * at this point, the device is zero sized. We want to
1508 * remove it from the devices list and zero out the old super
1509 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001510 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001511 /* make sure this device isn't detected as part of
1512 * the FS anymore
1513 */
1514 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1515 set_buffer_dirty(bh);
1516 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001517 }
Chris Masona061fc82008-05-07 11:43:44 -04001518
Chris Masona061fc82008-05-07 11:43:44 -04001519 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001520
1521error_brelse:
1522 brelse(bh);
1523error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001524 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001525 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001526out:
1527 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001528 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001529error_undo:
1530 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001531 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001532 list_add(&device->dev_alloc_list,
1533 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001534 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001535 root->fs_info->fs_devices->rw_devices++;
1536 }
1537 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001538}
1539
Stefan Behrense93c89c2012-11-05 17:33:06 +01001540void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1541 struct btrfs_device *srcdev)
1542{
1543 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1544 list_del_rcu(&srcdev->dev_list);
1545 list_del_rcu(&srcdev->dev_alloc_list);
1546 fs_info->fs_devices->num_devices--;
1547 if (srcdev->missing) {
1548 fs_info->fs_devices->missing_devices--;
1549 fs_info->fs_devices->rw_devices++;
1550 }
1551 if (srcdev->can_discard)
1552 fs_info->fs_devices->num_can_discard--;
1553 if (srcdev->bdev)
1554 fs_info->fs_devices->open_devices--;
1555
1556 call_rcu(&srcdev->rcu, free_device);
1557}
1558
1559void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1560 struct btrfs_device *tgtdev)
1561{
1562 struct btrfs_device *next_device;
1563
1564 WARN_ON(!tgtdev);
1565 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1566 if (tgtdev->bdev) {
1567 btrfs_scratch_superblock(tgtdev);
1568 fs_info->fs_devices->open_devices--;
1569 }
1570 fs_info->fs_devices->num_devices--;
1571 if (tgtdev->can_discard)
1572 fs_info->fs_devices->num_can_discard++;
1573
1574 next_device = list_entry(fs_info->fs_devices->devices.next,
1575 struct btrfs_device, dev_list);
1576 if (tgtdev->bdev == fs_info->sb->s_bdev)
1577 fs_info->sb->s_bdev = next_device->bdev;
1578 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1579 fs_info->fs_devices->latest_bdev = next_device->bdev;
1580 list_del_rcu(&tgtdev->dev_list);
1581
1582 call_rcu(&tgtdev->rcu, free_device);
1583
1584 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1585}
1586
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001587int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1588 struct btrfs_device **device)
1589{
1590 int ret = 0;
1591 struct btrfs_super_block *disk_super;
1592 u64 devid;
1593 u8 *dev_uuid;
1594 struct block_device *bdev;
1595 struct buffer_head *bh;
1596
1597 *device = NULL;
1598 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1599 root->fs_info->bdev_holder, 0, &bdev, &bh);
1600 if (ret)
1601 return ret;
1602 disk_super = (struct btrfs_super_block *)bh->b_data;
1603 devid = btrfs_stack_device_id(&disk_super->dev_item);
1604 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001605 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001606 disk_super->fsid);
1607 brelse(bh);
1608 if (!*device)
1609 ret = -ENOENT;
1610 blkdev_put(bdev, FMODE_READ);
1611 return ret;
1612}
1613
1614int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1615 char *device_path,
1616 struct btrfs_device **device)
1617{
1618 *device = NULL;
1619 if (strcmp(device_path, "missing") == 0) {
1620 struct list_head *devices;
1621 struct btrfs_device *tmp;
1622
1623 devices = &root->fs_info->fs_devices->devices;
1624 /*
1625 * It is safe to read the devices since the volume_mutex
1626 * is held by the caller.
1627 */
1628 list_for_each_entry(tmp, devices, dev_list) {
1629 if (tmp->in_fs_metadata && !tmp->bdev) {
1630 *device = tmp;
1631 break;
1632 }
1633 }
1634
1635 if (!*device) {
1636 pr_err("btrfs: no missing device found\n");
1637 return -ENOENT;
1638 }
1639
1640 return 0;
1641 } else {
1642 return btrfs_find_device_by_path(root, device_path, device);
1643 }
1644}
1645
Yan Zheng2b820322008-11-17 21:11:30 -05001646/*
1647 * does all the dirty work required for changing file system's UUID.
1648 */
Li Zefan125ccb02011-12-08 15:07:24 +08001649static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001650{
1651 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1652 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001653 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001654 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001655 struct btrfs_device *device;
1656 u64 super_flags;
1657
1658 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001659 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001660 return -EINVAL;
1661
Yan Zhenge4404d62008-12-12 10:03:26 -05001662 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1663 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001664 return -ENOMEM;
1665
Yan Zhenge4404d62008-12-12 10:03:26 -05001666 old_devices = clone_fs_devices(fs_devices);
1667 if (IS_ERR(old_devices)) {
1668 kfree(seed_devices);
1669 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001670 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001671
Yan Zheng2b820322008-11-17 21:11:30 -05001672 list_add(&old_devices->list, &fs_uuids);
1673
Yan Zhenge4404d62008-12-12 10:03:26 -05001674 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1675 seed_devices->opened = 1;
1676 INIT_LIST_HEAD(&seed_devices->devices);
1677 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001678 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001679
1680 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001681 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1682 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001683 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1684
Yan Zhenge4404d62008-12-12 10:03:26 -05001685 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1686 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1687 device->fs_devices = seed_devices;
1688 }
1689
Yan Zheng2b820322008-11-17 21:11:30 -05001690 fs_devices->seeding = 0;
1691 fs_devices->num_devices = 0;
1692 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001693 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001694 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001695
1696 generate_random_uuid(fs_devices->fsid);
1697 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1698 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1699 super_flags = btrfs_super_flags(disk_super) &
1700 ~BTRFS_SUPER_FLAG_SEEDING;
1701 btrfs_set_super_flags(disk_super, super_flags);
1702
1703 return 0;
1704}
1705
1706/*
1707 * strore the expected generation for seed devices in device items.
1708 */
1709static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1710 struct btrfs_root *root)
1711{
1712 struct btrfs_path *path;
1713 struct extent_buffer *leaf;
1714 struct btrfs_dev_item *dev_item;
1715 struct btrfs_device *device;
1716 struct btrfs_key key;
1717 u8 fs_uuid[BTRFS_UUID_SIZE];
1718 u8 dev_uuid[BTRFS_UUID_SIZE];
1719 u64 devid;
1720 int ret;
1721
1722 path = btrfs_alloc_path();
1723 if (!path)
1724 return -ENOMEM;
1725
1726 root = root->fs_info->chunk_root;
1727 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1728 key.offset = 0;
1729 key.type = BTRFS_DEV_ITEM_KEY;
1730
1731 while (1) {
1732 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1733 if (ret < 0)
1734 goto error;
1735
1736 leaf = path->nodes[0];
1737next_slot:
1738 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1739 ret = btrfs_next_leaf(root, path);
1740 if (ret > 0)
1741 break;
1742 if (ret < 0)
1743 goto error;
1744 leaf = path->nodes[0];
1745 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001746 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001747 continue;
1748 }
1749
1750 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1751 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1752 key.type != BTRFS_DEV_ITEM_KEY)
1753 break;
1754
1755 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1756 struct btrfs_dev_item);
1757 devid = btrfs_device_id(leaf, dev_item);
1758 read_extent_buffer(leaf, dev_uuid,
1759 (unsigned long)btrfs_device_uuid(dev_item),
1760 BTRFS_UUID_SIZE);
1761 read_extent_buffer(leaf, fs_uuid,
1762 (unsigned long)btrfs_device_fsid(dev_item),
1763 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001764 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1765 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001766 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001767
1768 if (device->fs_devices->seeding) {
1769 btrfs_set_device_generation(leaf, dev_item,
1770 device->generation);
1771 btrfs_mark_buffer_dirty(leaf);
1772 }
1773
1774 path->slots[0]++;
1775 goto next_slot;
1776 }
1777 ret = 0;
1778error:
1779 btrfs_free_path(path);
1780 return ret;
1781}
1782
Chris Mason788f20e2008-04-28 15:29:42 -04001783int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1784{
Josef Bacikd5e20032011-08-04 14:52:27 +00001785 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001786 struct btrfs_trans_handle *trans;
1787 struct btrfs_device *device;
1788 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001789 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001790 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001791 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001792 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001793 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001794 int ret = 0;
1795
Yan Zheng2b820322008-11-17 21:11:30 -05001796 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001797 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001798
Li Zefana5d16332011-12-07 20:08:40 -05001799 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001800 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001801 if (IS_ERR(bdev))
1802 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001803
Yan Zheng2b820322008-11-17 21:11:30 -05001804 if (root->fs_info->fs_devices->seeding) {
1805 seeding_dev = 1;
1806 down_write(&sb->s_umount);
1807 mutex_lock(&uuid_mutex);
1808 }
1809
Chris Mason8c8bee12008-09-29 11:19:10 -04001810 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001811
Chris Mason788f20e2008-04-28 15:29:42 -04001812 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001813
1814 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001815 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001816 if (device->bdev == bdev) {
1817 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001818 mutex_unlock(
1819 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001820 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001821 }
1822 }
Liu Bod25628b2012-11-14 14:35:30 +00001823 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001824
1825 device = kzalloc(sizeof(*device), GFP_NOFS);
1826 if (!device) {
1827 /* we can safely leave the fs_devices entry around */
1828 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001829 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001830 }
1831
Josef Bacik606686e2012-06-04 14:03:51 -04001832 name = rcu_string_strdup(device_path, GFP_NOFS);
1833 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001834 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001835 ret = -ENOMEM;
1836 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001837 }
Josef Bacik606686e2012-06-04 14:03:51 -04001838 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001839
1840 ret = find_next_devid(root, &device->devid);
1841 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001842 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001843 kfree(device);
1844 goto error;
1845 }
1846
Yan, Zhenga22285a2010-05-16 10:48:46 -04001847 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001848 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001849 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001850 kfree(device);
1851 ret = PTR_ERR(trans);
1852 goto error;
1853 }
1854
Yan Zheng2b820322008-11-17 21:11:30 -05001855 lock_chunks(root);
1856
Josef Bacikd5e20032011-08-04 14:52:27 +00001857 q = bdev_get_queue(bdev);
1858 if (blk_queue_discard(q))
1859 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001860 device->writeable = 1;
1861 device->work.func = pending_bios_fn;
1862 generate_random_uuid(device->uuid);
1863 spin_lock_init(&device->io_lock);
1864 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001865 device->io_width = root->sectorsize;
1866 device->io_align = root->sectorsize;
1867 device->sector_size = root->sectorsize;
1868 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001869 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001870 device->dev_root = root->fs_info->dev_root;
1871 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001872 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001873 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001874 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001875 set_blocksize(device->bdev, 4096);
1876
Yan Zheng2b820322008-11-17 21:11:30 -05001877 if (seeding_dev) {
1878 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001879 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001880 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001881 }
1882
1883 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001884
Chris Masone5e9a522009-06-10 15:17:02 -04001885 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001886 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001887 list_add(&device->dev_alloc_list,
1888 &root->fs_info->fs_devices->alloc_list);
1889 root->fs_info->fs_devices->num_devices++;
1890 root->fs_info->fs_devices->open_devices++;
1891 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001892 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001893 if (device->can_discard)
1894 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001895 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1896
Josef Bacik2bf64752011-09-26 17:12:22 -04001897 spin_lock(&root->fs_info->free_chunk_lock);
1898 root->fs_info->free_chunk_space += device->total_bytes;
1899 spin_unlock(&root->fs_info->free_chunk_lock);
1900
Chris Masonc289811c2009-06-10 09:51:32 -04001901 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1902 root->fs_info->fs_devices->rotating = 1;
1903
David Sterba6c417612011-04-13 15:41:04 +02001904 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1905 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001906 total_bytes + device->total_bytes);
1907
David Sterba6c417612011-04-13 15:41:04 +02001908 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1909 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001910 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001911 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001912
Yan Zheng2b820322008-11-17 21:11:30 -05001913 if (seeding_dev) {
1914 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001915 if (ret) {
1916 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001917 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001918 }
Yan Zheng2b820322008-11-17 21:11:30 -05001919 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06001920 if (ret) {
1921 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001923 }
Yan Zheng2b820322008-11-17 21:11:30 -05001924 } else {
1925 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001926 if (ret) {
1927 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001929 }
Yan Zheng2b820322008-11-17 21:11:30 -05001930 }
1931
Chris Mason913d9522009-03-10 13:17:18 -04001932 /*
1933 * we've got more storage, clear any full flags on the space
1934 * infos
1935 */
1936 btrfs_clear_space_info_full(root->fs_info);
1937
Chris Mason7d9eb122008-07-08 14:19:17 -04001938 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001939 root->fs_info->num_tolerated_disk_barrier_failures =
1940 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001941 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001942
1943 if (seeding_dev) {
1944 mutex_unlock(&uuid_mutex);
1945 up_write(&sb->s_umount);
1946
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001947 if (ret) /* transaction commit */
1948 return ret;
1949
Yan Zheng2b820322008-11-17 21:11:30 -05001950 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951 if (ret < 0)
1952 btrfs_error(root->fs_info, ret,
1953 "Failed to relocate sys chunks after "
1954 "device initialization. This can be fixed "
1955 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00001956 trans = btrfs_attach_transaction(root);
1957 if (IS_ERR(trans)) {
1958 if (PTR_ERR(trans) == -ENOENT)
1959 return 0;
1960 return PTR_ERR(trans);
1961 }
1962 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001963 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001964
Chris Mason788f20e2008-04-28 15:29:42 -04001965 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001966
1967error_trans:
1968 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001969 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001970 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001971 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001972error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001973 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001974 if (seeding_dev) {
1975 mutex_unlock(&uuid_mutex);
1976 up_write(&sb->s_umount);
1977 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001978 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001979}
1980
Stefan Behrense93c89c2012-11-05 17:33:06 +01001981int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
1982 struct btrfs_device **device_out)
1983{
1984 struct request_queue *q;
1985 struct btrfs_device *device;
1986 struct block_device *bdev;
1987 struct btrfs_fs_info *fs_info = root->fs_info;
1988 struct list_head *devices;
1989 struct rcu_string *name;
1990 int ret = 0;
1991
1992 *device_out = NULL;
1993 if (fs_info->fs_devices->seeding)
1994 return -EINVAL;
1995
1996 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
1997 fs_info->bdev_holder);
1998 if (IS_ERR(bdev))
1999 return PTR_ERR(bdev);
2000
2001 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2002
2003 devices = &fs_info->fs_devices->devices;
2004 list_for_each_entry(device, devices, dev_list) {
2005 if (device->bdev == bdev) {
2006 ret = -EEXIST;
2007 goto error;
2008 }
2009 }
2010
2011 device = kzalloc(sizeof(*device), GFP_NOFS);
2012 if (!device) {
2013 ret = -ENOMEM;
2014 goto error;
2015 }
2016
2017 name = rcu_string_strdup(device_path, GFP_NOFS);
2018 if (!name) {
2019 kfree(device);
2020 ret = -ENOMEM;
2021 goto error;
2022 }
2023 rcu_assign_pointer(device->name, name);
2024
2025 q = bdev_get_queue(bdev);
2026 if (blk_queue_discard(q))
2027 device->can_discard = 1;
2028 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2029 device->writeable = 1;
2030 device->work.func = pending_bios_fn;
2031 generate_random_uuid(device->uuid);
2032 device->devid = BTRFS_DEV_REPLACE_DEVID;
2033 spin_lock_init(&device->io_lock);
2034 device->generation = 0;
2035 device->io_width = root->sectorsize;
2036 device->io_align = root->sectorsize;
2037 device->sector_size = root->sectorsize;
2038 device->total_bytes = i_size_read(bdev->bd_inode);
2039 device->disk_total_bytes = device->total_bytes;
2040 device->dev_root = fs_info->dev_root;
2041 device->bdev = bdev;
2042 device->in_fs_metadata = 1;
2043 device->is_tgtdev_for_dev_replace = 1;
2044 device->mode = FMODE_EXCL;
2045 set_blocksize(device->bdev, 4096);
2046 device->fs_devices = fs_info->fs_devices;
2047 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2048 fs_info->fs_devices->num_devices++;
2049 fs_info->fs_devices->open_devices++;
2050 if (device->can_discard)
2051 fs_info->fs_devices->num_can_discard++;
2052 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2053
2054 *device_out = device;
2055 return ret;
2056
2057error:
2058 blkdev_put(bdev, FMODE_EXCL);
2059 return ret;
2060}
2061
2062void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2063 struct btrfs_device *tgtdev)
2064{
2065 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2066 tgtdev->io_width = fs_info->dev_root->sectorsize;
2067 tgtdev->io_align = fs_info->dev_root->sectorsize;
2068 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2069 tgtdev->dev_root = fs_info->dev_root;
2070 tgtdev->in_fs_metadata = 1;
2071}
2072
Chris Masond3977122009-01-05 21:25:51 -05002073static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2074 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002075{
2076 int ret;
2077 struct btrfs_path *path;
2078 struct btrfs_root *root;
2079 struct btrfs_dev_item *dev_item;
2080 struct extent_buffer *leaf;
2081 struct btrfs_key key;
2082
2083 root = device->dev_root->fs_info->chunk_root;
2084
2085 path = btrfs_alloc_path();
2086 if (!path)
2087 return -ENOMEM;
2088
2089 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2090 key.type = BTRFS_DEV_ITEM_KEY;
2091 key.offset = device->devid;
2092
2093 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2094 if (ret < 0)
2095 goto out;
2096
2097 if (ret > 0) {
2098 ret = -ENOENT;
2099 goto out;
2100 }
2101
2102 leaf = path->nodes[0];
2103 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2104
2105 btrfs_set_device_id(leaf, dev_item, device->devid);
2106 btrfs_set_device_type(leaf, dev_item, device->type);
2107 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2108 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2109 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002110 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002111 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2112 btrfs_mark_buffer_dirty(leaf);
2113
2114out:
2115 btrfs_free_path(path);
2116 return ret;
2117}
2118
Chris Mason7d9eb122008-07-08 14:19:17 -04002119static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002120 struct btrfs_device *device, u64 new_size)
2121{
2122 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002123 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002124 u64 old_total = btrfs_super_total_bytes(super_copy);
2125 u64 diff = new_size - device->total_bytes;
2126
Yan Zheng2b820322008-11-17 21:11:30 -05002127 if (!device->writeable)
2128 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002129 if (new_size <= device->total_bytes ||
2130 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002131 return -EINVAL;
2132
Chris Mason8f18cf12008-04-25 16:53:30 -04002133 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002134 device->fs_devices->total_rw_bytes += diff;
2135
2136 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002137 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002138 btrfs_clear_space_info_full(device->dev_root->fs_info);
2139
Chris Mason8f18cf12008-04-25 16:53:30 -04002140 return btrfs_update_device(trans, device);
2141}
2142
Chris Mason7d9eb122008-07-08 14:19:17 -04002143int btrfs_grow_device(struct btrfs_trans_handle *trans,
2144 struct btrfs_device *device, u64 new_size)
2145{
2146 int ret;
2147 lock_chunks(device->dev_root);
2148 ret = __btrfs_grow_device(trans, device, new_size);
2149 unlock_chunks(device->dev_root);
2150 return ret;
2151}
2152
Chris Mason8f18cf12008-04-25 16:53:30 -04002153static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2154 struct btrfs_root *root,
2155 u64 chunk_tree, u64 chunk_objectid,
2156 u64 chunk_offset)
2157{
2158 int ret;
2159 struct btrfs_path *path;
2160 struct btrfs_key key;
2161
2162 root = root->fs_info->chunk_root;
2163 path = btrfs_alloc_path();
2164 if (!path)
2165 return -ENOMEM;
2166
2167 key.objectid = chunk_objectid;
2168 key.offset = chunk_offset;
2169 key.type = BTRFS_CHUNK_ITEM_KEY;
2170
2171 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002172 if (ret < 0)
2173 goto out;
2174 else if (ret > 0) { /* Logic error or corruption */
2175 btrfs_error(root->fs_info, -ENOENT,
2176 "Failed lookup while freeing chunk.");
2177 ret = -ENOENT;
2178 goto out;
2179 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002180
2181 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002182 if (ret < 0)
2183 btrfs_error(root->fs_info, ret,
2184 "Failed to delete chunk item.");
2185out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002186 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002187 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002188}
2189
Christoph Hellwigb2950862008-12-02 09:54:17 -05002190static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002191 chunk_offset)
2192{
David Sterba6c417612011-04-13 15:41:04 +02002193 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002194 struct btrfs_disk_key *disk_key;
2195 struct btrfs_chunk *chunk;
2196 u8 *ptr;
2197 int ret = 0;
2198 u32 num_stripes;
2199 u32 array_size;
2200 u32 len = 0;
2201 u32 cur;
2202 struct btrfs_key key;
2203
2204 array_size = btrfs_super_sys_array_size(super_copy);
2205
2206 ptr = super_copy->sys_chunk_array;
2207 cur = 0;
2208
2209 while (cur < array_size) {
2210 disk_key = (struct btrfs_disk_key *)ptr;
2211 btrfs_disk_key_to_cpu(&key, disk_key);
2212
2213 len = sizeof(*disk_key);
2214
2215 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2216 chunk = (struct btrfs_chunk *)(ptr + len);
2217 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2218 len += btrfs_chunk_item_size(num_stripes);
2219 } else {
2220 ret = -EIO;
2221 break;
2222 }
2223 if (key.objectid == chunk_objectid &&
2224 key.offset == chunk_offset) {
2225 memmove(ptr, ptr + len, array_size - (cur + len));
2226 array_size -= len;
2227 btrfs_set_super_sys_array_size(super_copy, array_size);
2228 } else {
2229 ptr += len;
2230 cur += len;
2231 }
2232 }
2233 return ret;
2234}
2235
Christoph Hellwigb2950862008-12-02 09:54:17 -05002236static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002237 u64 chunk_tree, u64 chunk_objectid,
2238 u64 chunk_offset)
2239{
2240 struct extent_map_tree *em_tree;
2241 struct btrfs_root *extent_root;
2242 struct btrfs_trans_handle *trans;
2243 struct extent_map *em;
2244 struct map_lookup *map;
2245 int ret;
2246 int i;
2247
2248 root = root->fs_info->chunk_root;
2249 extent_root = root->fs_info->extent_root;
2250 em_tree = &root->fs_info->mapping_tree.map_tree;
2251
Josef Bacikba1bf482009-09-11 16:11:19 -04002252 ret = btrfs_can_relocate(extent_root, chunk_offset);
2253 if (ret)
2254 return -ENOSPC;
2255
Chris Mason8f18cf12008-04-25 16:53:30 -04002256 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002257 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002258 if (ret)
2259 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002260
Yan, Zhenga22285a2010-05-16 10:48:46 -04002261 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002262 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002263
Chris Mason7d9eb122008-07-08 14:19:17 -04002264 lock_chunks(root);
2265
Chris Mason8f18cf12008-04-25 16:53:30 -04002266 /*
2267 * step two, delete the device extents and the
2268 * chunk tree entries
2269 */
Chris Mason890871b2009-09-02 16:24:52 -04002270 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002271 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002272 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002273
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002274 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002275 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002276 map = (struct map_lookup *)em->bdev;
2277
2278 for (i = 0; i < map->num_stripes; i++) {
2279 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2280 map->stripes[i].physical);
2281 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002282
Chris Masondfe25022008-05-13 13:46:40 -04002283 if (map->stripes[i].dev) {
2284 ret = btrfs_update_device(trans, map->stripes[i].dev);
2285 BUG_ON(ret);
2286 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002287 }
2288 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2289 chunk_offset);
2290
2291 BUG_ON(ret);
2292
liubo1abe9b82011-03-24 11:18:59 +00002293 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2294
Chris Mason8f18cf12008-04-25 16:53:30 -04002295 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2296 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2297 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002298 }
2299
Zheng Yan1a40e232008-09-26 10:09:34 -04002300 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2301 BUG_ON(ret);
2302
Chris Mason890871b2009-09-02 16:24:52 -04002303 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002304 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002305 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002306
Chris Mason8f18cf12008-04-25 16:53:30 -04002307 kfree(map);
2308 em->bdev = NULL;
2309
2310 /* once for the tree */
2311 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002312 /* once for us */
2313 free_extent_map(em);
2314
Chris Mason7d9eb122008-07-08 14:19:17 -04002315 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002316 btrfs_end_transaction(trans, root);
2317 return 0;
2318}
2319
Yan Zheng2b820322008-11-17 21:11:30 -05002320static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2321{
2322 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2323 struct btrfs_path *path;
2324 struct extent_buffer *leaf;
2325 struct btrfs_chunk *chunk;
2326 struct btrfs_key key;
2327 struct btrfs_key found_key;
2328 u64 chunk_tree = chunk_root->root_key.objectid;
2329 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002330 bool retried = false;
2331 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002332 int ret;
2333
2334 path = btrfs_alloc_path();
2335 if (!path)
2336 return -ENOMEM;
2337
Josef Bacikba1bf482009-09-11 16:11:19 -04002338again:
Yan Zheng2b820322008-11-17 21:11:30 -05002339 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2340 key.offset = (u64)-1;
2341 key.type = BTRFS_CHUNK_ITEM_KEY;
2342
2343 while (1) {
2344 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2345 if (ret < 0)
2346 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002347 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002348
2349 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2350 key.type);
2351 if (ret < 0)
2352 goto error;
2353 if (ret > 0)
2354 break;
2355
2356 leaf = path->nodes[0];
2357 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2358
2359 chunk = btrfs_item_ptr(leaf, path->slots[0],
2360 struct btrfs_chunk);
2361 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002362 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002363
2364 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2365 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2366 found_key.objectid,
2367 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002368 if (ret == -ENOSPC)
2369 failed++;
2370 else if (ret)
2371 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002372 }
2373
2374 if (found_key.offset == 0)
2375 break;
2376 key.offset = found_key.offset - 1;
2377 }
2378 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002379 if (failed && !retried) {
2380 failed = 0;
2381 retried = true;
2382 goto again;
2383 } else if (failed && retried) {
2384 WARN_ON(1);
2385 ret = -ENOSPC;
2386 }
Yan Zheng2b820322008-11-17 21:11:30 -05002387error:
2388 btrfs_free_path(path);
2389 return ret;
2390}
2391
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002392static int insert_balance_item(struct btrfs_root *root,
2393 struct btrfs_balance_control *bctl)
2394{
2395 struct btrfs_trans_handle *trans;
2396 struct btrfs_balance_item *item;
2397 struct btrfs_disk_balance_args disk_bargs;
2398 struct btrfs_path *path;
2399 struct extent_buffer *leaf;
2400 struct btrfs_key key;
2401 int ret, err;
2402
2403 path = btrfs_alloc_path();
2404 if (!path)
2405 return -ENOMEM;
2406
2407 trans = btrfs_start_transaction(root, 0);
2408 if (IS_ERR(trans)) {
2409 btrfs_free_path(path);
2410 return PTR_ERR(trans);
2411 }
2412
2413 key.objectid = BTRFS_BALANCE_OBJECTID;
2414 key.type = BTRFS_BALANCE_ITEM_KEY;
2415 key.offset = 0;
2416
2417 ret = btrfs_insert_empty_item(trans, root, path, &key,
2418 sizeof(*item));
2419 if (ret)
2420 goto out;
2421
2422 leaf = path->nodes[0];
2423 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2424
2425 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2426
2427 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2428 btrfs_set_balance_data(leaf, item, &disk_bargs);
2429 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2430 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2431 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2432 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2433
2434 btrfs_set_balance_flags(leaf, item, bctl->flags);
2435
2436 btrfs_mark_buffer_dirty(leaf);
2437out:
2438 btrfs_free_path(path);
2439 err = btrfs_commit_transaction(trans, root);
2440 if (err && !ret)
2441 ret = err;
2442 return ret;
2443}
2444
2445static int del_balance_item(struct btrfs_root *root)
2446{
2447 struct btrfs_trans_handle *trans;
2448 struct btrfs_path *path;
2449 struct btrfs_key key;
2450 int ret, err;
2451
2452 path = btrfs_alloc_path();
2453 if (!path)
2454 return -ENOMEM;
2455
2456 trans = btrfs_start_transaction(root, 0);
2457 if (IS_ERR(trans)) {
2458 btrfs_free_path(path);
2459 return PTR_ERR(trans);
2460 }
2461
2462 key.objectid = BTRFS_BALANCE_OBJECTID;
2463 key.type = BTRFS_BALANCE_ITEM_KEY;
2464 key.offset = 0;
2465
2466 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2467 if (ret < 0)
2468 goto out;
2469 if (ret > 0) {
2470 ret = -ENOENT;
2471 goto out;
2472 }
2473
2474 ret = btrfs_del_item(trans, root, path);
2475out:
2476 btrfs_free_path(path);
2477 err = btrfs_commit_transaction(trans, root);
2478 if (err && !ret)
2479 ret = err;
2480 return ret;
2481}
2482
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002483/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002484 * This is a heuristic used to reduce the number of chunks balanced on
2485 * resume after balance was interrupted.
2486 */
2487static void update_balance_args(struct btrfs_balance_control *bctl)
2488{
2489 /*
2490 * Turn on soft mode for chunk types that were being converted.
2491 */
2492 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2493 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2494 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2495 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2496 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2497 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2498
2499 /*
2500 * Turn on usage filter if is not already used. The idea is
2501 * that chunks that we have already balanced should be
2502 * reasonably full. Don't do it for chunks that are being
2503 * converted - that will keep us from relocating unconverted
2504 * (albeit full) chunks.
2505 */
2506 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2507 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2508 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2509 bctl->data.usage = 90;
2510 }
2511 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2512 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2513 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2514 bctl->sys.usage = 90;
2515 }
2516 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2517 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2518 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2519 bctl->meta.usage = 90;
2520 }
2521}
2522
2523/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002524 * Should be called with both balance and volume mutexes held to
2525 * serialize other volume operations (add_dev/rm_dev/resize) with
2526 * restriper. Same goes for unset_balance_control.
2527 */
2528static void set_balance_control(struct btrfs_balance_control *bctl)
2529{
2530 struct btrfs_fs_info *fs_info = bctl->fs_info;
2531
2532 BUG_ON(fs_info->balance_ctl);
2533
2534 spin_lock(&fs_info->balance_lock);
2535 fs_info->balance_ctl = bctl;
2536 spin_unlock(&fs_info->balance_lock);
2537}
2538
2539static void unset_balance_control(struct btrfs_fs_info *fs_info)
2540{
2541 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2542
2543 BUG_ON(!fs_info->balance_ctl);
2544
2545 spin_lock(&fs_info->balance_lock);
2546 fs_info->balance_ctl = NULL;
2547 spin_unlock(&fs_info->balance_lock);
2548
2549 kfree(bctl);
2550}
2551
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002552/*
2553 * Balance filters. Return 1 if chunk should be filtered out
2554 * (should not be balanced).
2555 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002556static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002557 struct btrfs_balance_args *bargs)
2558{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002559 chunk_type = chunk_to_extended(chunk_type) &
2560 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002561
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002562 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002563 return 0;
2564
2565 return 1;
2566}
2567
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002568static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2569 struct btrfs_balance_args *bargs)
2570{
2571 struct btrfs_block_group_cache *cache;
2572 u64 chunk_used, user_thresh;
2573 int ret = 1;
2574
2575 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2576 chunk_used = btrfs_block_group_used(&cache->item);
2577
2578 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2579 if (chunk_used < user_thresh)
2580 ret = 0;
2581
2582 btrfs_put_block_group(cache);
2583 return ret;
2584}
2585
Ilya Dryomov409d4042012-01-16 22:04:47 +02002586static int chunk_devid_filter(struct extent_buffer *leaf,
2587 struct btrfs_chunk *chunk,
2588 struct btrfs_balance_args *bargs)
2589{
2590 struct btrfs_stripe *stripe;
2591 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2592 int i;
2593
2594 for (i = 0; i < num_stripes; i++) {
2595 stripe = btrfs_stripe_nr(chunk, i);
2596 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2597 return 0;
2598 }
2599
2600 return 1;
2601}
2602
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002603/* [pstart, pend) */
2604static int chunk_drange_filter(struct extent_buffer *leaf,
2605 struct btrfs_chunk *chunk,
2606 u64 chunk_offset,
2607 struct btrfs_balance_args *bargs)
2608{
2609 struct btrfs_stripe *stripe;
2610 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2611 u64 stripe_offset;
2612 u64 stripe_length;
2613 int factor;
2614 int i;
2615
2616 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2617 return 0;
2618
2619 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2620 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2621 factor = 2;
2622 else
2623 factor = 1;
2624 factor = num_stripes / factor;
2625
2626 for (i = 0; i < num_stripes; i++) {
2627 stripe = btrfs_stripe_nr(chunk, i);
2628 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2629 continue;
2630
2631 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2632 stripe_length = btrfs_chunk_length(leaf, chunk);
2633 do_div(stripe_length, factor);
2634
2635 if (stripe_offset < bargs->pend &&
2636 stripe_offset + stripe_length > bargs->pstart)
2637 return 0;
2638 }
2639
2640 return 1;
2641}
2642
Ilya Dryomovea671762012-01-16 22:04:48 +02002643/* [vstart, vend) */
2644static int chunk_vrange_filter(struct extent_buffer *leaf,
2645 struct btrfs_chunk *chunk,
2646 u64 chunk_offset,
2647 struct btrfs_balance_args *bargs)
2648{
2649 if (chunk_offset < bargs->vend &&
2650 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2651 /* at least part of the chunk is inside this vrange */
2652 return 0;
2653
2654 return 1;
2655}
2656
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002657static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002658 struct btrfs_balance_args *bargs)
2659{
2660 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2661 return 0;
2662
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002663 chunk_type = chunk_to_extended(chunk_type) &
2664 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002665
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002666 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002667 return 1;
2668
2669 return 0;
2670}
2671
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002672static int should_balance_chunk(struct btrfs_root *root,
2673 struct extent_buffer *leaf,
2674 struct btrfs_chunk *chunk, u64 chunk_offset)
2675{
2676 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2677 struct btrfs_balance_args *bargs = NULL;
2678 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2679
2680 /* type filter */
2681 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2682 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2683 return 0;
2684 }
2685
2686 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2687 bargs = &bctl->data;
2688 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2689 bargs = &bctl->sys;
2690 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2691 bargs = &bctl->meta;
2692
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002693 /* profiles filter */
2694 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2695 chunk_profiles_filter(chunk_type, bargs)) {
2696 return 0;
2697 }
2698
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002699 /* usage filter */
2700 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2701 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2702 return 0;
2703 }
2704
Ilya Dryomov409d4042012-01-16 22:04:47 +02002705 /* devid filter */
2706 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2707 chunk_devid_filter(leaf, chunk, bargs)) {
2708 return 0;
2709 }
2710
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002711 /* drange filter, makes sense only with devid filter */
2712 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2713 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2714 return 0;
2715 }
2716
Ilya Dryomovea671762012-01-16 22:04:48 +02002717 /* vrange filter */
2718 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2719 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2720 return 0;
2721 }
2722
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002723 /* soft profile changing mode */
2724 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2725 chunk_soft_convert_filter(chunk_type, bargs)) {
2726 return 0;
2727 }
2728
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002729 return 1;
2730}
2731
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002732static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002733{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002734 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002735 struct btrfs_root *chunk_root = fs_info->chunk_root;
2736 struct btrfs_root *dev_root = fs_info->dev_root;
2737 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002738 struct btrfs_device *device;
2739 u64 old_size;
2740 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002741 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002742 struct btrfs_path *path;
2743 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002744 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002745 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002746 struct extent_buffer *leaf;
2747 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002748 int ret;
2749 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002750 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002751
Chris Masonec44a352008-04-28 15:29:52 -04002752 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002753 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002754 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002755 old_size = device->total_bytes;
2756 size_to_free = div_factor(old_size, 1);
2757 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002758 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002759 device->total_bytes - device->bytes_used > size_to_free ||
2760 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002761 continue;
2762
2763 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002764 if (ret == -ENOSPC)
2765 break;
Chris Masonec44a352008-04-28 15:29:52 -04002766 BUG_ON(ret);
2767
Yan, Zhenga22285a2010-05-16 10:48:46 -04002768 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002769 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002770
2771 ret = btrfs_grow_device(trans, device, old_size);
2772 BUG_ON(ret);
2773
2774 btrfs_end_transaction(trans, dev_root);
2775 }
2776
2777 /* step two, relocate all the chunks */
2778 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002779 if (!path) {
2780 ret = -ENOMEM;
2781 goto error;
2782 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002783
2784 /* zero out stat counters */
2785 spin_lock(&fs_info->balance_lock);
2786 memset(&bctl->stat, 0, sizeof(bctl->stat));
2787 spin_unlock(&fs_info->balance_lock);
2788again:
Chris Masonec44a352008-04-28 15:29:52 -04002789 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2790 key.offset = (u64)-1;
2791 key.type = BTRFS_CHUNK_ITEM_KEY;
2792
Chris Masond3977122009-01-05 21:25:51 -05002793 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002794 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002795 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002796 ret = -ECANCELED;
2797 goto error;
2798 }
2799
Chris Masonec44a352008-04-28 15:29:52 -04002800 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2801 if (ret < 0)
2802 goto error;
2803
2804 /*
2805 * this shouldn't happen, it means the last relocate
2806 * failed
2807 */
2808 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002809 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002810
2811 ret = btrfs_previous_item(chunk_root, path, 0,
2812 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002813 if (ret) {
2814 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002815 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002816 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002817
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002818 leaf = path->nodes[0];
2819 slot = path->slots[0];
2820 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2821
Chris Masonec44a352008-04-28 15:29:52 -04002822 if (found_key.objectid != key.objectid)
2823 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002824
Chris Masonec44a352008-04-28 15:29:52 -04002825 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002826 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002827 break;
2828
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002829 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2830
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002831 if (!counting) {
2832 spin_lock(&fs_info->balance_lock);
2833 bctl->stat.considered++;
2834 spin_unlock(&fs_info->balance_lock);
2835 }
2836
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002837 ret = should_balance_chunk(chunk_root, leaf, chunk,
2838 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002839 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002840 if (!ret)
2841 goto loop;
2842
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002843 if (counting) {
2844 spin_lock(&fs_info->balance_lock);
2845 bctl->stat.expected++;
2846 spin_unlock(&fs_info->balance_lock);
2847 goto loop;
2848 }
2849
Chris Masonec44a352008-04-28 15:29:52 -04002850 ret = btrfs_relocate_chunk(chunk_root,
2851 chunk_root->root_key.objectid,
2852 found_key.objectid,
2853 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002854 if (ret && ret != -ENOSPC)
2855 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002856 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002857 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002858 } else {
2859 spin_lock(&fs_info->balance_lock);
2860 bctl->stat.completed++;
2861 spin_unlock(&fs_info->balance_lock);
2862 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002863loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002864 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002865 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002866
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002867 if (counting) {
2868 btrfs_release_path(path);
2869 counting = false;
2870 goto again;
2871 }
Chris Masonec44a352008-04-28 15:29:52 -04002872error:
2873 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002874 if (enospc_errors) {
2875 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2876 enospc_errors);
2877 if (!ret)
2878 ret = -ENOSPC;
2879 }
2880
Chris Masonec44a352008-04-28 15:29:52 -04002881 return ret;
2882}
2883
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002884/**
2885 * alloc_profile_is_valid - see if a given profile is valid and reduced
2886 * @flags: profile to validate
2887 * @extended: if true @flags is treated as an extended profile
2888 */
2889static int alloc_profile_is_valid(u64 flags, int extended)
2890{
2891 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2892 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2893
2894 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2895
2896 /* 1) check that all other bits are zeroed */
2897 if (flags & ~mask)
2898 return 0;
2899
2900 /* 2) see if profile is reduced */
2901 if (flags == 0)
2902 return !extended; /* "0" is valid for usual profiles */
2903
2904 /* true if exactly one bit set */
2905 return (flags & (flags - 1)) == 0;
2906}
2907
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002908static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2909{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002910 /* cancel requested || normal exit path */
2911 return atomic_read(&fs_info->balance_cancel_req) ||
2912 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2913 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002914}
2915
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002916static void __cancel_balance(struct btrfs_fs_info *fs_info)
2917{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002918 int ret;
2919
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002920 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002921 ret = del_balance_item(fs_info->tree_root);
2922 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002923}
2924
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002925void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002926 struct btrfs_ioctl_balance_args *bargs);
2927
2928/*
2929 * Should be called with both balance and volume mutexes held
2930 */
2931int btrfs_balance(struct btrfs_balance_control *bctl,
2932 struct btrfs_ioctl_balance_args *bargs)
2933{
2934 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002935 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002936 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002937 int ret;
2938
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002939 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002940 atomic_read(&fs_info->balance_pause_req) ||
2941 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002942 ret = -EINVAL;
2943 goto out;
2944 }
2945
Ilya Dryomove4837f82012-03-27 17:09:17 +03002946 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2947 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2948 mixed = 1;
2949
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002950 /*
2951 * In case of mixed groups both data and meta should be picked,
2952 * and identical options should be given for both of them.
2953 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002954 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2955 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002956 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2957 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2958 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2959 printk(KERN_ERR "btrfs: with mixed groups data and "
2960 "metadata balance options must be the same\n");
2961 ret = -EINVAL;
2962 goto out;
2963 }
2964 }
2965
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002966 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2967 if (fs_info->fs_devices->num_devices == 1)
2968 allowed |= BTRFS_BLOCK_GROUP_DUP;
2969 else if (fs_info->fs_devices->num_devices < 4)
2970 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2971 else
2972 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2973 BTRFS_BLOCK_GROUP_RAID10);
2974
Ilya Dryomov6728b192012-03-27 17:09:17 +03002975 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2976 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2977 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002978 printk(KERN_ERR "btrfs: unable to start balance with target "
2979 "data profile %llu\n",
2980 (unsigned long long)bctl->data.target);
2981 ret = -EINVAL;
2982 goto out;
2983 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002984 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2985 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2986 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002987 printk(KERN_ERR "btrfs: unable to start balance with target "
2988 "metadata profile %llu\n",
2989 (unsigned long long)bctl->meta.target);
2990 ret = -EINVAL;
2991 goto out;
2992 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002993 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2994 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2995 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002996 printk(KERN_ERR "btrfs: unable to start balance with target "
2997 "system profile %llu\n",
2998 (unsigned long long)bctl->sys.target);
2999 ret = -EINVAL;
3000 goto out;
3001 }
3002
Ilya Dryomove4837f82012-03-27 17:09:17 +03003003 /* allow dup'ed data chunks only in mixed mode */
3004 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003005 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003006 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3007 ret = -EINVAL;
3008 goto out;
3009 }
3010
3011 /* allow to reduce meta or sys integrity only if force set */
3012 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3013 BTRFS_BLOCK_GROUP_RAID10;
3014 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3015 (fs_info->avail_system_alloc_bits & allowed) &&
3016 !(bctl->sys.target & allowed)) ||
3017 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3018 (fs_info->avail_metadata_alloc_bits & allowed) &&
3019 !(bctl->meta.target & allowed))) {
3020 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3021 printk(KERN_INFO "btrfs: force reducing metadata "
3022 "integrity\n");
3023 } else {
3024 printk(KERN_ERR "btrfs: balance will reduce metadata "
3025 "integrity, use force if you want this\n");
3026 ret = -EINVAL;
3027 goto out;
3028 }
3029 }
3030
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003031 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3032 int num_tolerated_disk_barrier_failures;
3033 u64 target = bctl->sys.target;
3034
3035 num_tolerated_disk_barrier_failures =
3036 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3037 if (num_tolerated_disk_barrier_failures > 0 &&
3038 (target &
3039 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3040 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3041 num_tolerated_disk_barrier_failures = 0;
3042 else if (num_tolerated_disk_barrier_failures > 1 &&
3043 (target &
3044 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3045 num_tolerated_disk_barrier_failures = 1;
3046
3047 fs_info->num_tolerated_disk_barrier_failures =
3048 num_tolerated_disk_barrier_failures;
3049 }
3050
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003051 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003052 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003053 goto out;
3054
Ilya Dryomov59641012012-01-16 22:04:48 +02003055 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3056 BUG_ON(ret == -EEXIST);
3057 set_balance_control(bctl);
3058 } else {
3059 BUG_ON(ret != -EEXIST);
3060 spin_lock(&fs_info->balance_lock);
3061 update_balance_args(bctl);
3062 spin_unlock(&fs_info->balance_lock);
3063 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003064
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003065 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003066 mutex_unlock(&fs_info->balance_mutex);
3067
3068 ret = __btrfs_balance(fs_info);
3069
3070 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003071 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003072
3073 if (bargs) {
3074 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003075 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003076 }
3077
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003078 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3079 balance_need_close(fs_info)) {
3080 __cancel_balance(fs_info);
3081 }
3082
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003083 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3084 fs_info->num_tolerated_disk_barrier_failures =
3085 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3086 }
3087
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003088 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003089
3090 return ret;
3091out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003092 if (bctl->flags & BTRFS_BALANCE_RESUME)
3093 __cancel_balance(fs_info);
3094 else
3095 kfree(bctl);
3096 return ret;
3097}
3098
3099static int balance_kthread(void *data)
3100{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003101 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003102 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003103
3104 mutex_lock(&fs_info->volume_mutex);
3105 mutex_lock(&fs_info->balance_mutex);
3106
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003107 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003108 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003109 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003110 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003111
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003112 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov59641012012-01-16 22:04:48 +02003113 mutex_unlock(&fs_info->balance_mutex);
3114 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003115
Ilya Dryomov59641012012-01-16 22:04:48 +02003116 return ret;
3117}
3118
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003119int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3120{
3121 struct task_struct *tsk;
3122
3123 spin_lock(&fs_info->balance_lock);
3124 if (!fs_info->balance_ctl) {
3125 spin_unlock(&fs_info->balance_lock);
3126 return 0;
3127 }
3128 spin_unlock(&fs_info->balance_lock);
3129
3130 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3131 printk(KERN_INFO "btrfs: force skipping balance\n");
3132 return 0;
3133 }
3134
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003135 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003136 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3137 if (IS_ERR(tsk))
3138 return PTR_ERR(tsk);
3139
3140 return 0;
3141}
3142
Ilya Dryomov68310a52012-06-22 12:24:12 -06003143int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003144{
Ilya Dryomov59641012012-01-16 22:04:48 +02003145 struct btrfs_balance_control *bctl;
3146 struct btrfs_balance_item *item;
3147 struct btrfs_disk_balance_args disk_bargs;
3148 struct btrfs_path *path;
3149 struct extent_buffer *leaf;
3150 struct btrfs_key key;
3151 int ret;
3152
3153 path = btrfs_alloc_path();
3154 if (!path)
3155 return -ENOMEM;
3156
Ilya Dryomov68310a52012-06-22 12:24:12 -06003157 key.objectid = BTRFS_BALANCE_OBJECTID;
3158 key.type = BTRFS_BALANCE_ITEM_KEY;
3159 key.offset = 0;
3160
3161 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3162 if (ret < 0)
3163 goto out;
3164 if (ret > 0) { /* ret = -ENOENT; */
3165 ret = 0;
3166 goto out;
3167 }
3168
Ilya Dryomov59641012012-01-16 22:04:48 +02003169 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3170 if (!bctl) {
3171 ret = -ENOMEM;
3172 goto out;
3173 }
3174
Ilya Dryomov59641012012-01-16 22:04:48 +02003175 leaf = path->nodes[0];
3176 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3177
Ilya Dryomov68310a52012-06-22 12:24:12 -06003178 bctl->fs_info = fs_info;
3179 bctl->flags = btrfs_balance_flags(leaf, item);
3180 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003181
3182 btrfs_balance_data(leaf, item, &disk_bargs);
3183 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3184 btrfs_balance_meta(leaf, item, &disk_bargs);
3185 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3186 btrfs_balance_sys(leaf, item, &disk_bargs);
3187 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3188
Ilya Dryomov68310a52012-06-22 12:24:12 -06003189 mutex_lock(&fs_info->volume_mutex);
3190 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003191
Ilya Dryomov68310a52012-06-22 12:24:12 -06003192 set_balance_control(bctl);
3193
3194 mutex_unlock(&fs_info->balance_mutex);
3195 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003196out:
3197 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003198 return ret;
3199}
3200
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003201int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3202{
3203 int ret = 0;
3204
3205 mutex_lock(&fs_info->balance_mutex);
3206 if (!fs_info->balance_ctl) {
3207 mutex_unlock(&fs_info->balance_mutex);
3208 return -ENOTCONN;
3209 }
3210
3211 if (atomic_read(&fs_info->balance_running)) {
3212 atomic_inc(&fs_info->balance_pause_req);
3213 mutex_unlock(&fs_info->balance_mutex);
3214
3215 wait_event(fs_info->balance_wait_q,
3216 atomic_read(&fs_info->balance_running) == 0);
3217
3218 mutex_lock(&fs_info->balance_mutex);
3219 /* we are good with balance_ctl ripped off from under us */
3220 BUG_ON(atomic_read(&fs_info->balance_running));
3221 atomic_dec(&fs_info->balance_pause_req);
3222 } else {
3223 ret = -ENOTCONN;
3224 }
3225
3226 mutex_unlock(&fs_info->balance_mutex);
3227 return ret;
3228}
3229
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003230int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3231{
3232 mutex_lock(&fs_info->balance_mutex);
3233 if (!fs_info->balance_ctl) {
3234 mutex_unlock(&fs_info->balance_mutex);
3235 return -ENOTCONN;
3236 }
3237
3238 atomic_inc(&fs_info->balance_cancel_req);
3239 /*
3240 * if we are running just wait and return, balance item is
3241 * deleted in btrfs_balance in this case
3242 */
3243 if (atomic_read(&fs_info->balance_running)) {
3244 mutex_unlock(&fs_info->balance_mutex);
3245 wait_event(fs_info->balance_wait_q,
3246 atomic_read(&fs_info->balance_running) == 0);
3247 mutex_lock(&fs_info->balance_mutex);
3248 } else {
3249 /* __cancel_balance needs volume_mutex */
3250 mutex_unlock(&fs_info->balance_mutex);
3251 mutex_lock(&fs_info->volume_mutex);
3252 mutex_lock(&fs_info->balance_mutex);
3253
3254 if (fs_info->balance_ctl)
3255 __cancel_balance(fs_info);
3256
3257 mutex_unlock(&fs_info->volume_mutex);
3258 }
3259
3260 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3261 atomic_dec(&fs_info->balance_cancel_req);
3262 mutex_unlock(&fs_info->balance_mutex);
3263 return 0;
3264}
3265
Chris Mason8f18cf12008-04-25 16:53:30 -04003266/*
3267 * shrinking a device means finding all of the device extents past
3268 * the new size, and then following the back refs to the chunks.
3269 * The chunk relocation code actually frees the device extent
3270 */
3271int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3272{
3273 struct btrfs_trans_handle *trans;
3274 struct btrfs_root *root = device->dev_root;
3275 struct btrfs_dev_extent *dev_extent = NULL;
3276 struct btrfs_path *path;
3277 u64 length;
3278 u64 chunk_tree;
3279 u64 chunk_objectid;
3280 u64 chunk_offset;
3281 int ret;
3282 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003283 int failed = 0;
3284 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003285 struct extent_buffer *l;
3286 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003287 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003288 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003289 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003290 u64 diff = device->total_bytes - new_size;
3291
Stefan Behrens63a212a2012-11-05 18:29:28 +01003292 if (device->is_tgtdev_for_dev_replace)
3293 return -EINVAL;
3294
Chris Mason8f18cf12008-04-25 16:53:30 -04003295 path = btrfs_alloc_path();
3296 if (!path)
3297 return -ENOMEM;
3298
Chris Mason8f18cf12008-04-25 16:53:30 -04003299 path->reada = 2;
3300
Chris Mason7d9eb122008-07-08 14:19:17 -04003301 lock_chunks(root);
3302
Chris Mason8f18cf12008-04-25 16:53:30 -04003303 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003304 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003305 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003306 spin_lock(&root->fs_info->free_chunk_lock);
3307 root->fs_info->free_chunk_space -= diff;
3308 spin_unlock(&root->fs_info->free_chunk_lock);
3309 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003310 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003311
Josef Bacikba1bf482009-09-11 16:11:19 -04003312again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003313 key.objectid = device->devid;
3314 key.offset = (u64)-1;
3315 key.type = BTRFS_DEV_EXTENT_KEY;
3316
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003317 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3319 if (ret < 0)
3320 goto done;
3321
3322 ret = btrfs_previous_item(root, path, 0, key.type);
3323 if (ret < 0)
3324 goto done;
3325 if (ret) {
3326 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003327 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003328 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003329 }
3330
3331 l = path->nodes[0];
3332 slot = path->slots[0];
3333 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3334
Josef Bacikba1bf482009-09-11 16:11:19 -04003335 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003336 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003337 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003338 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003339
3340 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3341 length = btrfs_dev_extent_length(l, dev_extent);
3342
Josef Bacikba1bf482009-09-11 16:11:19 -04003343 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003344 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003345 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003346 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003347
3348 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3349 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3350 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003351 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003352
3353 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3354 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003355 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003356 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003357 if (ret == -ENOSPC)
3358 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003359 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003360
3361 if (failed && !retried) {
3362 failed = 0;
3363 retried = true;
3364 goto again;
3365 } else if (failed && retried) {
3366 ret = -ENOSPC;
3367 lock_chunks(root);
3368
3369 device->total_bytes = old_size;
3370 if (device->writeable)
3371 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003372 spin_lock(&root->fs_info->free_chunk_lock);
3373 root->fs_info->free_chunk_space += diff;
3374 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003375 unlock_chunks(root);
3376 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003377 }
3378
Chris Balld6397ba2009-04-27 07:29:03 -04003379 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003380 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003381 if (IS_ERR(trans)) {
3382 ret = PTR_ERR(trans);
3383 goto done;
3384 }
3385
Chris Balld6397ba2009-04-27 07:29:03 -04003386 lock_chunks(root);
3387
3388 device->disk_total_bytes = new_size;
3389 /* Now btrfs_update_device() will change the on-disk size. */
3390 ret = btrfs_update_device(trans, device);
3391 if (ret) {
3392 unlock_chunks(root);
3393 btrfs_end_transaction(trans, root);
3394 goto done;
3395 }
3396 WARN_ON(diff > old_total);
3397 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3398 unlock_chunks(root);
3399 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003400done:
3401 btrfs_free_path(path);
3402 return ret;
3403}
3404
Li Zefan125ccb02011-12-08 15:07:24 +08003405static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003406 struct btrfs_key *key,
3407 struct btrfs_chunk *chunk, int item_size)
3408{
David Sterba6c417612011-04-13 15:41:04 +02003409 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003410 struct btrfs_disk_key disk_key;
3411 u32 array_size;
3412 u8 *ptr;
3413
3414 array_size = btrfs_super_sys_array_size(super_copy);
3415 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3416 return -EFBIG;
3417
3418 ptr = super_copy->sys_chunk_array + array_size;
3419 btrfs_cpu_key_to_disk(&disk_key, key);
3420 memcpy(ptr, &disk_key, sizeof(disk_key));
3421 ptr += sizeof(disk_key);
3422 memcpy(ptr, chunk, item_size);
3423 item_size += sizeof(disk_key);
3424 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3425 return 0;
3426}
3427
Miao Xieb2117a32011-01-05 10:07:28 +00003428/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003429 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003430 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003431static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003432{
Arne Jansen73c5de02011-04-12 12:07:57 +02003433 const struct btrfs_device_info *di_a = a;
3434 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003435
Arne Jansen73c5de02011-04-12 12:07:57 +02003436 if (di_a->max_avail > di_b->max_avail)
3437 return -1;
3438 if (di_a->max_avail < di_b->max_avail)
3439 return 1;
3440 if (di_a->total_avail > di_b->total_avail)
3441 return -1;
3442 if (di_a->total_avail < di_b->total_avail)
3443 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003444 return 0;
3445}
3446
3447static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3448 struct btrfs_root *extent_root,
3449 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003450 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003451 u64 start, u64 type)
3452{
3453 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003454 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3455 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003456 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003457 struct extent_map_tree *em_tree;
3458 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003459 struct btrfs_device_info *devices_info = NULL;
3460 u64 total_avail;
3461 int num_stripes; /* total number of stripes to allocate */
3462 int sub_stripes; /* sub_stripes info for map */
3463 int dev_stripes; /* stripes per dev */
3464 int devs_max; /* max devs to use */
3465 int devs_min; /* min devs needed */
3466 int devs_increment; /* ndevs has to be a multiple of this */
3467 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003468 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003469 u64 max_stripe_size;
3470 u64 max_chunk_size;
3471 u64 stripe_size;
3472 u64 num_bytes;
3473 int ndevs;
3474 int i;
3475 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003476
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003477 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003478
Miao Xieb2117a32011-01-05 10:07:28 +00003479 if (list_empty(&fs_devices->alloc_list))
3480 return -ENOSPC;
3481
Arne Jansen73c5de02011-04-12 12:07:57 +02003482 sub_stripes = 1;
3483 dev_stripes = 1;
3484 devs_increment = 1;
3485 ncopies = 1;
3486 devs_max = 0; /* 0 == as many as possible */
3487 devs_min = 1;
3488
3489 /*
3490 * define the properties of each RAID type.
3491 * FIXME: move this to a global table and use it in all RAID
3492 * calculation code
3493 */
3494 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3495 dev_stripes = 2;
3496 ncopies = 2;
3497 devs_max = 1;
3498 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3499 devs_min = 2;
3500 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3501 devs_increment = 2;
3502 ncopies = 2;
3503 devs_max = 2;
3504 devs_min = 2;
3505 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3506 sub_stripes = 2;
3507 devs_increment = 2;
3508 ncopies = 2;
3509 devs_min = 4;
3510 } else {
3511 devs_max = 1;
3512 }
3513
3514 if (type & BTRFS_BLOCK_GROUP_DATA) {
3515 max_stripe_size = 1024 * 1024 * 1024;
3516 max_chunk_size = 10 * max_stripe_size;
3517 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003518 /* for larger filesystems, use larger metadata chunks */
3519 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3520 max_stripe_size = 1024 * 1024 * 1024;
3521 else
3522 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003523 max_chunk_size = max_stripe_size;
3524 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003525 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003526 max_chunk_size = 2 * max_stripe_size;
3527 } else {
3528 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3529 type);
3530 BUG_ON(1);
3531 }
3532
3533 /* we don't want a chunk larger than 10% of writeable space */
3534 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3535 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003536
3537 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3538 GFP_NOFS);
3539 if (!devices_info)
3540 return -ENOMEM;
3541
Arne Jansen73c5de02011-04-12 12:07:57 +02003542 cur = fs_devices->alloc_list.next;
3543
3544 /*
3545 * in the first pass through the devices list, we gather information
3546 * about the available holes on each device.
3547 */
3548 ndevs = 0;
3549 while (cur != &fs_devices->alloc_list) {
3550 struct btrfs_device *device;
3551 u64 max_avail;
3552 u64 dev_offset;
3553
3554 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3555
3556 cur = cur->next;
3557
3558 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003559 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003560 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003561 continue;
3562 }
3563
Stefan Behrens63a212a2012-11-05 18:29:28 +01003564 if (!device->in_fs_metadata ||
3565 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003566 continue;
3567
3568 if (device->total_bytes > device->bytes_used)
3569 total_avail = device->total_bytes - device->bytes_used;
3570 else
3571 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003572
3573 /* If there is no space on this device, skip it. */
3574 if (total_avail == 0)
3575 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003576
Li Zefan125ccb02011-12-08 15:07:24 +08003577 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003578 max_stripe_size * dev_stripes,
3579 &dev_offset, &max_avail);
3580 if (ret && ret != -ENOSPC)
3581 goto error;
3582
3583 if (ret == 0)
3584 max_avail = max_stripe_size * dev_stripes;
3585
3586 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3587 continue;
3588
3589 devices_info[ndevs].dev_offset = dev_offset;
3590 devices_info[ndevs].max_avail = max_avail;
3591 devices_info[ndevs].total_avail = total_avail;
3592 devices_info[ndevs].dev = device;
3593 ++ndevs;
3594 }
3595
3596 /*
3597 * now sort the devices by hole size / available space
3598 */
3599 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3600 btrfs_cmp_device_info, NULL);
3601
3602 /* round down to number of usable stripes */
3603 ndevs -= ndevs % devs_increment;
3604
3605 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3606 ret = -ENOSPC;
3607 goto error;
3608 }
3609
3610 if (devs_max && ndevs > devs_max)
3611 ndevs = devs_max;
3612 /*
3613 * the primary goal is to maximize the number of stripes, so use as many
3614 * devices as possible, even if the stripes are not maximum sized.
3615 */
3616 stripe_size = devices_info[ndevs-1].max_avail;
3617 num_stripes = ndevs * dev_stripes;
3618
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003619 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003620 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003621 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003622 }
3623
3624 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003625
3626 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003627 do_div(stripe_size, BTRFS_STRIPE_LEN);
3628 stripe_size *= BTRFS_STRIPE_LEN;
3629
Miao Xieb2117a32011-01-05 10:07:28 +00003630 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3631 if (!map) {
3632 ret = -ENOMEM;
3633 goto error;
3634 }
3635 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003636
Arne Jansen73c5de02011-04-12 12:07:57 +02003637 for (i = 0; i < ndevs; ++i) {
3638 for (j = 0; j < dev_stripes; ++j) {
3639 int s = i * dev_stripes + j;
3640 map->stripes[s].dev = devices_info[i].dev;
3641 map->stripes[s].physical = devices_info[i].dev_offset +
3642 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003643 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003644 }
Chris Mason593060d2008-03-25 16:50:33 -04003645 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003646 map->stripe_len = BTRFS_STRIPE_LEN;
3647 map->io_align = BTRFS_STRIPE_LEN;
3648 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003649 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003650 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003651
Yan Zheng2b820322008-11-17 21:11:30 -05003652 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003653 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003654
Arne Jansen73c5de02011-04-12 12:07:57 +02003655 *stripe_size_out = stripe_size;
3656 *num_bytes_out = num_bytes;
3657
3658 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003659
David Sterba172ddd62011-04-21 00:48:27 +02003660 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003661 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003662 ret = -ENOMEM;
3663 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003664 }
Chris Mason0b86a832008-03-24 15:01:56 -04003665 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003666 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003667 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003668 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003669 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003670
Chris Mason0b86a832008-03-24 15:01:56 -04003671 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003672 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003673 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003674 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003675 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003676 if (ret)
3677 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003678
3679 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3680 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003681 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003682 if (ret)
3683 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003684
Arne Jansen73c5de02011-04-12 12:07:57 +02003685 for (i = 0; i < map->num_stripes; ++i) {
3686 struct btrfs_device *device;
3687 u64 dev_offset;
3688
3689 device = map->stripes[i].dev;
3690 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003691
3692 ret = btrfs_alloc_dev_extent(trans, device,
3693 info->chunk_root->root_key.objectid,
3694 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003695 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003696 if (ret) {
3697 btrfs_abort_transaction(trans, extent_root, ret);
3698 goto error;
3699 }
Yan Zheng2b820322008-11-17 21:11:30 -05003700 }
3701
Miao Xieb2117a32011-01-05 10:07:28 +00003702 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003703 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003704
3705error:
3706 kfree(map);
3707 kfree(devices_info);
3708 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003709}
3710
3711static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3712 struct btrfs_root *extent_root,
3713 struct map_lookup *map, u64 chunk_offset,
3714 u64 chunk_size, u64 stripe_size)
3715{
3716 u64 dev_offset;
3717 struct btrfs_key key;
3718 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3719 struct btrfs_device *device;
3720 struct btrfs_chunk *chunk;
3721 struct btrfs_stripe *stripe;
3722 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3723 int index = 0;
3724 int ret;
3725
3726 chunk = kzalloc(item_size, GFP_NOFS);
3727 if (!chunk)
3728 return -ENOMEM;
3729
3730 index = 0;
3731 while (index < map->num_stripes) {
3732 device = map->stripes[index].dev;
3733 device->bytes_used += stripe_size;
3734 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003735 if (ret)
3736 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003737 index++;
3738 }
3739
Josef Bacik2bf64752011-09-26 17:12:22 -04003740 spin_lock(&extent_root->fs_info->free_chunk_lock);
3741 extent_root->fs_info->free_chunk_space -= (stripe_size *
3742 map->num_stripes);
3743 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3744
Yan Zheng2b820322008-11-17 21:11:30 -05003745 index = 0;
3746 stripe = &chunk->stripe;
3747 while (index < map->num_stripes) {
3748 device = map->stripes[index].dev;
3749 dev_offset = map->stripes[index].physical;
3750
3751 btrfs_set_stack_stripe_devid(stripe, device->devid);
3752 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3753 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3754 stripe++;
3755 index++;
3756 }
3757
3758 btrfs_set_stack_chunk_length(chunk, chunk_size);
3759 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3760 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3761 btrfs_set_stack_chunk_type(chunk, map->type);
3762 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3763 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3764 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3765 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3766 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3767
3768 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3769 key.type = BTRFS_CHUNK_ITEM_KEY;
3770 key.offset = chunk_offset;
3771
3772 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003773
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003774 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3775 /*
3776 * TODO: Cleanup of inserted chunk root in case of
3777 * failure.
3778 */
Li Zefan125ccb02011-12-08 15:07:24 +08003779 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003780 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003781 }
liubo1abe9b82011-03-24 11:18:59 +00003782
Mark Fasheh3acd3952011-09-08 17:40:01 -07003783out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003784 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003785 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003786}
3787
3788/*
3789 * Chunk allocation falls into two parts. The first part does works
3790 * that make the new allocated chunk useable, but not do any operation
3791 * that modifies the chunk tree. The second part does the works that
3792 * require modifying the chunk tree. This division is important for the
3793 * bootstrap process of adding storage to a seed btrfs.
3794 */
3795int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3796 struct btrfs_root *extent_root, u64 type)
3797{
3798 u64 chunk_offset;
3799 u64 chunk_size;
3800 u64 stripe_size;
3801 struct map_lookup *map;
3802 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3803 int ret;
3804
3805 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3806 &chunk_offset);
3807 if (ret)
3808 return ret;
3809
3810 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3811 &stripe_size, chunk_offset, type);
3812 if (ret)
3813 return ret;
3814
3815 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3816 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003817 if (ret)
3818 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003819 return 0;
3820}
3821
Chris Masond3977122009-01-05 21:25:51 -05003822static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003823 struct btrfs_root *root,
3824 struct btrfs_device *device)
3825{
3826 u64 chunk_offset;
3827 u64 sys_chunk_offset;
3828 u64 chunk_size;
3829 u64 sys_chunk_size;
3830 u64 stripe_size;
3831 u64 sys_stripe_size;
3832 u64 alloc_profile;
3833 struct map_lookup *map;
3834 struct map_lookup *sys_map;
3835 struct btrfs_fs_info *fs_info = root->fs_info;
3836 struct btrfs_root *extent_root = fs_info->extent_root;
3837 int ret;
3838
3839 ret = find_next_chunk(fs_info->chunk_root,
3840 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003841 if (ret)
3842 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003843
3844 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003845 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003846 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3847
3848 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3849 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003850 if (ret)
3851 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003852
3853 sys_chunk_offset = chunk_offset + chunk_size;
3854
3855 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003856 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003857 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3858
3859 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3860 &sys_chunk_size, &sys_stripe_size,
3861 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06003862 if (ret) {
3863 btrfs_abort_transaction(trans, root, ret);
3864 goto out;
3865 }
Yan Zheng2b820322008-11-17 21:11:30 -05003866
3867 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06003868 if (ret) {
3869 btrfs_abort_transaction(trans, root, ret);
3870 goto out;
3871 }
Yan Zheng2b820322008-11-17 21:11:30 -05003872
3873 /*
3874 * Modifying chunk tree needs allocating new blocks from both
3875 * system block group and metadata block group. So we only can
3876 * do operations require modifying the chunk tree after both
3877 * block groups were created.
3878 */
3879 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3880 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06003881 if (ret) {
3882 btrfs_abort_transaction(trans, root, ret);
3883 goto out;
3884 }
Yan Zheng2b820322008-11-17 21:11:30 -05003885
3886 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3887 sys_chunk_offset, sys_chunk_size,
3888 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003889 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06003890 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003891
David Sterba005d6422012-09-18 07:52:32 -06003892out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003893
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003894 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003895}
3896
3897int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3898{
3899 struct extent_map *em;
3900 struct map_lookup *map;
3901 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3902 int readonly = 0;
3903 int i;
3904
Chris Mason890871b2009-09-02 16:24:52 -04003905 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003906 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003907 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003908 if (!em)
3909 return 1;
3910
Josef Bacikf48b9072010-01-27 02:07:59 +00003911 if (btrfs_test_opt(root, DEGRADED)) {
3912 free_extent_map(em);
3913 return 0;
3914 }
3915
Yan Zheng2b820322008-11-17 21:11:30 -05003916 map = (struct map_lookup *)em->bdev;
3917 for (i = 0; i < map->num_stripes; i++) {
3918 if (!map->stripes[i].dev->writeable) {
3919 readonly = 1;
3920 break;
3921 }
3922 }
3923 free_extent_map(em);
3924 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003925}
3926
3927void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3928{
David Sterbaa8067e02011-04-21 00:34:43 +02003929 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003930}
3931
3932void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3933{
3934 struct extent_map *em;
3935
Chris Masond3977122009-01-05 21:25:51 -05003936 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003937 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003938 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3939 if (em)
3940 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003941 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003942 if (!em)
3943 break;
3944 kfree(em->bdev);
3945 /* once for us */
3946 free_extent_map(em);
3947 /* once for the tree */
3948 free_extent_map(em);
3949 }
3950}
3951
Stefan Behrens5d964052012-11-05 14:59:07 +01003952int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04003953{
Stefan Behrens5d964052012-11-05 14:59:07 +01003954 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04003955 struct extent_map *em;
3956 struct map_lookup *map;
3957 struct extent_map_tree *em_tree = &map_tree->map_tree;
3958 int ret;
3959
Chris Mason890871b2009-09-02 16:24:52 -04003960 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003961 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003962 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003963 BUG_ON(!em);
3964
3965 BUG_ON(em->start > logical || em->start + em->len < logical);
3966 map = (struct map_lookup *)em->bdev;
3967 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3968 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003969 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3970 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003971 else
3972 ret = 1;
3973 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003974 return ret;
3975}
3976
Chris Masondfe25022008-05-13 13:46:40 -04003977static int find_live_mirror(struct map_lookup *map, int first, int num,
3978 int optimal)
3979{
3980 int i;
3981 if (map->stripes[optimal].dev->bdev)
3982 return optimal;
3983 for (i = first; i < first + num; i++) {
3984 if (map->stripes[i].dev->bdev)
3985 return i;
3986 }
3987 /* we couldn't find one that doesn't fail. Just return something
3988 * and the io error handling code will clean up eventually
3989 */
3990 return optimal;
3991}
3992
Stefan Behrens3ec706c2012-11-05 15:46:42 +01003993static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04003994 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003995 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003996 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003997{
3998 struct extent_map *em;
3999 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004000 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004001 struct extent_map_tree *em_tree = &map_tree->map_tree;
4002 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004003 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004004 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004005 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004006 u64 stripe_nr_orig;
4007 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04004008 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004009 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004010 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004011 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004012 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004013 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004014
Chris Mason890871b2009-09-02 16:24:52 -04004015 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004016 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004017 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004018
Chris Mason3b951512008-04-17 11:29:12 -04004019 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004020 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004021 (unsigned long long)logical,
4022 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04004023 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04004024 }
Chris Mason0b86a832008-03-24 15:01:56 -04004025
4026 BUG_ON(em->start > logical || em->start + em->len < logical);
4027 map = (struct map_lookup *)em->bdev;
4028 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004029
Chris Masonf1885912008-04-09 16:28:12 -04004030 if (mirror_num > map->num_stripes)
4031 mirror_num = 0;
4032
Chris Mason593060d2008-03-25 16:50:33 -04004033 stripe_nr = offset;
4034 /*
4035 * stripe_nr counts the total number of stripes we have to stride
4036 * to get to this block
4037 */
4038 do_div(stripe_nr, map->stripe_len);
4039
4040 stripe_offset = stripe_nr * map->stripe_len;
4041 BUG_ON(offset < stripe_offset);
4042
4043 /* stripe_offset is the offset of this block in its stripe*/
4044 stripe_offset = offset - stripe_offset;
4045
Li Dongyangfce3bb92011-03-24 10:24:26 +00004046 if (rw & REQ_DISCARD)
4047 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02004048 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04004049 /* we limit the length of each bio to what fits in a stripe */
4050 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00004051 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04004052 } else {
4053 *length = em->len - offset;
4054 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004055
Jan Schmidta1d3c472011-08-04 17:15:33 +02004056 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004057 goto out;
4058
Chris Masonf2d8d742008-04-21 10:03:05 -04004059 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004060 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004061 stripe_nr_orig = stripe_nr;
4062 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
4063 (~(map->stripe_len - 1));
4064 do_div(stripe_nr_end, map->stripe_len);
4065 stripe_end_offset = stripe_nr_end * map->stripe_len -
4066 (offset + *length);
4067 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4068 if (rw & REQ_DISCARD)
4069 num_stripes = min_t(u64, map->num_stripes,
4070 stripe_nr_end - stripe_nr_orig);
4071 stripe_index = do_div(stripe_nr, map->num_stripes);
4072 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07004073 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04004074 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004075 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004076 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004077 else {
4078 stripe_index = find_live_mirror(map, 0,
4079 map->num_stripes,
4080 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004081 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004082 }
Chris Mason2fff7342008-04-29 14:12:09 -04004083
Chris Mason611f0e02008-04-03 16:29:03 -04004084 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004085 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004086 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004087 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004088 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004089 } else {
4090 mirror_num = 1;
4091 }
Chris Mason2fff7342008-04-29 14:12:09 -04004092
Chris Mason321aecc2008-04-16 10:49:51 -04004093 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4094 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004095
4096 stripe_index = do_div(stripe_nr, factor);
4097 stripe_index *= map->sub_stripes;
4098
Jens Axboe7eaceac2011-03-10 08:52:07 +01004099 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04004100 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004101 else if (rw & REQ_DISCARD)
4102 num_stripes = min_t(u64, map->sub_stripes *
4103 (stripe_nr_end - stripe_nr_orig),
4104 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004105 else if (mirror_num)
4106 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004107 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004108 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04004109 stripe_index = find_live_mirror(map, stripe_index,
4110 map->sub_stripes, stripe_index +
4111 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04004112 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004113 }
Chris Mason8790d502008-04-03 16:29:03 -04004114 } else {
4115 /*
4116 * after this do_div call, stripe_nr is the number of stripes
4117 * on this device we have to walk to find the data, and
4118 * stripe_index is the number of our device in the stripe array
4119 */
4120 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004121 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004122 }
Chris Mason593060d2008-03-25 16:50:33 -04004123 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004124
Li Zefande11cc12011-12-01 12:55:47 +08004125 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
4126 if (!bbio) {
4127 ret = -ENOMEM;
4128 goto out;
4129 }
4130 atomic_set(&bbio->error, 0);
4131
Li Dongyangfce3bb92011-03-24 10:24:26 +00004132 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004133 int factor = 0;
4134 int sub_stripes = 0;
4135 u64 stripes_per_dev = 0;
4136 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004137 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004138
4139 if (map->type &
4140 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4141 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4142 sub_stripes = 1;
4143 else
4144 sub_stripes = map->sub_stripes;
4145
4146 factor = map->num_stripes / sub_stripes;
4147 stripes_per_dev = div_u64_rem(stripe_nr_end -
4148 stripe_nr_orig,
4149 factor,
4150 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004151 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4152 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004153 }
4154
Li Dongyangfce3bb92011-03-24 10:24:26 +00004155 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004156 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004157 map->stripes[stripe_index].physical +
4158 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004159 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004160
Li Zefanec9ef7a2011-12-01 14:06:42 +08004161 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4162 BTRFS_BLOCK_GROUP_RAID10)) {
4163 bbio->stripes[i].length = stripes_per_dev *
4164 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004165
Li Zefanec9ef7a2011-12-01 14:06:42 +08004166 if (i / sub_stripes < remaining_stripes)
4167 bbio->stripes[i].length +=
4168 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004169
4170 /*
4171 * Special for the first stripe and
4172 * the last stripe:
4173 *
4174 * |-------|...|-------|
4175 * |----------|
4176 * off end_off
4177 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004178 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004179 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004180 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004181
4182 if (stripe_index >= last_stripe &&
4183 stripe_index <= (last_stripe +
4184 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004185 bbio->stripes[i].length -=
4186 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004187
Li Zefanec9ef7a2011-12-01 14:06:42 +08004188 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004189 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004190 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004191 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004192
4193 stripe_index++;
4194 if (stripe_index == map->num_stripes) {
4195 /* This could only happen for RAID0/10 */
4196 stripe_index = 0;
4197 stripe_nr++;
4198 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004199 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004200 } else {
4201 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004202 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004203 map->stripes[stripe_index].physical +
4204 stripe_offset +
4205 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004206 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004207 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004208 stripe_index++;
4209 }
Chris Mason593060d2008-03-25 16:50:33 -04004210 }
Li Zefande11cc12011-12-01 12:55:47 +08004211
4212 if (rw & REQ_WRITE) {
4213 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4214 BTRFS_BLOCK_GROUP_RAID10 |
4215 BTRFS_BLOCK_GROUP_DUP)) {
4216 max_errors = 1;
4217 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004218 }
Li Zefande11cc12011-12-01 12:55:47 +08004219
4220 *bbio_ret = bbio;
4221 bbio->num_stripes = num_stripes;
4222 bbio->max_errors = max_errors;
4223 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04004224out:
Chris Mason0b86a832008-03-24 15:01:56 -04004225 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004226 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004227}
4228
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004229int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004230 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004231 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004232{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004233 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004234 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04004235}
4236
Yan Zhenga512bbf2008-12-08 16:46:26 -05004237int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4238 u64 chunk_start, u64 physical, u64 devid,
4239 u64 **logical, int *naddrs, int *stripe_len)
4240{
4241 struct extent_map_tree *em_tree = &map_tree->map_tree;
4242 struct extent_map *em;
4243 struct map_lookup *map;
4244 u64 *buf;
4245 u64 bytenr;
4246 u64 length;
4247 u64 stripe_nr;
4248 int i, j, nr = 0;
4249
Chris Mason890871b2009-09-02 16:24:52 -04004250 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004251 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004252 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004253
4254 BUG_ON(!em || em->start != chunk_start);
4255 map = (struct map_lookup *)em->bdev;
4256
4257 length = em->len;
4258 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4259 do_div(length, map->num_stripes / map->sub_stripes);
4260 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4261 do_div(length, map->num_stripes);
4262
4263 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004265
4266 for (i = 0; i < map->num_stripes; i++) {
4267 if (devid && map->stripes[i].dev->devid != devid)
4268 continue;
4269 if (map->stripes[i].physical > physical ||
4270 map->stripes[i].physical + length <= physical)
4271 continue;
4272
4273 stripe_nr = physical - map->stripes[i].physical;
4274 do_div(stripe_nr, map->stripe_len);
4275
4276 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4277 stripe_nr = stripe_nr * map->num_stripes + i;
4278 do_div(stripe_nr, map->sub_stripes);
4279 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4280 stripe_nr = stripe_nr * map->num_stripes + i;
4281 }
4282 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004283 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004284 for (j = 0; j < nr; j++) {
4285 if (buf[j] == bytenr)
4286 break;
4287 }
Chris Mason934d3752008-12-08 16:43:10 -05004288 if (j == nr) {
4289 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004290 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004291 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004292 }
4293
Yan Zhenga512bbf2008-12-08 16:46:26 -05004294 *logical = buf;
4295 *naddrs = nr;
4296 *stripe_len = map->stripe_len;
4297
4298 free_extent_map(em);
4299 return 0;
4300}
4301
Stefan Behrens442a4f62012-05-25 16:06:08 +02004302static void *merge_stripe_index_into_bio_private(void *bi_private,
4303 unsigned int stripe_index)
4304{
4305 /*
4306 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4307 * at most 1.
4308 * The alternative solution (instead of stealing bits from the
4309 * pointer) would be to allocate an intermediate structure
4310 * that contains the old private pointer plus the stripe_index.
4311 */
4312 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4313 BUG_ON(stripe_index > 3);
4314 return (void *)(((uintptr_t)bi_private) | stripe_index);
4315}
4316
4317static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4318{
4319 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4320}
4321
4322static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4323{
4324 return (unsigned int)((uintptr_t)bi_private) & 3;
4325}
4326
Jan Schmidta1d3c472011-08-04 17:15:33 +02004327static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004328{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004329 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004330 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004331
Stefan Behrens442a4f62012-05-25 16:06:08 +02004332 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004333 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004334 if (err == -EIO || err == -EREMOTEIO) {
4335 unsigned int stripe_index =
4336 extract_stripe_index_from_bio_private(
4337 bio->bi_private);
4338 struct btrfs_device *dev;
4339
4340 BUG_ON(stripe_index >= bbio->num_stripes);
4341 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004342 if (dev->bdev) {
4343 if (bio->bi_rw & WRITE)
4344 btrfs_dev_stat_inc(dev,
4345 BTRFS_DEV_STAT_WRITE_ERRS);
4346 else
4347 btrfs_dev_stat_inc(dev,
4348 BTRFS_DEV_STAT_READ_ERRS);
4349 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4350 btrfs_dev_stat_inc(dev,
4351 BTRFS_DEV_STAT_FLUSH_ERRS);
4352 btrfs_dev_stat_print_on_error(dev);
4353 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004354 }
4355 }
Chris Mason8790d502008-04-03 16:29:03 -04004356
Jan Schmidta1d3c472011-08-04 17:15:33 +02004357 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004358 is_orig_bio = 1;
4359
Jan Schmidta1d3c472011-08-04 17:15:33 +02004360 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004361 if (!is_orig_bio) {
4362 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004363 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004364 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004365 bio->bi_private = bbio->private;
4366 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004367 bio->bi_bdev = (struct block_device *)
4368 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004369 /* only send an error to the higher layers if it is
4370 * beyond the tolerance of the multi-bio
4371 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004372 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004373 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004374 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004375 /*
4376 * this bio is actually up to date, we didn't
4377 * go over the max number of errors
4378 */
4379 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004380 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004381 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004382 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004383
4384 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004385 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004386 bio_put(bio);
4387 }
Chris Mason8790d502008-04-03 16:29:03 -04004388}
4389
Chris Mason8b712842008-06-11 16:50:36 -04004390struct async_sched {
4391 struct bio *bio;
4392 int rw;
4393 struct btrfs_fs_info *info;
4394 struct btrfs_work work;
4395};
4396
4397/*
4398 * see run_scheduled_bios for a description of why bios are collected for
4399 * async submit.
4400 *
4401 * This will add one bio to the pending list for a device and make sure
4402 * the work struct is scheduled.
4403 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004404static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004405 struct btrfs_device *device,
4406 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004407{
4408 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004409 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004410
4411 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004412 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004413 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004414 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004415 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004416 return;
Chris Mason8b712842008-06-11 16:50:36 -04004417 }
4418
4419 /*
Chris Mason0986fe92008-08-15 15:34:15 -04004420 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004421 * higher layers. Otherwise, the async bio makes it appear we have
4422 * made progress against dirty pages when we've really just put it
4423 * on a queue for later
4424 */
Chris Mason0986fe92008-08-15 15:34:15 -04004425 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004426 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004427 bio->bi_next = NULL;
4428 bio->bi_rw |= rw;
4429
4430 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004431 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004432 pending_bios = &device->pending_sync_bios;
4433 else
4434 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004435
Chris Masonffbd5172009-04-20 15:50:09 -04004436 if (pending_bios->tail)
4437 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004438
Chris Masonffbd5172009-04-20 15:50:09 -04004439 pending_bios->tail = bio;
4440 if (!pending_bios->head)
4441 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004442 if (device->running_pending)
4443 should_queue = 0;
4444
4445 spin_unlock(&device->io_lock);
4446
4447 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004448 btrfs_queue_worker(&root->fs_info->submit_workers,
4449 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004450}
4451
Josef Bacikde1ee922012-10-19 16:50:56 -04004452static int bio_size_ok(struct block_device *bdev, struct bio *bio,
4453 sector_t sector)
4454{
4455 struct bio_vec *prev;
4456 struct request_queue *q = bdev_get_queue(bdev);
4457 unsigned short max_sectors = queue_max_sectors(q);
4458 struct bvec_merge_data bvm = {
4459 .bi_bdev = bdev,
4460 .bi_sector = sector,
4461 .bi_rw = bio->bi_rw,
4462 };
4463
4464 if (bio->bi_vcnt == 0) {
4465 WARN_ON(1);
4466 return 1;
4467 }
4468
4469 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
4470 if ((bio->bi_size >> 9) > max_sectors)
4471 return 0;
4472
4473 if (!q->merge_bvec_fn)
4474 return 1;
4475
4476 bvm.bi_size = bio->bi_size - prev->bv_len;
4477 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
4478 return 0;
4479 return 1;
4480}
4481
4482static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4483 struct bio *bio, u64 physical, int dev_nr,
4484 int rw, int async)
4485{
4486 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
4487
4488 bio->bi_private = bbio;
4489 bio->bi_private = merge_stripe_index_into_bio_private(
4490 bio->bi_private, (unsigned int)dev_nr);
4491 bio->bi_end_io = btrfs_end_bio;
4492 bio->bi_sector = physical >> 9;
4493#ifdef DEBUG
4494 {
4495 struct rcu_string *name;
4496
4497 rcu_read_lock();
4498 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00004499 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04004500 "(%s id %llu), size=%u\n", rw,
4501 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4502 name->str, dev->devid, bio->bi_size);
4503 rcu_read_unlock();
4504 }
4505#endif
4506 bio->bi_bdev = dev->bdev;
4507 if (async)
4508 schedule_bio(root, dev, rw, bio);
4509 else
4510 btrfsic_submit_bio(rw, bio);
4511}
4512
4513static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4514 struct bio *first_bio, struct btrfs_device *dev,
4515 int dev_nr, int rw, int async)
4516{
4517 struct bio_vec *bvec = first_bio->bi_io_vec;
4518 struct bio *bio;
4519 int nr_vecs = bio_get_nr_vecs(dev->bdev);
4520 u64 physical = bbio->stripes[dev_nr].physical;
4521
4522again:
4523 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
4524 if (!bio)
4525 return -ENOMEM;
4526
4527 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
4528 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
4529 bvec->bv_offset) < bvec->bv_len) {
4530 u64 len = bio->bi_size;
4531
4532 atomic_inc(&bbio->stripes_pending);
4533 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
4534 rw, async);
4535 physical += len;
4536 goto again;
4537 }
4538 bvec++;
4539 }
4540
4541 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
4542 return 0;
4543}
4544
4545static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
4546{
4547 atomic_inc(&bbio->error);
4548 if (atomic_dec_and_test(&bbio->stripes_pending)) {
4549 bio->bi_private = bbio->private;
4550 bio->bi_end_io = bbio->end_io;
4551 bio->bi_bdev = (struct block_device *)
4552 (unsigned long)bbio->mirror_num;
4553 bio->bi_sector = logical >> 9;
4554 kfree(bbio);
4555 bio_endio(bio, -EIO);
4556 }
4557}
4558
Chris Masonf1885912008-04-09 16:28:12 -04004559int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004560 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004561{
Chris Mason0b86a832008-03-24 15:01:56 -04004562 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004563 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004564 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004565 u64 length = 0;
4566 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004567 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004568 int dev_nr = 0;
4569 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004570 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004571
Chris Masonf2d8d742008-04-21 10:03:05 -04004572 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004573 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004574
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004575 ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004576 mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01004577 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004578 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004579
Jan Schmidta1d3c472011-08-04 17:15:33 +02004580 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004581 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004582 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05004583 "len %llu\n", (unsigned long long)logical,
4584 (unsigned long long)length,
4585 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004586 BUG();
4587 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004588
4589 bbio->orig_bio = first_bio;
4590 bbio->private = first_bio->bi_private;
4591 bbio->end_io = first_bio->bi_end_io;
4592 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004593
Chris Masond3977122009-01-05 21:25:51 -05004594 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04004595 dev = bbio->stripes[dev_nr].dev;
4596 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
4597 bbio_error(bbio, first_bio, logical);
4598 dev_nr++;
4599 continue;
4600 }
4601
4602 /*
4603 * Check and see if we're ok with this bio based on it's size
4604 * and offset with the given device.
4605 */
4606 if (!bio_size_ok(dev->bdev, first_bio,
4607 bbio->stripes[dev_nr].physical >> 9)) {
4608 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
4609 dev_nr, rw, async_submit);
4610 BUG_ON(ret);
4611 dev_nr++;
4612 continue;
4613 }
4614
Jan Schmidta1d3c472011-08-04 17:15:33 +02004615 if (dev_nr < total_devs - 1) {
4616 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004617 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004618 } else {
4619 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004620 }
Josef Bacik606686e2012-06-04 14:03:51 -04004621
Josef Bacikde1ee922012-10-19 16:50:56 -04004622 submit_stripe_bio(root, bbio, bio,
4623 bbio->stripes[dev_nr].physical, dev_nr, rw,
4624 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04004625 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004626 }
Chris Mason0b86a832008-03-24 15:01:56 -04004627 return 0;
4628}
4629
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004630struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004631 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004632{
Yan Zheng2b820322008-11-17 21:11:30 -05004633 struct btrfs_device *device;
4634 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004635
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004636 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004637 while (cur_devices) {
4638 if (!fsid ||
4639 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4640 device = __find_device(&cur_devices->devices,
4641 devid, uuid);
4642 if (device)
4643 return device;
4644 }
4645 cur_devices = cur_devices->seed;
4646 }
4647 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004648}
4649
Chris Masondfe25022008-05-13 13:46:40 -04004650static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4651 u64 devid, u8 *dev_uuid)
4652{
4653 struct btrfs_device *device;
4654 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4655
4656 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004657 if (!device)
4658 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004659 list_add(&device->dev_list,
4660 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004661 device->dev_root = root->fs_info->dev_root;
4662 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004663 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004664 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004665 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004666 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004667 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004668 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004669 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004670 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4671 return device;
4672}
4673
Chris Mason0b86a832008-03-24 15:01:56 -04004674static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4675 struct extent_buffer *leaf,
4676 struct btrfs_chunk *chunk)
4677{
4678 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4679 struct map_lookup *map;
4680 struct extent_map *em;
4681 u64 logical;
4682 u64 length;
4683 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004684 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004685 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004686 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004687 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004688
Chris Masone17cade2008-04-15 15:41:47 -04004689 logical = key->offset;
4690 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004691
Chris Mason890871b2009-09-02 16:24:52 -04004692 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004693 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004694 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004695
4696 /* already mapped? */
4697 if (em && em->start <= logical && em->start + em->len > logical) {
4698 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004699 return 0;
4700 } else if (em) {
4701 free_extent_map(em);
4702 }
Chris Mason0b86a832008-03-24 15:01:56 -04004703
David Sterba172ddd62011-04-21 00:48:27 +02004704 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004705 if (!em)
4706 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004707 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4708 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004709 if (!map) {
4710 free_extent_map(em);
4711 return -ENOMEM;
4712 }
4713
4714 em->bdev = (struct block_device *)map;
4715 em->start = logical;
4716 em->len = length;
4717 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004718 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004719
Chris Mason593060d2008-03-25 16:50:33 -04004720 map->num_stripes = num_stripes;
4721 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4722 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4723 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4724 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4725 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004726 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004727 for (i = 0; i < num_stripes; i++) {
4728 map->stripes[i].physical =
4729 btrfs_stripe_offset_nr(leaf, chunk, i);
4730 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004731 read_extent_buffer(leaf, uuid, (unsigned long)
4732 btrfs_stripe_dev_uuid_nr(chunk, i),
4733 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004734 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
4735 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004736 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004737 kfree(map);
4738 free_extent_map(em);
4739 return -EIO;
4740 }
Chris Masondfe25022008-05-13 13:46:40 -04004741 if (!map->stripes[i].dev) {
4742 map->stripes[i].dev =
4743 add_missing_dev(root, devid, uuid);
4744 if (!map->stripes[i].dev) {
4745 kfree(map);
4746 free_extent_map(em);
4747 return -EIO;
4748 }
4749 }
4750 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004751 }
4752
Chris Mason890871b2009-09-02 16:24:52 -04004753 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004754 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004755 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004756 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004757 free_extent_map(em);
4758
4759 return 0;
4760}
4761
Jeff Mahoney143bede2012-03-01 14:56:26 +01004762static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004763 struct btrfs_dev_item *dev_item,
4764 struct btrfs_device *device)
4765{
4766 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004767
4768 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004769 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4770 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004771 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4772 device->type = btrfs_device_type(leaf, dev_item);
4773 device->io_align = btrfs_device_io_align(leaf, dev_item);
4774 device->io_width = btrfs_device_io_width(leaf, dev_item);
4775 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004776 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004777
4778 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004779 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004780}
4781
Yan Zheng2b820322008-11-17 21:11:30 -05004782static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4783{
4784 struct btrfs_fs_devices *fs_devices;
4785 int ret;
4786
Li Zefanb367e472011-12-07 11:38:24 +08004787 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004788
4789 fs_devices = root->fs_info->fs_devices->seed;
4790 while (fs_devices) {
4791 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4792 ret = 0;
4793 goto out;
4794 }
4795 fs_devices = fs_devices->seed;
4796 }
4797
4798 fs_devices = find_fsid(fsid);
4799 if (!fs_devices) {
4800 ret = -ENOENT;
4801 goto out;
4802 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004803
4804 fs_devices = clone_fs_devices(fs_devices);
4805 if (IS_ERR(fs_devices)) {
4806 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004807 goto out;
4808 }
4809
Christoph Hellwig97288f22008-12-02 06:36:09 -05004810 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004811 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004812 if (ret) {
4813 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004814 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004815 }
Yan Zheng2b820322008-11-17 21:11:30 -05004816
4817 if (!fs_devices->seeding) {
4818 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004819 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004820 ret = -EINVAL;
4821 goto out;
4822 }
4823
4824 fs_devices->seed = root->fs_info->fs_devices->seed;
4825 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004826out:
Yan Zheng2b820322008-11-17 21:11:30 -05004827 return ret;
4828}
4829
Chris Mason0d81ba52008-03-24 15:02:07 -04004830static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004831 struct extent_buffer *leaf,
4832 struct btrfs_dev_item *dev_item)
4833{
4834 struct btrfs_device *device;
4835 u64 devid;
4836 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004837 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004838 u8 dev_uuid[BTRFS_UUID_SIZE];
4839
Chris Mason0b86a832008-03-24 15:01:56 -04004840 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004841 read_extent_buffer(leaf, dev_uuid,
4842 (unsigned long)btrfs_device_uuid(dev_item),
4843 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004844 read_extent_buffer(leaf, fs_uuid,
4845 (unsigned long)btrfs_device_fsid(dev_item),
4846 BTRFS_UUID_SIZE);
4847
4848 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4849 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004850 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004851 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004852 }
4853
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004854 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05004855 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004856 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004857 return -EIO;
4858
4859 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004860 printk(KERN_WARNING "warning devid %llu missing\n",
4861 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004862 device = add_missing_dev(root, devid, dev_uuid);
4863 if (!device)
4864 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004865 } else if (!device->missing) {
4866 /*
4867 * this happens when a device that was properly setup
4868 * in the device info lists suddenly goes bad.
4869 * device->bdev is NULL, and so we have to set
4870 * device->missing to one here
4871 */
4872 root->fs_info->fs_devices->missing_devices++;
4873 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004874 }
4875 }
4876
4877 if (device->fs_devices != root->fs_info->fs_devices) {
4878 BUG_ON(device->writeable);
4879 if (device->generation !=
4880 btrfs_device_generation(leaf, dev_item))
4881 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004882 }
Chris Mason0b86a832008-03-24 15:01:56 -04004883
4884 fill_device_from_item(leaf, dev_item, device);
4885 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004886 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01004887 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05004888 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004889 spin_lock(&root->fs_info->free_chunk_lock);
4890 root->fs_info->free_chunk_space += device->total_bytes -
4891 device->bytes_used;
4892 spin_unlock(&root->fs_info->free_chunk_lock);
4893 }
Chris Mason0b86a832008-03-24 15:01:56 -04004894 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004895 return ret;
4896}
4897
Yan Zhenge4404d62008-12-12 10:03:26 -05004898int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004899{
David Sterba6c417612011-04-13 15:41:04 +02004900 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004901 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004902 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004903 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004904 u8 *ptr;
4905 unsigned long sb_ptr;
4906 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004907 u32 num_stripes;
4908 u32 array_size;
4909 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004910 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004911 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004912
Yan Zhenge4404d62008-12-12 10:03:26 -05004913 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004914 BTRFS_SUPER_INFO_SIZE);
4915 if (!sb)
4916 return -ENOMEM;
4917 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004918 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004919 /*
4920 * The sb extent buffer is artifical and just used to read the system array.
4921 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4922 * pages up-to-date when the page is larger: extent does not cover the
4923 * whole page and consequently check_page_uptodate does not find all
4924 * the page's extents up-to-date (the hole beyond sb),
4925 * write_extent_buffer then triggers a WARN_ON.
4926 *
4927 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4928 * but sb spans only this function. Add an explicit SetPageUptodate call
4929 * to silence the warning eg. on PowerPC 64.
4930 */
4931 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004932 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004933
Chris Masona061fc82008-05-07 11:43:44 -04004934 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004935 array_size = btrfs_super_sys_array_size(super_copy);
4936
Chris Mason0b86a832008-03-24 15:01:56 -04004937 ptr = super_copy->sys_chunk_array;
4938 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4939 cur = 0;
4940
4941 while (cur < array_size) {
4942 disk_key = (struct btrfs_disk_key *)ptr;
4943 btrfs_disk_key_to_cpu(&key, disk_key);
4944
Chris Masona061fc82008-05-07 11:43:44 -04004945 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004946 sb_ptr += len;
4947 cur += len;
4948
Chris Mason0d81ba52008-03-24 15:02:07 -04004949 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004950 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004951 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004952 if (ret)
4953 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004954 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4955 len = btrfs_chunk_item_size(num_stripes);
4956 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004957 ret = -EIO;
4958 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004959 }
4960 ptr += len;
4961 sb_ptr += len;
4962 cur += len;
4963 }
Chris Masona061fc82008-05-07 11:43:44 -04004964 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004965 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004966}
4967
4968int btrfs_read_chunk_tree(struct btrfs_root *root)
4969{
4970 struct btrfs_path *path;
4971 struct extent_buffer *leaf;
4972 struct btrfs_key key;
4973 struct btrfs_key found_key;
4974 int ret;
4975 int slot;
4976
4977 root = root->fs_info->chunk_root;
4978
4979 path = btrfs_alloc_path();
4980 if (!path)
4981 return -ENOMEM;
4982
Li Zefanb367e472011-12-07 11:38:24 +08004983 mutex_lock(&uuid_mutex);
4984 lock_chunks(root);
4985
Chris Mason0b86a832008-03-24 15:01:56 -04004986 /* first we search for all of the device items, and then we
4987 * read in all of the chunk items. This way we can create chunk
4988 * mappings that reference all of the devices that are afound
4989 */
4990 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4991 key.offset = 0;
4992 key.type = 0;
4993again:
4994 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004995 if (ret < 0)
4996 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004997 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004998 leaf = path->nodes[0];
4999 slot = path->slots[0];
5000 if (slot >= btrfs_header_nritems(leaf)) {
5001 ret = btrfs_next_leaf(root, path);
5002 if (ret == 0)
5003 continue;
5004 if (ret < 0)
5005 goto error;
5006 break;
5007 }
5008 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5009 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5010 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5011 break;
5012 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5013 struct btrfs_dev_item *dev_item;
5014 dev_item = btrfs_item_ptr(leaf, slot,
5015 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005016 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005017 if (ret)
5018 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005019 }
5020 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5021 struct btrfs_chunk *chunk;
5022 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5023 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005024 if (ret)
5025 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005026 }
5027 path->slots[0]++;
5028 }
5029 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5030 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005031 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005032 goto again;
5033 }
Chris Mason0b86a832008-03-24 15:01:56 -04005034 ret = 0;
5035error:
Li Zefanb367e472011-12-07 11:38:24 +08005036 unlock_chunks(root);
5037 mutex_unlock(&uuid_mutex);
5038
Yan Zheng2b820322008-11-17 21:11:30 -05005039 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005040 return ret;
5041}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005042
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005043static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5044{
5045 int i;
5046
5047 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5048 btrfs_dev_stat_reset(dev, i);
5049}
5050
5051int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5052{
5053 struct btrfs_key key;
5054 struct btrfs_key found_key;
5055 struct btrfs_root *dev_root = fs_info->dev_root;
5056 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5057 struct extent_buffer *eb;
5058 int slot;
5059 int ret = 0;
5060 struct btrfs_device *device;
5061 struct btrfs_path *path = NULL;
5062 int i;
5063
5064 path = btrfs_alloc_path();
5065 if (!path) {
5066 ret = -ENOMEM;
5067 goto out;
5068 }
5069
5070 mutex_lock(&fs_devices->device_list_mutex);
5071 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5072 int item_size;
5073 struct btrfs_dev_stats_item *ptr;
5074
5075 key.objectid = 0;
5076 key.type = BTRFS_DEV_STATS_KEY;
5077 key.offset = device->devid;
5078 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5079 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005080 __btrfs_reset_dev_stats(device);
5081 device->dev_stats_valid = 1;
5082 btrfs_release_path(path);
5083 continue;
5084 }
5085 slot = path->slots[0];
5086 eb = path->nodes[0];
5087 btrfs_item_key_to_cpu(eb, &found_key, slot);
5088 item_size = btrfs_item_size_nr(eb, slot);
5089
5090 ptr = btrfs_item_ptr(eb, slot,
5091 struct btrfs_dev_stats_item);
5092
5093 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5094 if (item_size >= (1 + i) * sizeof(__le64))
5095 btrfs_dev_stat_set(device, i,
5096 btrfs_dev_stats_value(eb, ptr, i));
5097 else
5098 btrfs_dev_stat_reset(device, i);
5099 }
5100
5101 device->dev_stats_valid = 1;
5102 btrfs_dev_stat_print_on_load(device);
5103 btrfs_release_path(path);
5104 }
5105 mutex_unlock(&fs_devices->device_list_mutex);
5106
5107out:
5108 btrfs_free_path(path);
5109 return ret < 0 ? ret : 0;
5110}
5111
5112static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5113 struct btrfs_root *dev_root,
5114 struct btrfs_device *device)
5115{
5116 struct btrfs_path *path;
5117 struct btrfs_key key;
5118 struct extent_buffer *eb;
5119 struct btrfs_dev_stats_item *ptr;
5120 int ret;
5121 int i;
5122
5123 key.objectid = 0;
5124 key.type = BTRFS_DEV_STATS_KEY;
5125 key.offset = device->devid;
5126
5127 path = btrfs_alloc_path();
5128 BUG_ON(!path);
5129 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5130 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005131 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5132 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005133 goto out;
5134 }
5135
5136 if (ret == 0 &&
5137 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5138 /* need to delete old one and insert a new one */
5139 ret = btrfs_del_item(trans, dev_root, path);
5140 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005141 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5142 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005143 goto out;
5144 }
5145 ret = 1;
5146 }
5147
5148 if (ret == 1) {
5149 /* need to insert a new item */
5150 btrfs_release_path(path);
5151 ret = btrfs_insert_empty_item(trans, dev_root, path,
5152 &key, sizeof(*ptr));
5153 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005154 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5155 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005156 goto out;
5157 }
5158 }
5159
5160 eb = path->nodes[0];
5161 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5162 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5163 btrfs_set_dev_stats_value(eb, ptr, i,
5164 btrfs_dev_stat_read(device, i));
5165 btrfs_mark_buffer_dirty(eb);
5166
5167out:
5168 btrfs_free_path(path);
5169 return ret;
5170}
5171
5172/*
5173 * called from commit_transaction. Writes all changed device stats to disk.
5174 */
5175int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5176 struct btrfs_fs_info *fs_info)
5177{
5178 struct btrfs_root *dev_root = fs_info->dev_root;
5179 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5180 struct btrfs_device *device;
5181 int ret = 0;
5182
5183 mutex_lock(&fs_devices->device_list_mutex);
5184 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5185 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5186 continue;
5187
5188 ret = update_dev_stat_item(trans, dev_root, device);
5189 if (!ret)
5190 device->dev_stats_dirty = 0;
5191 }
5192 mutex_unlock(&fs_devices->device_list_mutex);
5193
5194 return ret;
5195}
5196
Stefan Behrens442a4f62012-05-25 16:06:08 +02005197void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5198{
5199 btrfs_dev_stat_inc(dev, index);
5200 btrfs_dev_stat_print_on_error(dev);
5201}
5202
5203void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5204{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005205 if (!dev->dev_stats_valid)
5206 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005207 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005208 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005209 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005210 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5211 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5212 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5213 btrfs_dev_stat_read(dev,
5214 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5215 btrfs_dev_stat_read(dev,
5216 BTRFS_DEV_STAT_GENERATION_ERRS));
5217}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005218
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005219static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5220{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005221 int i;
5222
5223 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5224 if (btrfs_dev_stat_read(dev, i) != 0)
5225 break;
5226 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5227 return; /* all values == 0, suppress message */
5228
Josef Bacik606686e2012-06-04 14:03:51 -04005229 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5230 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005231 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5232 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5233 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5234 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5235 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5236}
5237
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005238int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005239 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005240{
5241 struct btrfs_device *dev;
5242 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5243 int i;
5244
5245 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005246 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005247 mutex_unlock(&fs_devices->device_list_mutex);
5248
5249 if (!dev) {
5250 printk(KERN_WARNING
5251 "btrfs: get dev_stats failed, device not found\n");
5252 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005253 } else if (!dev->dev_stats_valid) {
5254 printk(KERN_WARNING
5255 "btrfs: get dev_stats failed, not yet valid\n");
5256 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005257 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005258 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5259 if (stats->nr_items > i)
5260 stats->values[i] =
5261 btrfs_dev_stat_read_and_reset(dev, i);
5262 else
5263 btrfs_dev_stat_reset(dev, i);
5264 }
5265 } else {
5266 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5267 if (stats->nr_items > i)
5268 stats->values[i] = btrfs_dev_stat_read(dev, i);
5269 }
5270 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5271 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5272 return 0;
5273}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005274
5275int btrfs_scratch_superblock(struct btrfs_device *device)
5276{
5277 struct buffer_head *bh;
5278 struct btrfs_super_block *disk_super;
5279
5280 bh = btrfs_read_dev_super(device->bdev);
5281 if (!bh)
5282 return -EINVAL;
5283 disk_super = (struct btrfs_super_block *)bh->b_data;
5284
5285 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5286 set_buffer_dirty(bh);
5287 sync_dirty_buffer(bh);
5288 brelse(bh);
5289
5290 return 0;
5291}