blob: 0b4e2af7954d3c209d8f1e581d4ee26c0cb60c2f [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>
Ilya Dryomov59641012012-01-16 22:04:48 +020026#include <linux/kthread.h>
Chris Mason593060d2008-03-25 16:50:33 -040027#include <asm/div64.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"
Chris Mason0b86a832008-03-24 15:01:56 -040037
Yan Zheng2b820322008-11-17 21:11:30 -050038static int init_first_rw_device(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 struct btrfs_device *device);
41static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
42
Chris Mason8a4b83c2008-03-24 15:02:07 -040043static DEFINE_MUTEX(uuid_mutex);
44static LIST_HEAD(fs_uuids);
45
Chris Mason7d9eb122008-07-08 14:19:17 -040046static void lock_chunks(struct btrfs_root *root)
47{
Chris Mason7d9eb122008-07-08 14:19:17 -040048 mutex_lock(&root->fs_info->chunk_mutex);
49}
50
51static void unlock_chunks(struct btrfs_root *root)
52{
Chris Mason7d9eb122008-07-08 14:19:17 -040053 mutex_unlock(&root->fs_info->chunk_mutex);
54}
55
Yan Zhenge4404d62008-12-12 10:03:26 -050056static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
57{
58 struct btrfs_device *device;
59 WARN_ON(fs_devices->opened);
60 while (!list_empty(&fs_devices->devices)) {
61 device = list_entry(fs_devices->devices.next,
62 struct btrfs_device, dev_list);
63 list_del(&device->dev_list);
64 kfree(device->name);
65 kfree(device);
66 }
67 kfree(fs_devices);
68}
69
Chris Mason8a4b83c2008-03-24 15:02:07 -040070int btrfs_cleanup_fs_uuids(void)
71{
72 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040073
Yan Zheng2b820322008-11-17 21:11:30 -050074 while (!list_empty(&fs_uuids)) {
75 fs_devices = list_entry(fs_uuids.next,
76 struct btrfs_fs_devices, list);
77 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050078 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040079 }
80 return 0;
81}
82
Chris Masona1b32a52008-09-05 16:09:51 -040083static noinline struct btrfs_device *__find_device(struct list_head *head,
84 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040085{
86 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040087
Qinghuang Fengc6e30872009-01-21 10:59:08 -050088 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040089 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040090 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040091 return dev;
Chris Masona4437552008-04-18 10:29:38 -040092 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040093 }
94 return NULL;
95}
96
Chris Masona1b32a52008-09-05 16:09:51 -040097static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040098{
Chris Mason8a4b83c2008-03-24 15:02:07 -040099 struct btrfs_fs_devices *fs_devices;
100
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500101 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
103 return fs_devices;
104 }
105 return NULL;
106}
107
Chris Masonffbd5172009-04-20 15:50:09 -0400108static void requeue_list(struct btrfs_pending_bios *pending_bios,
109 struct bio *head, struct bio *tail)
110{
111
112 struct bio *old_head;
113
114 old_head = pending_bios->head;
115 pending_bios->head = head;
116 if (pending_bios->tail)
117 tail->bi_next = old_head;
118 else
119 pending_bios->tail = tail;
120}
121
Chris Mason8b712842008-06-11 16:50:36 -0400122/*
123 * we try to collect pending bios for a device so we don't get a large
124 * number of procs sending bios down to the same device. This greatly
125 * improves the schedulers ability to collect and merge the bios.
126 *
127 * But, it also turns into a long list of bios to process and that is sure
128 * to eventually make the worker thread block. The solution here is to
129 * make some progress and then put this work struct back at the end of
130 * the list if the block device is congested. This way, multiple devices
131 * can make progress from a single worker thread.
132 */
Chris Masond3977122009-01-05 21:25:51 -0500133static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400134{
135 struct bio *pending;
136 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400137 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400138 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400139 struct bio *tail;
140 struct bio *cur;
141 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400142 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400143 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400144 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400145 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400146 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000147 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400148 struct blk_plug plug;
149
150 /*
151 * this function runs all the bios we've collected for
152 * a particular device. We don't want to wander off to
153 * another device without first sending all of these down.
154 * So, setup a plug here and finish it off before we return
155 */
156 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400157
Chris Masonbedf7622009-04-03 10:32:58 -0400158 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400159 fs_info = device->dev_root->fs_info;
160 limit = btrfs_async_submit_limit(fs_info);
161 limit = limit * 2 / 3;
162
Chris Mason8b712842008-06-11 16:50:36 -0400163loop:
164 spin_lock(&device->io_lock);
165
Chris Masona6837052009-02-04 09:19:41 -0500166loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400167 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400168
Chris Mason8b712842008-06-11 16:50:36 -0400169 /* take all the bios off the list at once and process them
170 * later on (without the lock held). But, remember the
171 * tail and other pointers so the bios can be properly reinserted
172 * into the list if we hit congestion
173 */
Chris Masond84275c2009-06-09 15:39:08 -0400174 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400175 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400176 force_reg = 1;
177 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400178 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400179 force_reg = 0;
180 }
Chris Masonffbd5172009-04-20 15:50:09 -0400181
182 pending = pending_bios->head;
183 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400184 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400185
186 /*
187 * if pending was null this time around, no bios need processing
188 * at all and we can stop. Otherwise it'll loop back up again
189 * and do an additional check so no bios are missed.
190 *
191 * device->running_pending is used to synchronize with the
192 * schedule_bio code.
193 */
Chris Masonffbd5172009-04-20 15:50:09 -0400194 if (device->pending_sync_bios.head == NULL &&
195 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400196 again = 0;
197 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400198 } else {
199 again = 1;
200 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400201 }
Chris Masonffbd5172009-04-20 15:50:09 -0400202
203 pending_bios->head = NULL;
204 pending_bios->tail = NULL;
205
Chris Mason8b712842008-06-11 16:50:36 -0400206 spin_unlock(&device->io_lock);
207
Chris Masond3977122009-01-05 21:25:51 -0500208 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400209
210 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400211 /* we want to work on both lists, but do more bios on the
212 * sync list than the regular list
213 */
214 if ((num_run > 32 &&
215 pending_bios != &device->pending_sync_bios &&
216 device->pending_sync_bios.head) ||
217 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
218 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400219 spin_lock(&device->io_lock);
220 requeue_list(pending_bios, pending, tail);
221 goto loop_lock;
222 }
223
Chris Mason8b712842008-06-11 16:50:36 -0400224 cur = pending;
225 pending = pending->bi_next;
226 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400227 atomic_dec(&fs_info->nr_async_bios);
228
229 if (atomic_read(&fs_info->nr_async_bios) < limit &&
230 waitqueue_active(&fs_info->async_submit_wait))
231 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400232
233 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400234
Chris Mason2ab1ba62011-08-04 14:28:36 -0400235 /*
236 * if we're doing the sync list, record that our
237 * plug has some sync requests on it
238 *
239 * If we're doing the regular list and there are
240 * sync requests sitting around, unplug before
241 * we add more
242 */
243 if (pending_bios == &device->pending_sync_bios) {
244 sync_pending = 1;
245 } else if (sync_pending) {
246 blk_finish_plug(&plug);
247 blk_start_plug(&plug);
248 sync_pending = 0;
249 }
250
Stefan Behrens21adbd52011-11-09 13:44:05 +0100251 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400252 num_run++;
253 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100254 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400255 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400256
257 /*
258 * we made progress, there is more work to do and the bdi
259 * is now congested. Back off and let other work structs
260 * run instead
261 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400262 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500263 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400264 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400265
Chris Masonb765ead2009-04-03 10:27:10 -0400266 ioc = current->io_context;
267
268 /*
269 * the main goal here is that we don't want to
270 * block if we're going to be able to submit
271 * more requests without blocking.
272 *
273 * This code does two great things, it pokes into
274 * the elevator code from a filesystem _and_
275 * it makes assumptions about how batching works.
276 */
277 if (ioc && ioc->nr_batch_requests > 0 &&
278 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
279 (last_waited == 0 ||
280 ioc->last_waited == last_waited)) {
281 /*
282 * we want to go through our batch of
283 * requests and stop. So, we copy out
284 * the ioc->last_waited time and test
285 * against it before looping
286 */
287 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100288 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400289 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400290 continue;
291 }
Chris Mason8b712842008-06-11 16:50:36 -0400292 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400293 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500294 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400295
296 spin_unlock(&device->io_lock);
297 btrfs_requeue_work(&device->work);
298 goto done;
299 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500300 /* unplug every 64 requests just for good measure */
301 if (batch_run % 64 == 0) {
302 blk_finish_plug(&plug);
303 blk_start_plug(&plug);
304 sync_pending = 0;
305 }
Chris Mason8b712842008-06-11 16:50:36 -0400306 }
Chris Masonffbd5172009-04-20 15:50:09 -0400307
Chris Mason51684082010-03-10 15:33:32 -0500308 cond_resched();
309 if (again)
310 goto loop;
311
312 spin_lock(&device->io_lock);
313 if (device->pending_bios.head || device->pending_sync_bios.head)
314 goto loop_lock;
315 spin_unlock(&device->io_lock);
316
Chris Mason8b712842008-06-11 16:50:36 -0400317done:
Chris Mason211588a2011-04-19 20:12:40 -0400318 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400319 return 0;
320}
321
Christoph Hellwigb2950862008-12-02 09:54:17 -0500322static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400323{
324 struct btrfs_device *device;
325
326 device = container_of(work, struct btrfs_device, work);
327 run_scheduled_bios(device);
328}
329
Chris Masona1b32a52008-09-05 16:09:51 -0400330static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400331 struct btrfs_super_block *disk_super,
332 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
333{
334 struct btrfs_device *device;
335 struct btrfs_fs_devices *fs_devices;
336 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000337 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400338
339 fs_devices = find_fsid(disk_super->fsid);
340 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400341 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400342 if (!fs_devices)
343 return -ENOMEM;
344 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400345 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400346 list_add(&fs_devices->list, &fs_uuids);
347 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
348 fs_devices->latest_devid = devid;
349 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400350 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400351 device = NULL;
352 } else {
Chris Masona4437552008-04-18 10:29:38 -0400353 device = __find_device(&fs_devices->devices, devid,
354 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400355 }
356 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500357 if (fs_devices->opened)
358 return -EBUSY;
359
Chris Mason8a4b83c2008-03-24 15:02:07 -0400360 device = kzalloc(sizeof(*device), GFP_NOFS);
361 if (!device) {
362 /* we can safely leave the fs_devices entry around */
363 return -ENOMEM;
364 }
365 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400366 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400367 memcpy(device->uuid, disk_super->dev_item.uuid,
368 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400369 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 device->name = kstrdup(path, GFP_NOFS);
371 if (!device->name) {
372 kfree(device);
373 return -ENOMEM;
374 }
Yan Zheng2b820322008-11-17 21:11:30 -0500375 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400376
Arne Jansen90519d62011-05-23 14:30:00 +0200377 /* init readahead state */
378 spin_lock_init(&device->reada_lock);
379 device->reada_curr_zone = NULL;
380 atomic_set(&device->reada_in_flight, 0);
381 device->reada_next = 0;
382 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
383 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
384
Chris Masone5e9a522009-06-10 15:17:02 -0400385 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000386 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400387 mutex_unlock(&fs_devices->device_list_mutex);
388
Yan Zheng2b820322008-11-17 21:11:30 -0500389 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400390 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500391 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000392 name = kstrdup(path, GFP_NOFS);
393 if (!name)
394 return -ENOMEM;
395 kfree(device->name);
396 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500397 if (device->missing) {
398 fs_devices->missing_devices--;
399 device->missing = 0;
400 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400401 }
402
403 if (found_transid > fs_devices->latest_trans) {
404 fs_devices->latest_devid = devid;
405 fs_devices->latest_trans = found_transid;
406 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400407 *fs_devices_ret = fs_devices;
408 return 0;
409}
410
Yan Zhenge4404d62008-12-12 10:03:26 -0500411static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
412{
413 struct btrfs_fs_devices *fs_devices;
414 struct btrfs_device *device;
415 struct btrfs_device *orig_dev;
416
417 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
418 if (!fs_devices)
419 return ERR_PTR(-ENOMEM);
420
421 INIT_LIST_HEAD(&fs_devices->devices);
422 INIT_LIST_HEAD(&fs_devices->alloc_list);
423 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400424 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500425 fs_devices->latest_devid = orig->latest_devid;
426 fs_devices->latest_trans = orig->latest_trans;
427 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
428
Xiao Guangrong46224702011-04-20 10:08:47 +0000429 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500430 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
431 device = kzalloc(sizeof(*device), GFP_NOFS);
432 if (!device)
433 goto error;
434
435 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400436 if (!device->name) {
437 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500438 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400439 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500440
441 device->devid = orig_dev->devid;
442 device->work.func = pending_bios_fn;
443 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500444 spin_lock_init(&device->io_lock);
445 INIT_LIST_HEAD(&device->dev_list);
446 INIT_LIST_HEAD(&device->dev_alloc_list);
447
448 list_add(&device->dev_list, &fs_devices->devices);
449 device->fs_devices = fs_devices;
450 fs_devices->num_devices++;
451 }
452 return fs_devices;
453error:
454 free_fs_devices(fs_devices);
455 return ERR_PTR(-ENOMEM);
456}
457
Chris Masondfe25022008-05-13 13:46:40 -0400458int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
459{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500460 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400461
462 mutex_lock(&uuid_mutex);
463again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000464 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500465 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500466 if (device->in_fs_metadata)
467 continue;
468
469 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100470 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500471 device->bdev = NULL;
472 fs_devices->open_devices--;
473 }
474 if (device->writeable) {
475 list_del_init(&device->dev_alloc_list);
476 device->writeable = 0;
477 fs_devices->rw_devices--;
478 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500479 list_del_init(&device->dev_list);
480 fs_devices->num_devices--;
481 kfree(device->name);
482 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400483 }
Yan Zheng2b820322008-11-17 21:11:30 -0500484
485 if (fs_devices->seed) {
486 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500487 goto again;
488 }
489
Chris Masondfe25022008-05-13 13:46:40 -0400490 mutex_unlock(&uuid_mutex);
491 return 0;
492}
Chris Masona0af4692008-05-13 16:03:06 -0400493
Xiao Guangrong1f781602011-04-20 10:09:16 +0000494static void __free_device(struct work_struct *work)
495{
496 struct btrfs_device *device;
497
498 device = container_of(work, struct btrfs_device, rcu_work);
499
500 if (device->bdev)
501 blkdev_put(device->bdev, device->mode);
502
503 kfree(device->name);
504 kfree(device);
505}
506
507static void free_device(struct rcu_head *head)
508{
509 struct btrfs_device *device;
510
511 device = container_of(head, struct btrfs_device, rcu);
512
513 INIT_WORK(&device->rcu_work, __free_device);
514 schedule_work(&device->rcu_work);
515}
516
Yan Zheng2b820322008-11-17 21:11:30 -0500517static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400518{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400519 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500520
Yan Zheng2b820322008-11-17 21:11:30 -0500521 if (--fs_devices->opened > 0)
522 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400523
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000524 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500525 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000526 struct btrfs_device *new_device;
527
528 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400529 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000530
Yan Zheng2b820322008-11-17 21:11:30 -0500531 if (device->writeable) {
532 list_del_init(&device->dev_alloc_list);
533 fs_devices->rw_devices--;
534 }
535
Josef Bacikd5e20032011-08-04 14:52:27 +0000536 if (device->can_discard)
537 fs_devices->num_can_discard--;
538
Xiao Guangrong1f781602011-04-20 10:09:16 +0000539 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
540 BUG_ON(!new_device);
541 memcpy(new_device, device, sizeof(*new_device));
542 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000543 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000544 new_device->bdev = NULL;
545 new_device->writeable = 0;
546 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000547 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000548 list_replace_rcu(&device->dev_list, &new_device->dev_list);
549
550 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400551 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000552 mutex_unlock(&fs_devices->device_list_mutex);
553
Yan Zhenge4404d62008-12-12 10:03:26 -0500554 WARN_ON(fs_devices->open_devices);
555 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500556 fs_devices->opened = 0;
557 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500558
Chris Mason8a4b83c2008-03-24 15:02:07 -0400559 return 0;
560}
561
Yan Zheng2b820322008-11-17 21:11:30 -0500562int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
563{
Yan Zhenge4404d62008-12-12 10:03:26 -0500564 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500565 int ret;
566
567 mutex_lock(&uuid_mutex);
568 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500569 if (!fs_devices->opened) {
570 seed_devices = fs_devices->seed;
571 fs_devices->seed = NULL;
572 }
Yan Zheng2b820322008-11-17 21:11:30 -0500573 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500574
575 while (seed_devices) {
576 fs_devices = seed_devices;
577 seed_devices = fs_devices->seed;
578 __btrfs_close_devices(fs_devices);
579 free_fs_devices(fs_devices);
580 }
Yan Zheng2b820322008-11-17 21:11:30 -0500581 return ret;
582}
583
Yan Zhenge4404d62008-12-12 10:03:26 -0500584static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
585 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400586{
Josef Bacikd5e20032011-08-04 14:52:27 +0000587 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588 struct block_device *bdev;
589 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400590 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400591 struct block_device *latest_bdev = NULL;
592 struct buffer_head *bh;
593 struct btrfs_super_block *disk_super;
594 u64 latest_devid = 0;
595 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400596 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500597 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400598 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400599
Tejun Heod4d77622010-11-13 11:55:18 +0100600 flags |= FMODE_EXCL;
601
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500602 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400603 if (device->bdev)
604 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400605 if (!device->name)
606 continue;
607
Tejun Heod4d77622010-11-13 11:55:18 +0100608 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400609 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500610 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400611 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400612 }
Chris Masona061fc82008-05-07 11:43:44 -0400613 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400614
Yan Zhenga512bbf2008-12-08 16:46:26 -0500615 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300616 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400617 goto error_close;
618
619 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000620 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400621 if (devid != device->devid)
622 goto error_brelse;
623
Yan Zheng2b820322008-11-17 21:11:30 -0500624 if (memcmp(device->uuid, disk_super->dev_item.uuid,
625 BTRFS_UUID_SIZE))
626 goto error_brelse;
627
628 device->generation = btrfs_super_generation(disk_super);
629 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400630 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500631 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400632 latest_bdev = bdev;
633 }
634
Yan Zheng2b820322008-11-17 21:11:30 -0500635 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
636 device->writeable = 0;
637 } else {
638 device->writeable = !bdev_read_only(bdev);
639 seeding = 0;
640 }
641
Josef Bacikd5e20032011-08-04 14:52:27 +0000642 q = bdev_get_queue(bdev);
643 if (blk_queue_discard(q)) {
644 device->can_discard = 1;
645 fs_devices->num_can_discard++;
646 }
647
Chris Mason8a4b83c2008-03-24 15:02:07 -0400648 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400649 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500650 device->mode = flags;
651
Chris Masonc2898112009-06-10 09:51:32 -0400652 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
653 fs_devices->rotating = 1;
654
Chris Masona0af4692008-05-13 16:03:06 -0400655 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500656 if (device->writeable) {
657 fs_devices->rw_devices++;
658 list_add(&device->dev_alloc_list,
659 &fs_devices->alloc_list);
660 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000661 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400662 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400663
Chris Masona0af4692008-05-13 16:03:06 -0400664error_brelse:
665 brelse(bh);
666error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100667 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400668error:
669 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400670 }
Chris Masona0af4692008-05-13 16:03:06 -0400671 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300672 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400673 goto out;
674 }
Yan Zheng2b820322008-11-17 21:11:30 -0500675 fs_devices->seeding = seeding;
676 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400677 fs_devices->latest_bdev = latest_bdev;
678 fs_devices->latest_devid = latest_devid;
679 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500680 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400681out:
Yan Zheng2b820322008-11-17 21:11:30 -0500682 return ret;
683}
684
685int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500686 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500687{
688 int ret;
689
690 mutex_lock(&uuid_mutex);
691 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500692 fs_devices->opened++;
693 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500694 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500695 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500696 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400697 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400698 return ret;
699}
700
Christoph Hellwig97288f22008-12-02 06:36:09 -0500701int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400702 struct btrfs_fs_devices **fs_devices_ret)
703{
704 struct btrfs_super_block *disk_super;
705 struct block_device *bdev;
706 struct buffer_head *bh;
707 int ret;
708 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400709 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400710
Tejun Heod4d77622010-11-13 11:55:18 +0100711 flags |= FMODE_EXCL;
712 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713
714 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400715 ret = PTR_ERR(bdev);
716 goto error;
717 }
718
Al Viro10f63272011-11-17 15:05:22 -0500719 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400720 ret = set_blocksize(bdev, 4096);
721 if (ret)
722 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500723 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400724 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000725 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400726 goto error_close;
727 }
728 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000729 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400730 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400731 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500732 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200733 else
734 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500735 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500736 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400737 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
738
Chris Mason8a4b83c2008-03-24 15:02:07 -0400739 brelse(bh);
740error_close:
Al Viro10f63272011-11-17 15:05:22 -0500741 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100742 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400743error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400744 return ret;
745}
Chris Mason0b86a832008-03-24 15:01:56 -0400746
Miao Xie6d07bce2011-01-05 10:07:31 +0000747/* helper to account the used device space in the range */
748int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
749 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400750{
751 struct btrfs_key key;
752 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000753 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500754 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000755 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400756 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000757 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400758 struct extent_buffer *l;
759
Miao Xie6d07bce2011-01-05 10:07:31 +0000760 *length = 0;
761
762 if (start >= device->total_bytes)
763 return 0;
764
Yan Zheng2b820322008-11-17 21:11:30 -0500765 path = btrfs_alloc_path();
766 if (!path)
767 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400768 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400769
Chris Mason0b86a832008-03-24 15:01:56 -0400770 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000771 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400772 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000773
774 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400775 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000776 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400777 if (ret > 0) {
778 ret = btrfs_previous_item(root, path, key.objectid, key.type);
779 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000780 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400781 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000782
Chris Mason0b86a832008-03-24 15:01:56 -0400783 while (1) {
784 l = path->nodes[0];
785 slot = path->slots[0];
786 if (slot >= btrfs_header_nritems(l)) {
787 ret = btrfs_next_leaf(root, path);
788 if (ret == 0)
789 continue;
790 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000791 goto out;
792
793 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400794 }
795 btrfs_item_key_to_cpu(l, &key, slot);
796
797 if (key.objectid < device->devid)
798 goto next;
799
800 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000801 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400802
Chris Masond3977122009-01-05 21:25:51 -0500803 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400804 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400805
Chris Mason0b86a832008-03-24 15:01:56 -0400806 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000807 extent_end = key.offset + btrfs_dev_extent_length(l,
808 dev_extent);
809 if (key.offset <= start && extent_end > end) {
810 *length = end - start + 1;
811 break;
812 } else if (key.offset <= start && extent_end > start)
813 *length += extent_end - start;
814 else if (key.offset > start && extent_end <= end)
815 *length += extent_end - key.offset;
816 else if (key.offset > start && key.offset <= end) {
817 *length += end - key.offset + 1;
818 break;
819 } else if (key.offset > end)
820 break;
821
822next:
823 path->slots[0]++;
824 }
825 ret = 0;
826out:
827 btrfs_free_path(path);
828 return ret;
829}
830
Chris Mason0b86a832008-03-24 15:01:56 -0400831/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000832 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000833 * @device: the device which we search the free space in
834 * @num_bytes: the size of the free space that we need
835 * @start: store the start of the free space.
836 * @len: the size of the free space. that we find, or the size of the max
837 * free space if we don't find suitable free space
838 *
Chris Mason0b86a832008-03-24 15:01:56 -0400839 * this uses a pretty simple search, the expectation is that it is
840 * called very infrequently and that a given device has a small number
841 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000842 *
843 * @start is used to store the start of the free space if we find. But if we
844 * don't find suitable free space, it will be used to store the start position
845 * of the max free space.
846 *
847 * @len is used to store the size of the free space that we find.
848 * But if we don't find suitable free space, it is used to store the size of
849 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400850 */
Li Zefan125ccb02011-12-08 15:07:24 +0800851int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000852 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400853{
854 struct btrfs_key key;
855 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000856 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400857 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000858 u64 hole_size;
859 u64 max_hole_start;
860 u64 max_hole_size;
861 u64 extent_end;
862 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400863 u64 search_end = device->total_bytes;
864 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000865 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400866 struct extent_buffer *l;
867
Chris Mason0b86a832008-03-24 15:01:56 -0400868 /* FIXME use last free of some kind */
869
870 /* we don't want to overwrite the superblock on the drive,
871 * so we make sure to start at an offset of at least 1MB
872 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200873 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400874
Miao Xie7bfc8372011-01-05 10:07:26 +0000875 max_hole_start = search_start;
876 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000877 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000878
879 if (search_start >= search_end) {
880 ret = -ENOSPC;
881 goto error;
882 }
883
884 path = btrfs_alloc_path();
885 if (!path) {
886 ret = -ENOMEM;
887 goto error;
888 }
889 path->reada = 2;
890
Chris Mason0b86a832008-03-24 15:01:56 -0400891 key.objectid = device->devid;
892 key.offset = search_start;
893 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000894
Li Zefan125ccb02011-12-08 15:07:24 +0800895 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400896 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000897 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400898 if (ret > 0) {
899 ret = btrfs_previous_item(root, path, key.objectid, key.type);
900 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000901 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400902 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000903
Chris Mason0b86a832008-03-24 15:01:56 -0400904 while (1) {
905 l = path->nodes[0];
906 slot = path->slots[0];
907 if (slot >= btrfs_header_nritems(l)) {
908 ret = btrfs_next_leaf(root, path);
909 if (ret == 0)
910 continue;
911 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000912 goto out;
913
914 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400915 }
916 btrfs_item_key_to_cpu(l, &key, slot);
917
918 if (key.objectid < device->devid)
919 goto next;
920
921 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000922 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400923
Chris Mason0b86a832008-03-24 15:01:56 -0400924 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
925 goto next;
926
Miao Xie7bfc8372011-01-05 10:07:26 +0000927 if (key.offset > search_start) {
928 hole_size = key.offset - search_start;
929
930 if (hole_size > max_hole_size) {
931 max_hole_start = search_start;
932 max_hole_size = hole_size;
933 }
934
935 /*
936 * If this free space is greater than which we need,
937 * it must be the max free space that we have found
938 * until now, so max_hole_start must point to the start
939 * of this free space and the length of this free space
940 * is stored in max_hole_size. Thus, we return
941 * max_hole_start and max_hole_size and go back to the
942 * caller.
943 */
944 if (hole_size >= num_bytes) {
945 ret = 0;
946 goto out;
947 }
948 }
949
Chris Mason0b86a832008-03-24 15:01:56 -0400950 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000951 extent_end = key.offset + btrfs_dev_extent_length(l,
952 dev_extent);
953 if (extent_end > search_start)
954 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400955next:
956 path->slots[0]++;
957 cond_resched();
958 }
Chris Mason0b86a832008-03-24 15:01:56 -0400959
liubo38c01b92011-08-02 02:39:03 +0000960 /*
961 * At this point, search_start should be the end of
962 * allocated dev extents, and when shrinking the device,
963 * search_end may be smaller than search_start.
964 */
965 if (search_end > search_start)
966 hole_size = search_end - search_start;
967
Miao Xie7bfc8372011-01-05 10:07:26 +0000968 if (hole_size > max_hole_size) {
969 max_hole_start = search_start;
970 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400971 }
Chris Mason0b86a832008-03-24 15:01:56 -0400972
Miao Xie7bfc8372011-01-05 10:07:26 +0000973 /* See above. */
974 if (hole_size < num_bytes)
975 ret = -ENOSPC;
976 else
977 ret = 0;
978
979out:
Yan Zheng2b820322008-11-17 21:11:30 -0500980 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000981error:
982 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000983 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000984 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400985 return ret;
986}
987
Christoph Hellwigb2950862008-12-02 09:54:17 -0500988static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400989 struct btrfs_device *device,
990 u64 start)
991{
992 int ret;
993 struct btrfs_path *path;
994 struct btrfs_root *root = device->dev_root;
995 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400996 struct btrfs_key found_key;
997 struct extent_buffer *leaf = NULL;
998 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400999
1000 path = btrfs_alloc_path();
1001 if (!path)
1002 return -ENOMEM;
1003
1004 key.objectid = device->devid;
1005 key.offset = start;
1006 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001007again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001008 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001009 if (ret > 0) {
1010 ret = btrfs_previous_item(root, path, key.objectid,
1011 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001012 if (ret)
1013 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001014 leaf = path->nodes[0];
1015 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1016 extent = btrfs_item_ptr(leaf, path->slots[0],
1017 struct btrfs_dev_extent);
1018 BUG_ON(found_key.offset > start || found_key.offset +
1019 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001020 key = found_key;
1021 btrfs_release_path(path);
1022 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001023 } else if (ret == 0) {
1024 leaf = path->nodes[0];
1025 extent = btrfs_item_ptr(leaf, path->slots[0],
1026 struct btrfs_dev_extent);
1027 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001028 BUG_ON(ret);
1029
Josef Bacik2bf64752011-09-26 17:12:22 -04001030 if (device->bytes_used > 0) {
1031 u64 len = btrfs_dev_extent_length(leaf, extent);
1032 device->bytes_used -= len;
1033 spin_lock(&root->fs_info->free_chunk_lock);
1034 root->fs_info->free_chunk_space += len;
1035 spin_unlock(&root->fs_info->free_chunk_lock);
1036 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001037 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001038
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001039out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001040 btrfs_free_path(path);
1041 return ret;
1042}
1043
Yan Zheng2b820322008-11-17 21:11:30 -05001044int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001045 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001046 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001047 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001048{
1049 int ret;
1050 struct btrfs_path *path;
1051 struct btrfs_root *root = device->dev_root;
1052 struct btrfs_dev_extent *extent;
1053 struct extent_buffer *leaf;
1054 struct btrfs_key key;
1055
Chris Masondfe25022008-05-13 13:46:40 -04001056 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001057 path = btrfs_alloc_path();
1058 if (!path)
1059 return -ENOMEM;
1060
Chris Mason0b86a832008-03-24 15:01:56 -04001061 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001062 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001063 key.type = BTRFS_DEV_EXTENT_KEY;
1064 ret = btrfs_insert_empty_item(trans, root, path, &key,
1065 sizeof(*extent));
1066 BUG_ON(ret);
1067
1068 leaf = path->nodes[0];
1069 extent = btrfs_item_ptr(leaf, path->slots[0],
1070 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001071 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1072 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1073 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1074
1075 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1076 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1077 BTRFS_UUID_SIZE);
1078
Chris Mason0b86a832008-03-24 15:01:56 -04001079 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1080 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001081 btrfs_free_path(path);
1082 return ret;
1083}
1084
Chris Masona1b32a52008-09-05 16:09:51 -04001085static noinline int find_next_chunk(struct btrfs_root *root,
1086 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001087{
1088 struct btrfs_path *path;
1089 int ret;
1090 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001091 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001092 struct btrfs_key found_key;
1093
1094 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001095 if (!path)
1096 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001097
Chris Masone17cade2008-04-15 15:41:47 -04001098 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001099 key.offset = (u64)-1;
1100 key.type = BTRFS_CHUNK_ITEM_KEY;
1101
1102 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1103 if (ret < 0)
1104 goto error;
1105
1106 BUG_ON(ret == 0);
1107
1108 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1109 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001110 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001111 } else {
1112 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1113 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001114 if (found_key.objectid != objectid)
1115 *offset = 0;
1116 else {
1117 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1118 struct btrfs_chunk);
1119 *offset = found_key.offset +
1120 btrfs_chunk_length(path->nodes[0], chunk);
1121 }
Chris Mason0b86a832008-03-24 15:01:56 -04001122 }
1123 ret = 0;
1124error:
1125 btrfs_free_path(path);
1126 return ret;
1127}
1128
Yan Zheng2b820322008-11-17 21:11:30 -05001129static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001130{
1131 int ret;
1132 struct btrfs_key key;
1133 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001134 struct btrfs_path *path;
1135
1136 root = root->fs_info->chunk_root;
1137
1138 path = btrfs_alloc_path();
1139 if (!path)
1140 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001141
1142 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1143 key.type = BTRFS_DEV_ITEM_KEY;
1144 key.offset = (u64)-1;
1145
1146 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1147 if (ret < 0)
1148 goto error;
1149
1150 BUG_ON(ret == 0);
1151
1152 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1153 BTRFS_DEV_ITEM_KEY);
1154 if (ret) {
1155 *objectid = 1;
1156 } else {
1157 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1158 path->slots[0]);
1159 *objectid = found_key.offset + 1;
1160 }
1161 ret = 0;
1162error:
Yan Zheng2b820322008-11-17 21:11:30 -05001163 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001164 return ret;
1165}
1166
1167/*
1168 * the device information is stored in the chunk root
1169 * the btrfs_device struct should be fully filled in
1170 */
1171int btrfs_add_device(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root,
1173 struct btrfs_device *device)
1174{
1175 int ret;
1176 struct btrfs_path *path;
1177 struct btrfs_dev_item *dev_item;
1178 struct extent_buffer *leaf;
1179 struct btrfs_key key;
1180 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001181
1182 root = root->fs_info->chunk_root;
1183
1184 path = btrfs_alloc_path();
1185 if (!path)
1186 return -ENOMEM;
1187
Chris Mason0b86a832008-03-24 15:01:56 -04001188 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1189 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001190 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001191
1192 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001193 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001194 if (ret)
1195 goto out;
1196
1197 leaf = path->nodes[0];
1198 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1199
1200 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001201 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001202 btrfs_set_device_type(leaf, dev_item, device->type);
1203 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1204 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1205 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001206 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1207 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001208 btrfs_set_device_group(leaf, dev_item, 0);
1209 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1210 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001211 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001212
Chris Mason0b86a832008-03-24 15:01:56 -04001213 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001214 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001215 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1216 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001217 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001218
Yan Zheng2b820322008-11-17 21:11:30 -05001219 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001220out:
1221 btrfs_free_path(path);
1222 return ret;
1223}
Chris Mason8f18cf12008-04-25 16:53:30 -04001224
Chris Masona061fc82008-05-07 11:43:44 -04001225static int btrfs_rm_dev_item(struct btrfs_root *root,
1226 struct btrfs_device *device)
1227{
1228 int ret;
1229 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001230 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001231 struct btrfs_trans_handle *trans;
1232
1233 root = root->fs_info->chunk_root;
1234
1235 path = btrfs_alloc_path();
1236 if (!path)
1237 return -ENOMEM;
1238
Yan, Zhenga22285a2010-05-16 10:48:46 -04001239 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001240 if (IS_ERR(trans)) {
1241 btrfs_free_path(path);
1242 return PTR_ERR(trans);
1243 }
Chris Masona061fc82008-05-07 11:43:44 -04001244 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1245 key.type = BTRFS_DEV_ITEM_KEY;
1246 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001247 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001248
1249 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1250 if (ret < 0)
1251 goto out;
1252
1253 if (ret > 0) {
1254 ret = -ENOENT;
1255 goto out;
1256 }
1257
1258 ret = btrfs_del_item(trans, root, path);
1259 if (ret)
1260 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001261out:
1262 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001263 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001264 btrfs_commit_transaction(trans, root);
1265 return ret;
1266}
1267
1268int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1269{
1270 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001271 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001272 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001273 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001274 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001275 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001276 u64 all_avail;
1277 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001278 u64 num_devices;
1279 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001280 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001281 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001282
Chris Masona061fc82008-05-07 11:43:44 -04001283 mutex_lock(&uuid_mutex);
1284
1285 all_avail = root->fs_info->avail_data_alloc_bits |
1286 root->fs_info->avail_system_alloc_bits |
1287 root->fs_info->avail_metadata_alloc_bits;
1288
1289 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001290 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001291 printk(KERN_ERR "btrfs: unable to go below four devices "
1292 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001293 ret = -EINVAL;
1294 goto out;
1295 }
1296
1297 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001298 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001299 printk(KERN_ERR "btrfs: unable to go below two "
1300 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001301 ret = -EINVAL;
1302 goto out;
1303 }
1304
Chris Masondfe25022008-05-13 13:46:40 -04001305 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001306 struct list_head *devices;
1307 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001308
Chris Masondfe25022008-05-13 13:46:40 -04001309 device = NULL;
1310 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001311 /*
1312 * It is safe to read the devices since the volume_mutex
1313 * is held.
1314 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001315 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001316 if (tmp->in_fs_metadata && !tmp->bdev) {
1317 device = tmp;
1318 break;
1319 }
1320 }
1321 bdev = NULL;
1322 bh = NULL;
1323 disk_super = NULL;
1324 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001325 printk(KERN_ERR "btrfs: no missing devices found to "
1326 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001327 goto out;
1328 }
Chris Masondfe25022008-05-13 13:46:40 -04001329 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001330 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1331 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001332 if (IS_ERR(bdev)) {
1333 ret = PTR_ERR(bdev);
1334 goto out;
1335 }
1336
Yan Zheng2b820322008-11-17 21:11:30 -05001337 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001338 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001339 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001340 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001341 goto error_close;
1342 }
1343 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001344 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001345 dev_uuid = disk_super->dev_item.uuid;
1346 device = btrfs_find_device(root, devid, dev_uuid,
1347 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001348 if (!device) {
1349 ret = -ENOENT;
1350 goto error_brelse;
1351 }
Chris Masondfe25022008-05-13 13:46:40 -04001352 }
Yan Zheng2b820322008-11-17 21:11:30 -05001353
1354 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001355 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1356 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001357 ret = -EINVAL;
1358 goto error_brelse;
1359 }
1360
1361 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001362 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001363 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001364 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001365 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001366 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001367 }
Chris Masona061fc82008-05-07 11:43:44 -04001368
1369 ret = btrfs_shrink_device(device, 0);
1370 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001371 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001372
Chris Masona061fc82008-05-07 11:43:44 -04001373 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1374 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001375 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001376
Josef Bacik2bf64752011-09-26 17:12:22 -04001377 spin_lock(&root->fs_info->free_chunk_lock);
1378 root->fs_info->free_chunk_space = device->total_bytes -
1379 device->bytes_used;
1380 spin_unlock(&root->fs_info->free_chunk_lock);
1381
Yan Zheng2b820322008-11-17 21:11:30 -05001382 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001383 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001384
1385 /*
1386 * the device list mutex makes sure that we don't change
1387 * the device list while someone else is writing out all
1388 * the device supers.
1389 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001390
1391 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001392 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001393 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001394
Yan Zhenge4404d62008-12-12 10:03:26 -05001395 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001396
Chris Masoncd02dca2010-12-13 14:56:23 -05001397 if (device->missing)
1398 root->fs_info->fs_devices->missing_devices--;
1399
Yan Zheng2b820322008-11-17 21:11:30 -05001400 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1401 struct btrfs_device, dev_list);
1402 if (device->bdev == root->fs_info->sb->s_bdev)
1403 root->fs_info->sb->s_bdev = next_device->bdev;
1404 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1405 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1406
Xiao Guangrong1f781602011-04-20 10:09:16 +00001407 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001408 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001409
1410 call_rcu(&device->rcu, free_device);
1411 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001412
David Sterba6c417612011-04-13 15:41:04 +02001413 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1414 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001415
Xiao Guangrong1f781602011-04-20 10:09:16 +00001416 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001417 struct btrfs_fs_devices *fs_devices;
1418 fs_devices = root->fs_info->fs_devices;
1419 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001420 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001421 break;
1422 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001423 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001424 fs_devices->seed = cur_devices->seed;
1425 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001426 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001427 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001428 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001429 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001430 }
1431
1432 /*
1433 * at this point, the device is zero sized. We want to
1434 * remove it from the devices list and zero out the old super
1435 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001436 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001437 /* make sure this device isn't detected as part of
1438 * the FS anymore
1439 */
1440 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1441 set_buffer_dirty(bh);
1442 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001443 }
Chris Masona061fc82008-05-07 11:43:44 -04001444
Chris Masona061fc82008-05-07 11:43:44 -04001445 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001446
1447error_brelse:
1448 brelse(bh);
1449error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001450 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001451 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001452out:
1453 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001454 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001455error_undo:
1456 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001457 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001458 list_add(&device->dev_alloc_list,
1459 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001460 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001461 root->fs_info->fs_devices->rw_devices++;
1462 }
1463 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001464}
1465
Yan Zheng2b820322008-11-17 21:11:30 -05001466/*
1467 * does all the dirty work required for changing file system's UUID.
1468 */
Li Zefan125ccb02011-12-08 15:07:24 +08001469static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001470{
1471 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1472 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001473 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001474 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001475 struct btrfs_device *device;
1476 u64 super_flags;
1477
1478 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001479 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001480 return -EINVAL;
1481
Yan Zhenge4404d62008-12-12 10:03:26 -05001482 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1483 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001484 return -ENOMEM;
1485
Yan Zhenge4404d62008-12-12 10:03:26 -05001486 old_devices = clone_fs_devices(fs_devices);
1487 if (IS_ERR(old_devices)) {
1488 kfree(seed_devices);
1489 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001490 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001491
Yan Zheng2b820322008-11-17 21:11:30 -05001492 list_add(&old_devices->list, &fs_uuids);
1493
Yan Zhenge4404d62008-12-12 10:03:26 -05001494 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1495 seed_devices->opened = 1;
1496 INIT_LIST_HEAD(&seed_devices->devices);
1497 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001498 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001499
1500 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001501 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1502 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001503 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1504
Yan Zhenge4404d62008-12-12 10:03:26 -05001505 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1506 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1507 device->fs_devices = seed_devices;
1508 }
1509
Yan Zheng2b820322008-11-17 21:11:30 -05001510 fs_devices->seeding = 0;
1511 fs_devices->num_devices = 0;
1512 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001513 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001514
1515 generate_random_uuid(fs_devices->fsid);
1516 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1517 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1518 super_flags = btrfs_super_flags(disk_super) &
1519 ~BTRFS_SUPER_FLAG_SEEDING;
1520 btrfs_set_super_flags(disk_super, super_flags);
1521
1522 return 0;
1523}
1524
1525/*
1526 * strore the expected generation for seed devices in device items.
1527 */
1528static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1529 struct btrfs_root *root)
1530{
1531 struct btrfs_path *path;
1532 struct extent_buffer *leaf;
1533 struct btrfs_dev_item *dev_item;
1534 struct btrfs_device *device;
1535 struct btrfs_key key;
1536 u8 fs_uuid[BTRFS_UUID_SIZE];
1537 u8 dev_uuid[BTRFS_UUID_SIZE];
1538 u64 devid;
1539 int ret;
1540
1541 path = btrfs_alloc_path();
1542 if (!path)
1543 return -ENOMEM;
1544
1545 root = root->fs_info->chunk_root;
1546 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1547 key.offset = 0;
1548 key.type = BTRFS_DEV_ITEM_KEY;
1549
1550 while (1) {
1551 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1552 if (ret < 0)
1553 goto error;
1554
1555 leaf = path->nodes[0];
1556next_slot:
1557 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1558 ret = btrfs_next_leaf(root, path);
1559 if (ret > 0)
1560 break;
1561 if (ret < 0)
1562 goto error;
1563 leaf = path->nodes[0];
1564 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001565 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001566 continue;
1567 }
1568
1569 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1570 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1571 key.type != BTRFS_DEV_ITEM_KEY)
1572 break;
1573
1574 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1575 struct btrfs_dev_item);
1576 devid = btrfs_device_id(leaf, dev_item);
1577 read_extent_buffer(leaf, dev_uuid,
1578 (unsigned long)btrfs_device_uuid(dev_item),
1579 BTRFS_UUID_SIZE);
1580 read_extent_buffer(leaf, fs_uuid,
1581 (unsigned long)btrfs_device_fsid(dev_item),
1582 BTRFS_UUID_SIZE);
1583 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1584 BUG_ON(!device);
1585
1586 if (device->fs_devices->seeding) {
1587 btrfs_set_device_generation(leaf, dev_item,
1588 device->generation);
1589 btrfs_mark_buffer_dirty(leaf);
1590 }
1591
1592 path->slots[0]++;
1593 goto next_slot;
1594 }
1595 ret = 0;
1596error:
1597 btrfs_free_path(path);
1598 return ret;
1599}
1600
Chris Mason788f20e2008-04-28 15:29:42 -04001601int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1602{
Josef Bacikd5e20032011-08-04 14:52:27 +00001603 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001604 struct btrfs_trans_handle *trans;
1605 struct btrfs_device *device;
1606 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001607 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001608 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001609 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001610 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001611 int ret = 0;
1612
Yan Zheng2b820322008-11-17 21:11:30 -05001613 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1614 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001615
Li Zefana5d16332011-12-07 20:08:40 -05001616 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001617 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001618 if (IS_ERR(bdev))
1619 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001620
Yan Zheng2b820322008-11-17 21:11:30 -05001621 if (root->fs_info->fs_devices->seeding) {
1622 seeding_dev = 1;
1623 down_write(&sb->s_umount);
1624 mutex_lock(&uuid_mutex);
1625 }
1626
Chris Mason8c8bee12008-09-29 11:19:10 -04001627 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001628
Chris Mason788f20e2008-04-28 15:29:42 -04001629 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001630 /*
1631 * we have the volume lock, so we don't need the extra
1632 * device list mutex while reading the list here.
1633 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001634 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001635 if (device->bdev == bdev) {
1636 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001637 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001638 }
1639 }
1640
1641 device = kzalloc(sizeof(*device), GFP_NOFS);
1642 if (!device) {
1643 /* we can safely leave the fs_devices entry around */
1644 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001645 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001646 }
1647
Chris Mason788f20e2008-04-28 15:29:42 -04001648 device->name = kstrdup(device_path, GFP_NOFS);
1649 if (!device->name) {
1650 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001651 ret = -ENOMEM;
1652 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001653 }
Yan Zheng2b820322008-11-17 21:11:30 -05001654
1655 ret = find_next_devid(root, &device->devid);
1656 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001657 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001658 kfree(device);
1659 goto error;
1660 }
1661
Yan, Zhenga22285a2010-05-16 10:48:46 -04001662 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001663 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001664 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001665 kfree(device);
1666 ret = PTR_ERR(trans);
1667 goto error;
1668 }
1669
Yan Zheng2b820322008-11-17 21:11:30 -05001670 lock_chunks(root);
1671
Josef Bacikd5e20032011-08-04 14:52:27 +00001672 q = bdev_get_queue(bdev);
1673 if (blk_queue_discard(q))
1674 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001675 device->writeable = 1;
1676 device->work.func = pending_bios_fn;
1677 generate_random_uuid(device->uuid);
1678 spin_lock_init(&device->io_lock);
1679 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001680 device->io_width = root->sectorsize;
1681 device->io_align = root->sectorsize;
1682 device->sector_size = root->sectorsize;
1683 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001684 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001685 device->dev_root = root->fs_info->dev_root;
1686 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001687 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001688 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001689 set_blocksize(device->bdev, 4096);
1690
Yan Zheng2b820322008-11-17 21:11:30 -05001691 if (seeding_dev) {
1692 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001693 ret = btrfs_prepare_sprout(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001694 BUG_ON(ret);
1695 }
1696
1697 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001698
1699 /*
1700 * we don't want write_supers to jump in here with our device
1701 * half setup
1702 */
1703 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001704 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001705 list_add(&device->dev_alloc_list,
1706 &root->fs_info->fs_devices->alloc_list);
1707 root->fs_info->fs_devices->num_devices++;
1708 root->fs_info->fs_devices->open_devices++;
1709 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001710 if (device->can_discard)
1711 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001712 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1713
Josef Bacik2bf64752011-09-26 17:12:22 -04001714 spin_lock(&root->fs_info->free_chunk_lock);
1715 root->fs_info->free_chunk_space += device->total_bytes;
1716 spin_unlock(&root->fs_info->free_chunk_lock);
1717
Chris Masonc2898112009-06-10 09:51:32 -04001718 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1719 root->fs_info->fs_devices->rotating = 1;
1720
David Sterba6c417612011-04-13 15:41:04 +02001721 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1722 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001723 total_bytes + device->total_bytes);
1724
David Sterba6c417612011-04-13 15:41:04 +02001725 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1726 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001727 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001728 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001729
Yan Zheng2b820322008-11-17 21:11:30 -05001730 if (seeding_dev) {
1731 ret = init_first_rw_device(trans, root, device);
1732 BUG_ON(ret);
1733 ret = btrfs_finish_sprout(trans, root);
1734 BUG_ON(ret);
1735 } else {
1736 ret = btrfs_add_device(trans, root, device);
1737 }
1738
Chris Mason913d9522009-03-10 13:17:18 -04001739 /*
1740 * we've got more storage, clear any full flags on the space
1741 * infos
1742 */
1743 btrfs_clear_space_info_full(root->fs_info);
1744
Chris Mason7d9eb122008-07-08 14:19:17 -04001745 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001746 btrfs_commit_transaction(trans, root);
1747
1748 if (seeding_dev) {
1749 mutex_unlock(&uuid_mutex);
1750 up_write(&sb->s_umount);
1751
1752 ret = btrfs_relocate_sys_chunks(root);
1753 BUG_ON(ret);
1754 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001755
Chris Mason788f20e2008-04-28 15:29:42 -04001756 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001757error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001758 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001759 if (seeding_dev) {
1760 mutex_unlock(&uuid_mutex);
1761 up_write(&sb->s_umount);
1762 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001763 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001764}
1765
Chris Masond3977122009-01-05 21:25:51 -05001766static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1767 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001768{
1769 int ret;
1770 struct btrfs_path *path;
1771 struct btrfs_root *root;
1772 struct btrfs_dev_item *dev_item;
1773 struct extent_buffer *leaf;
1774 struct btrfs_key key;
1775
1776 root = device->dev_root->fs_info->chunk_root;
1777
1778 path = btrfs_alloc_path();
1779 if (!path)
1780 return -ENOMEM;
1781
1782 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1783 key.type = BTRFS_DEV_ITEM_KEY;
1784 key.offset = device->devid;
1785
1786 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1787 if (ret < 0)
1788 goto out;
1789
1790 if (ret > 0) {
1791 ret = -ENOENT;
1792 goto out;
1793 }
1794
1795 leaf = path->nodes[0];
1796 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1797
1798 btrfs_set_device_id(leaf, dev_item, device->devid);
1799 btrfs_set_device_type(leaf, dev_item, device->type);
1800 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1801 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1802 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001803 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001804 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1805 btrfs_mark_buffer_dirty(leaf);
1806
1807out:
1808 btrfs_free_path(path);
1809 return ret;
1810}
1811
Chris Mason7d9eb122008-07-08 14:19:17 -04001812static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001813 struct btrfs_device *device, u64 new_size)
1814{
1815 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001816 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001817 u64 old_total = btrfs_super_total_bytes(super_copy);
1818 u64 diff = new_size - device->total_bytes;
1819
Yan Zheng2b820322008-11-17 21:11:30 -05001820 if (!device->writeable)
1821 return -EACCES;
1822 if (new_size <= device->total_bytes)
1823 return -EINVAL;
1824
Chris Mason8f18cf12008-04-25 16:53:30 -04001825 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001826 device->fs_devices->total_rw_bytes += diff;
1827
1828 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001829 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001830 btrfs_clear_space_info_full(device->dev_root->fs_info);
1831
Chris Mason8f18cf12008-04-25 16:53:30 -04001832 return btrfs_update_device(trans, device);
1833}
1834
Chris Mason7d9eb122008-07-08 14:19:17 -04001835int btrfs_grow_device(struct btrfs_trans_handle *trans,
1836 struct btrfs_device *device, u64 new_size)
1837{
1838 int ret;
1839 lock_chunks(device->dev_root);
1840 ret = __btrfs_grow_device(trans, device, new_size);
1841 unlock_chunks(device->dev_root);
1842 return ret;
1843}
1844
Chris Mason8f18cf12008-04-25 16:53:30 -04001845static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1846 struct btrfs_root *root,
1847 u64 chunk_tree, u64 chunk_objectid,
1848 u64 chunk_offset)
1849{
1850 int ret;
1851 struct btrfs_path *path;
1852 struct btrfs_key key;
1853
1854 root = root->fs_info->chunk_root;
1855 path = btrfs_alloc_path();
1856 if (!path)
1857 return -ENOMEM;
1858
1859 key.objectid = chunk_objectid;
1860 key.offset = chunk_offset;
1861 key.type = BTRFS_CHUNK_ITEM_KEY;
1862
1863 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1864 BUG_ON(ret);
1865
1866 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001867
1868 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001869 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001870}
1871
Christoph Hellwigb2950862008-12-02 09:54:17 -05001872static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001873 chunk_offset)
1874{
David Sterba6c417612011-04-13 15:41:04 +02001875 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001876 struct btrfs_disk_key *disk_key;
1877 struct btrfs_chunk *chunk;
1878 u8 *ptr;
1879 int ret = 0;
1880 u32 num_stripes;
1881 u32 array_size;
1882 u32 len = 0;
1883 u32 cur;
1884 struct btrfs_key key;
1885
1886 array_size = btrfs_super_sys_array_size(super_copy);
1887
1888 ptr = super_copy->sys_chunk_array;
1889 cur = 0;
1890
1891 while (cur < array_size) {
1892 disk_key = (struct btrfs_disk_key *)ptr;
1893 btrfs_disk_key_to_cpu(&key, disk_key);
1894
1895 len = sizeof(*disk_key);
1896
1897 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1898 chunk = (struct btrfs_chunk *)(ptr + len);
1899 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1900 len += btrfs_chunk_item_size(num_stripes);
1901 } else {
1902 ret = -EIO;
1903 break;
1904 }
1905 if (key.objectid == chunk_objectid &&
1906 key.offset == chunk_offset) {
1907 memmove(ptr, ptr + len, array_size - (cur + len));
1908 array_size -= len;
1909 btrfs_set_super_sys_array_size(super_copy, array_size);
1910 } else {
1911 ptr += len;
1912 cur += len;
1913 }
1914 }
1915 return ret;
1916}
1917
Christoph Hellwigb2950862008-12-02 09:54:17 -05001918static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001919 u64 chunk_tree, u64 chunk_objectid,
1920 u64 chunk_offset)
1921{
1922 struct extent_map_tree *em_tree;
1923 struct btrfs_root *extent_root;
1924 struct btrfs_trans_handle *trans;
1925 struct extent_map *em;
1926 struct map_lookup *map;
1927 int ret;
1928 int i;
1929
1930 root = root->fs_info->chunk_root;
1931 extent_root = root->fs_info->extent_root;
1932 em_tree = &root->fs_info->mapping_tree.map_tree;
1933
Josef Bacikba1bf482009-09-11 16:11:19 -04001934 ret = btrfs_can_relocate(extent_root, chunk_offset);
1935 if (ret)
1936 return -ENOSPC;
1937
Chris Mason8f18cf12008-04-25 16:53:30 -04001938 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001939 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001940 if (ret)
1941 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001942
Yan, Zhenga22285a2010-05-16 10:48:46 -04001943 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001944 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001945
Chris Mason7d9eb122008-07-08 14:19:17 -04001946 lock_chunks(root);
1947
Chris Mason8f18cf12008-04-25 16:53:30 -04001948 /*
1949 * step two, delete the device extents and the
1950 * chunk tree entries
1951 */
Chris Mason890871b2009-09-02 16:24:52 -04001952 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001953 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001954 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001955
Chris Masona061fc82008-05-07 11:43:44 -04001956 BUG_ON(em->start > chunk_offset ||
1957 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001958 map = (struct map_lookup *)em->bdev;
1959
1960 for (i = 0; i < map->num_stripes; i++) {
1961 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1962 map->stripes[i].physical);
1963 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001964
Chris Masondfe25022008-05-13 13:46:40 -04001965 if (map->stripes[i].dev) {
1966 ret = btrfs_update_device(trans, map->stripes[i].dev);
1967 BUG_ON(ret);
1968 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001969 }
1970 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1971 chunk_offset);
1972
1973 BUG_ON(ret);
1974
liubo1abe9b82011-03-24 11:18:59 +00001975 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1976
Chris Mason8f18cf12008-04-25 16:53:30 -04001977 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1978 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1979 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001980 }
1981
Zheng Yan1a40e232008-09-26 10:09:34 -04001982 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1983 BUG_ON(ret);
1984
Chris Mason890871b2009-09-02 16:24:52 -04001985 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001986 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001987 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001988
Chris Mason8f18cf12008-04-25 16:53:30 -04001989 kfree(map);
1990 em->bdev = NULL;
1991
1992 /* once for the tree */
1993 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001994 /* once for us */
1995 free_extent_map(em);
1996
Chris Mason7d9eb122008-07-08 14:19:17 -04001997 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001998 btrfs_end_transaction(trans, root);
1999 return 0;
2000}
2001
Yan Zheng2b820322008-11-17 21:11:30 -05002002static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2003{
2004 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2005 struct btrfs_path *path;
2006 struct extent_buffer *leaf;
2007 struct btrfs_chunk *chunk;
2008 struct btrfs_key key;
2009 struct btrfs_key found_key;
2010 u64 chunk_tree = chunk_root->root_key.objectid;
2011 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002012 bool retried = false;
2013 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002014 int ret;
2015
2016 path = btrfs_alloc_path();
2017 if (!path)
2018 return -ENOMEM;
2019
Josef Bacikba1bf482009-09-11 16:11:19 -04002020again:
Yan Zheng2b820322008-11-17 21:11:30 -05002021 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2022 key.offset = (u64)-1;
2023 key.type = BTRFS_CHUNK_ITEM_KEY;
2024
2025 while (1) {
2026 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2027 if (ret < 0)
2028 goto error;
2029 BUG_ON(ret == 0);
2030
2031 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2032 key.type);
2033 if (ret < 0)
2034 goto error;
2035 if (ret > 0)
2036 break;
2037
2038 leaf = path->nodes[0];
2039 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2040
2041 chunk = btrfs_item_ptr(leaf, path->slots[0],
2042 struct btrfs_chunk);
2043 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002044 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002045
2046 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2047 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2048 found_key.objectid,
2049 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002050 if (ret == -ENOSPC)
2051 failed++;
2052 else if (ret)
2053 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002054 }
2055
2056 if (found_key.offset == 0)
2057 break;
2058 key.offset = found_key.offset - 1;
2059 }
2060 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002061 if (failed && !retried) {
2062 failed = 0;
2063 retried = true;
2064 goto again;
2065 } else if (failed && retried) {
2066 WARN_ON(1);
2067 ret = -ENOSPC;
2068 }
Yan Zheng2b820322008-11-17 21:11:30 -05002069error:
2070 btrfs_free_path(path);
2071 return ret;
2072}
2073
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002074static int insert_balance_item(struct btrfs_root *root,
2075 struct btrfs_balance_control *bctl)
2076{
2077 struct btrfs_trans_handle *trans;
2078 struct btrfs_balance_item *item;
2079 struct btrfs_disk_balance_args disk_bargs;
2080 struct btrfs_path *path;
2081 struct extent_buffer *leaf;
2082 struct btrfs_key key;
2083 int ret, err;
2084
2085 path = btrfs_alloc_path();
2086 if (!path)
2087 return -ENOMEM;
2088
2089 trans = btrfs_start_transaction(root, 0);
2090 if (IS_ERR(trans)) {
2091 btrfs_free_path(path);
2092 return PTR_ERR(trans);
2093 }
2094
2095 key.objectid = BTRFS_BALANCE_OBJECTID;
2096 key.type = BTRFS_BALANCE_ITEM_KEY;
2097 key.offset = 0;
2098
2099 ret = btrfs_insert_empty_item(trans, root, path, &key,
2100 sizeof(*item));
2101 if (ret)
2102 goto out;
2103
2104 leaf = path->nodes[0];
2105 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2106
2107 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2108
2109 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2110 btrfs_set_balance_data(leaf, item, &disk_bargs);
2111 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2112 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2113 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2114 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2115
2116 btrfs_set_balance_flags(leaf, item, bctl->flags);
2117
2118 btrfs_mark_buffer_dirty(leaf);
2119out:
2120 btrfs_free_path(path);
2121 err = btrfs_commit_transaction(trans, root);
2122 if (err && !ret)
2123 ret = err;
2124 return ret;
2125}
2126
2127static int del_balance_item(struct btrfs_root *root)
2128{
2129 struct btrfs_trans_handle *trans;
2130 struct btrfs_path *path;
2131 struct btrfs_key key;
2132 int ret, err;
2133
2134 path = btrfs_alloc_path();
2135 if (!path)
2136 return -ENOMEM;
2137
2138 trans = btrfs_start_transaction(root, 0);
2139 if (IS_ERR(trans)) {
2140 btrfs_free_path(path);
2141 return PTR_ERR(trans);
2142 }
2143
2144 key.objectid = BTRFS_BALANCE_OBJECTID;
2145 key.type = BTRFS_BALANCE_ITEM_KEY;
2146 key.offset = 0;
2147
2148 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2149 if (ret < 0)
2150 goto out;
2151 if (ret > 0) {
2152 ret = -ENOENT;
2153 goto out;
2154 }
2155
2156 ret = btrfs_del_item(trans, root, path);
2157out:
2158 btrfs_free_path(path);
2159 err = btrfs_commit_transaction(trans, root);
2160 if (err && !ret)
2161 ret = err;
2162 return ret;
2163}
2164
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002165/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002166 * This is a heuristic used to reduce the number of chunks balanced on
2167 * resume after balance was interrupted.
2168 */
2169static void update_balance_args(struct btrfs_balance_control *bctl)
2170{
2171 /*
2172 * Turn on soft mode for chunk types that were being converted.
2173 */
2174 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2175 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2176 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2177 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2178 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2179 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2180
2181 /*
2182 * Turn on usage filter if is not already used. The idea is
2183 * that chunks that we have already balanced should be
2184 * reasonably full. Don't do it for chunks that are being
2185 * converted - that will keep us from relocating unconverted
2186 * (albeit full) chunks.
2187 */
2188 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2189 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2190 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2191 bctl->data.usage = 90;
2192 }
2193 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2194 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2195 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2196 bctl->sys.usage = 90;
2197 }
2198 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2199 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2200 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2201 bctl->meta.usage = 90;
2202 }
2203}
2204
2205/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002206 * Should be called with both balance and volume mutexes held to
2207 * serialize other volume operations (add_dev/rm_dev/resize) with
2208 * restriper. Same goes for unset_balance_control.
2209 */
2210static void set_balance_control(struct btrfs_balance_control *bctl)
2211{
2212 struct btrfs_fs_info *fs_info = bctl->fs_info;
2213
2214 BUG_ON(fs_info->balance_ctl);
2215
2216 spin_lock(&fs_info->balance_lock);
2217 fs_info->balance_ctl = bctl;
2218 spin_unlock(&fs_info->balance_lock);
2219}
2220
2221static void unset_balance_control(struct btrfs_fs_info *fs_info)
2222{
2223 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2224
2225 BUG_ON(!fs_info->balance_ctl);
2226
2227 spin_lock(&fs_info->balance_lock);
2228 fs_info->balance_ctl = NULL;
2229 spin_unlock(&fs_info->balance_lock);
2230
2231 kfree(bctl);
2232}
2233
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002234/*
2235 * Balance filters. Return 1 if chunk should be filtered out
2236 * (should not be balanced).
2237 */
2238static int chunk_profiles_filter(u64 chunk_profile,
2239 struct btrfs_balance_args *bargs)
2240{
2241 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2242
2243 if (chunk_profile == 0)
2244 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2245
2246 if (bargs->profiles & chunk_profile)
2247 return 0;
2248
2249 return 1;
2250}
2251
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002252static u64 div_factor_fine(u64 num, int factor)
2253{
2254 if (factor <= 0)
2255 return 0;
2256 if (factor >= 100)
2257 return num;
2258
2259 num *= factor;
2260 do_div(num, 100);
2261 return num;
2262}
2263
2264static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2265 struct btrfs_balance_args *bargs)
2266{
2267 struct btrfs_block_group_cache *cache;
2268 u64 chunk_used, user_thresh;
2269 int ret = 1;
2270
2271 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2272 chunk_used = btrfs_block_group_used(&cache->item);
2273
2274 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2275 if (chunk_used < user_thresh)
2276 ret = 0;
2277
2278 btrfs_put_block_group(cache);
2279 return ret;
2280}
2281
Ilya Dryomov409d4042012-01-16 22:04:47 +02002282static int chunk_devid_filter(struct extent_buffer *leaf,
2283 struct btrfs_chunk *chunk,
2284 struct btrfs_balance_args *bargs)
2285{
2286 struct btrfs_stripe *stripe;
2287 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2288 int i;
2289
2290 for (i = 0; i < num_stripes; i++) {
2291 stripe = btrfs_stripe_nr(chunk, i);
2292 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2293 return 0;
2294 }
2295
2296 return 1;
2297}
2298
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002299/* [pstart, pend) */
2300static int chunk_drange_filter(struct extent_buffer *leaf,
2301 struct btrfs_chunk *chunk,
2302 u64 chunk_offset,
2303 struct btrfs_balance_args *bargs)
2304{
2305 struct btrfs_stripe *stripe;
2306 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2307 u64 stripe_offset;
2308 u64 stripe_length;
2309 int factor;
2310 int i;
2311
2312 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2313 return 0;
2314
2315 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2316 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2317 factor = 2;
2318 else
2319 factor = 1;
2320 factor = num_stripes / factor;
2321
2322 for (i = 0; i < num_stripes; i++) {
2323 stripe = btrfs_stripe_nr(chunk, i);
2324 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2325 continue;
2326
2327 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2328 stripe_length = btrfs_chunk_length(leaf, chunk);
2329 do_div(stripe_length, factor);
2330
2331 if (stripe_offset < bargs->pend &&
2332 stripe_offset + stripe_length > bargs->pstart)
2333 return 0;
2334 }
2335
2336 return 1;
2337}
2338
Ilya Dryomovea671762012-01-16 22:04:48 +02002339/* [vstart, vend) */
2340static int chunk_vrange_filter(struct extent_buffer *leaf,
2341 struct btrfs_chunk *chunk,
2342 u64 chunk_offset,
2343 struct btrfs_balance_args *bargs)
2344{
2345 if (chunk_offset < bargs->vend &&
2346 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2347 /* at least part of the chunk is inside this vrange */
2348 return 0;
2349
2350 return 1;
2351}
2352
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002353static int chunk_soft_convert_filter(u64 chunk_profile,
2354 struct btrfs_balance_args *bargs)
2355{
2356 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2357 return 0;
2358
2359 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2360
2361 if (chunk_profile == 0)
2362 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2363
2364 if (bargs->target & chunk_profile)
2365 return 1;
2366
2367 return 0;
2368}
2369
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002370static int should_balance_chunk(struct btrfs_root *root,
2371 struct extent_buffer *leaf,
2372 struct btrfs_chunk *chunk, u64 chunk_offset)
2373{
2374 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2375 struct btrfs_balance_args *bargs = NULL;
2376 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2377
2378 /* type filter */
2379 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2380 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2381 return 0;
2382 }
2383
2384 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2385 bargs = &bctl->data;
2386 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2387 bargs = &bctl->sys;
2388 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2389 bargs = &bctl->meta;
2390
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002391 /* profiles filter */
2392 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2393 chunk_profiles_filter(chunk_type, bargs)) {
2394 return 0;
2395 }
2396
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002397 /* usage filter */
2398 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2399 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2400 return 0;
2401 }
2402
Ilya Dryomov409d4042012-01-16 22:04:47 +02002403 /* devid filter */
2404 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2405 chunk_devid_filter(leaf, chunk, bargs)) {
2406 return 0;
2407 }
2408
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002409 /* drange filter, makes sense only with devid filter */
2410 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2411 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2412 return 0;
2413 }
2414
Ilya Dryomovea671762012-01-16 22:04:48 +02002415 /* vrange filter */
2416 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2417 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2418 return 0;
2419 }
2420
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002421 /* soft profile changing mode */
2422 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2423 chunk_soft_convert_filter(chunk_type, bargs)) {
2424 return 0;
2425 }
2426
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002427 return 1;
2428}
2429
Chris Masonec44a352008-04-28 15:29:52 -04002430static u64 div_factor(u64 num, int factor)
2431{
2432 if (factor == 10)
2433 return num;
2434 num *= factor;
2435 do_div(num, 10);
2436 return num;
2437}
2438
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002439static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002440{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002441 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002442 struct btrfs_root *chunk_root = fs_info->chunk_root;
2443 struct btrfs_root *dev_root = fs_info->dev_root;
2444 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002445 struct btrfs_device *device;
2446 u64 old_size;
2447 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002448 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002449 struct btrfs_path *path;
2450 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002451 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002452 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002453 struct extent_buffer *leaf;
2454 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002455 int ret;
2456 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002457 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002458
Chris Masonec44a352008-04-28 15:29:52 -04002459 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002460 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002461 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002462 old_size = device->total_bytes;
2463 size_to_free = div_factor(old_size, 1);
2464 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002465 if (!device->writeable ||
2466 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002467 continue;
2468
2469 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002470 if (ret == -ENOSPC)
2471 break;
Chris Masonec44a352008-04-28 15:29:52 -04002472 BUG_ON(ret);
2473
Yan, Zhenga22285a2010-05-16 10:48:46 -04002474 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002475 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002476
2477 ret = btrfs_grow_device(trans, device, old_size);
2478 BUG_ON(ret);
2479
2480 btrfs_end_transaction(trans, dev_root);
2481 }
2482
2483 /* step two, relocate all the chunks */
2484 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002485 if (!path) {
2486 ret = -ENOMEM;
2487 goto error;
2488 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002489
2490 /* zero out stat counters */
2491 spin_lock(&fs_info->balance_lock);
2492 memset(&bctl->stat, 0, sizeof(bctl->stat));
2493 spin_unlock(&fs_info->balance_lock);
2494again:
Chris Masonec44a352008-04-28 15:29:52 -04002495 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2496 key.offset = (u64)-1;
2497 key.type = BTRFS_CHUNK_ITEM_KEY;
2498
Chris Masond3977122009-01-05 21:25:51 -05002499 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002500 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002501 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002502 ret = -ECANCELED;
2503 goto error;
2504 }
2505
Chris Masonec44a352008-04-28 15:29:52 -04002506 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2507 if (ret < 0)
2508 goto error;
2509
2510 /*
2511 * this shouldn't happen, it means the last relocate
2512 * failed
2513 */
2514 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002515 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002516
2517 ret = btrfs_previous_item(chunk_root, path, 0,
2518 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002519 if (ret) {
2520 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002521 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002522 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002523
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002524 leaf = path->nodes[0];
2525 slot = path->slots[0];
2526 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2527
Chris Masonec44a352008-04-28 15:29:52 -04002528 if (found_key.objectid != key.objectid)
2529 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002530
Chris Masonec44a352008-04-28 15:29:52 -04002531 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002532 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002533 break;
2534
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002535 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2536
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002537 if (!counting) {
2538 spin_lock(&fs_info->balance_lock);
2539 bctl->stat.considered++;
2540 spin_unlock(&fs_info->balance_lock);
2541 }
2542
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002543 ret = should_balance_chunk(chunk_root, leaf, chunk,
2544 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002545 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002546 if (!ret)
2547 goto loop;
2548
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002549 if (counting) {
2550 spin_lock(&fs_info->balance_lock);
2551 bctl->stat.expected++;
2552 spin_unlock(&fs_info->balance_lock);
2553 goto loop;
2554 }
2555
Chris Masonec44a352008-04-28 15:29:52 -04002556 ret = btrfs_relocate_chunk(chunk_root,
2557 chunk_root->root_key.objectid,
2558 found_key.objectid,
2559 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002560 if (ret && ret != -ENOSPC)
2561 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002562 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002563 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002564 } else {
2565 spin_lock(&fs_info->balance_lock);
2566 bctl->stat.completed++;
2567 spin_unlock(&fs_info->balance_lock);
2568 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002569loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002570 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002571 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002572
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002573 if (counting) {
2574 btrfs_release_path(path);
2575 counting = false;
2576 goto again;
2577 }
Chris Masonec44a352008-04-28 15:29:52 -04002578error:
2579 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002580 if (enospc_errors) {
2581 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2582 enospc_errors);
2583 if (!ret)
2584 ret = -ENOSPC;
2585 }
2586
Chris Masonec44a352008-04-28 15:29:52 -04002587 return ret;
2588}
2589
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002590static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2591{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002592 /* cancel requested || normal exit path */
2593 return atomic_read(&fs_info->balance_cancel_req) ||
2594 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2595 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002596}
2597
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002598static void __cancel_balance(struct btrfs_fs_info *fs_info)
2599{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002600 int ret;
2601
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002602 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002603 ret = del_balance_item(fs_info->tree_root);
2604 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002605}
2606
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002607void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002608 struct btrfs_ioctl_balance_args *bargs);
2609
2610/*
2611 * Should be called with both balance and volume mutexes held
2612 */
2613int btrfs_balance(struct btrfs_balance_control *bctl,
2614 struct btrfs_ioctl_balance_args *bargs)
2615{
2616 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002617 u64 allowed;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002618 int ret;
2619
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002620 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002621 atomic_read(&fs_info->balance_pause_req) ||
2622 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002623 ret = -EINVAL;
2624 goto out;
2625 }
2626
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002627 /*
2628 * In case of mixed groups both data and meta should be picked,
2629 * and identical options should be given for both of them.
2630 */
2631 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2632 if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
2633 (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
2634 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2635 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2636 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2637 printk(KERN_ERR "btrfs: with mixed groups data and "
2638 "metadata balance options must be the same\n");
2639 ret = -EINVAL;
2640 goto out;
2641 }
2642 }
2643
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002644 /*
2645 * Profile changing sanity checks. Skip them if a simple
2646 * balance is requested.
2647 */
2648 if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
2649 BTRFS_BALANCE_ARGS_CONVERT))
2650 goto do_balance;
2651
2652 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2653 if (fs_info->fs_devices->num_devices == 1)
2654 allowed |= BTRFS_BLOCK_GROUP_DUP;
2655 else if (fs_info->fs_devices->num_devices < 4)
2656 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2657 else
2658 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2659 BTRFS_BLOCK_GROUP_RAID10);
2660
2661 if (!profile_is_valid(bctl->data.target, 1) ||
2662 bctl->data.target & ~allowed) {
2663 printk(KERN_ERR "btrfs: unable to start balance with target "
2664 "data profile %llu\n",
2665 (unsigned long long)bctl->data.target);
2666 ret = -EINVAL;
2667 goto out;
2668 }
2669 if (!profile_is_valid(bctl->meta.target, 1) ||
2670 bctl->meta.target & ~allowed) {
2671 printk(KERN_ERR "btrfs: unable to start balance with target "
2672 "metadata profile %llu\n",
2673 (unsigned long long)bctl->meta.target);
2674 ret = -EINVAL;
2675 goto out;
2676 }
2677 if (!profile_is_valid(bctl->sys.target, 1) ||
2678 bctl->sys.target & ~allowed) {
2679 printk(KERN_ERR "btrfs: unable to start balance with target "
2680 "system profile %llu\n",
2681 (unsigned long long)bctl->sys.target);
2682 ret = -EINVAL;
2683 goto out;
2684 }
2685
2686 if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
2687 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2688 ret = -EINVAL;
2689 goto out;
2690 }
2691
2692 /* allow to reduce meta or sys integrity only if force set */
2693 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2694 BTRFS_BLOCK_GROUP_RAID10;
2695 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2696 (fs_info->avail_system_alloc_bits & allowed) &&
2697 !(bctl->sys.target & allowed)) ||
2698 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2699 (fs_info->avail_metadata_alloc_bits & allowed) &&
2700 !(bctl->meta.target & allowed))) {
2701 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2702 printk(KERN_INFO "btrfs: force reducing metadata "
2703 "integrity\n");
2704 } else {
2705 printk(KERN_ERR "btrfs: balance will reduce metadata "
2706 "integrity, use force if you want this\n");
2707 ret = -EINVAL;
2708 goto out;
2709 }
2710 }
2711
2712do_balance:
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002713 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002714 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002715 goto out;
2716
Ilya Dryomov59641012012-01-16 22:04:48 +02002717 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2718 BUG_ON(ret == -EEXIST);
2719 set_balance_control(bctl);
2720 } else {
2721 BUG_ON(ret != -EEXIST);
2722 spin_lock(&fs_info->balance_lock);
2723 update_balance_args(bctl);
2724 spin_unlock(&fs_info->balance_lock);
2725 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002726
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002727 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002728 mutex_unlock(&fs_info->balance_mutex);
2729
2730 ret = __btrfs_balance(fs_info);
2731
2732 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002733 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002734
2735 if (bargs) {
2736 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002737 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002738 }
2739
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002740 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2741 balance_need_close(fs_info)) {
2742 __cancel_balance(fs_info);
2743 }
2744
2745 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002746
2747 return ret;
2748out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002749 if (bctl->flags & BTRFS_BALANCE_RESUME)
2750 __cancel_balance(fs_info);
2751 else
2752 kfree(bctl);
2753 return ret;
2754}
2755
2756static int balance_kthread(void *data)
2757{
2758 struct btrfs_balance_control *bctl =
2759 (struct btrfs_balance_control *)data;
2760 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002761 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002762
2763 mutex_lock(&fs_info->volume_mutex);
2764 mutex_lock(&fs_info->balance_mutex);
2765
2766 set_balance_control(bctl);
2767
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002768 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2769 printk(KERN_INFO "btrfs: force skipping balance\n");
2770 } else {
2771 printk(KERN_INFO "btrfs: continuing balance\n");
2772 ret = btrfs_balance(bctl, NULL);
2773 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002774
2775 mutex_unlock(&fs_info->balance_mutex);
2776 mutex_unlock(&fs_info->volume_mutex);
2777 return ret;
2778}
2779
2780int btrfs_recover_balance(struct btrfs_root *tree_root)
2781{
2782 struct task_struct *tsk;
2783 struct btrfs_balance_control *bctl;
2784 struct btrfs_balance_item *item;
2785 struct btrfs_disk_balance_args disk_bargs;
2786 struct btrfs_path *path;
2787 struct extent_buffer *leaf;
2788 struct btrfs_key key;
2789 int ret;
2790
2791 path = btrfs_alloc_path();
2792 if (!path)
2793 return -ENOMEM;
2794
2795 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2796 if (!bctl) {
2797 ret = -ENOMEM;
2798 goto out;
2799 }
2800
2801 key.objectid = BTRFS_BALANCE_OBJECTID;
2802 key.type = BTRFS_BALANCE_ITEM_KEY;
2803 key.offset = 0;
2804
2805 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2806 if (ret < 0)
2807 goto out_bctl;
2808 if (ret > 0) { /* ret = -ENOENT; */
2809 ret = 0;
2810 goto out_bctl;
2811 }
2812
2813 leaf = path->nodes[0];
2814 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2815
2816 bctl->fs_info = tree_root->fs_info;
2817 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2818
2819 btrfs_balance_data(leaf, item, &disk_bargs);
2820 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2821 btrfs_balance_meta(leaf, item, &disk_bargs);
2822 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2823 btrfs_balance_sys(leaf, item, &disk_bargs);
2824 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2825
2826 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2827 if (IS_ERR(tsk))
2828 ret = PTR_ERR(tsk);
2829 else
2830 goto out;
2831
2832out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002833 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002834out:
2835 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002836 return ret;
2837}
2838
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002839int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2840{
2841 int ret = 0;
2842
2843 mutex_lock(&fs_info->balance_mutex);
2844 if (!fs_info->balance_ctl) {
2845 mutex_unlock(&fs_info->balance_mutex);
2846 return -ENOTCONN;
2847 }
2848
2849 if (atomic_read(&fs_info->balance_running)) {
2850 atomic_inc(&fs_info->balance_pause_req);
2851 mutex_unlock(&fs_info->balance_mutex);
2852
2853 wait_event(fs_info->balance_wait_q,
2854 atomic_read(&fs_info->balance_running) == 0);
2855
2856 mutex_lock(&fs_info->balance_mutex);
2857 /* we are good with balance_ctl ripped off from under us */
2858 BUG_ON(atomic_read(&fs_info->balance_running));
2859 atomic_dec(&fs_info->balance_pause_req);
2860 } else {
2861 ret = -ENOTCONN;
2862 }
2863
2864 mutex_unlock(&fs_info->balance_mutex);
2865 return ret;
2866}
2867
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002868int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2869{
2870 mutex_lock(&fs_info->balance_mutex);
2871 if (!fs_info->balance_ctl) {
2872 mutex_unlock(&fs_info->balance_mutex);
2873 return -ENOTCONN;
2874 }
2875
2876 atomic_inc(&fs_info->balance_cancel_req);
2877 /*
2878 * if we are running just wait and return, balance item is
2879 * deleted in btrfs_balance in this case
2880 */
2881 if (atomic_read(&fs_info->balance_running)) {
2882 mutex_unlock(&fs_info->balance_mutex);
2883 wait_event(fs_info->balance_wait_q,
2884 atomic_read(&fs_info->balance_running) == 0);
2885 mutex_lock(&fs_info->balance_mutex);
2886 } else {
2887 /* __cancel_balance needs volume_mutex */
2888 mutex_unlock(&fs_info->balance_mutex);
2889 mutex_lock(&fs_info->volume_mutex);
2890 mutex_lock(&fs_info->balance_mutex);
2891
2892 if (fs_info->balance_ctl)
2893 __cancel_balance(fs_info);
2894
2895 mutex_unlock(&fs_info->volume_mutex);
2896 }
2897
2898 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2899 atomic_dec(&fs_info->balance_cancel_req);
2900 mutex_unlock(&fs_info->balance_mutex);
2901 return 0;
2902}
2903
Chris Mason8f18cf12008-04-25 16:53:30 -04002904/*
2905 * shrinking a device means finding all of the device extents past
2906 * the new size, and then following the back refs to the chunks.
2907 * The chunk relocation code actually frees the device extent
2908 */
2909int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2910{
2911 struct btrfs_trans_handle *trans;
2912 struct btrfs_root *root = device->dev_root;
2913 struct btrfs_dev_extent *dev_extent = NULL;
2914 struct btrfs_path *path;
2915 u64 length;
2916 u64 chunk_tree;
2917 u64 chunk_objectid;
2918 u64 chunk_offset;
2919 int ret;
2920 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002921 int failed = 0;
2922 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002923 struct extent_buffer *l;
2924 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002925 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002926 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002927 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002928 u64 diff = device->total_bytes - new_size;
2929
Yan Zheng2b820322008-11-17 21:11:30 -05002930 if (new_size >= device->total_bytes)
2931 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002932
2933 path = btrfs_alloc_path();
2934 if (!path)
2935 return -ENOMEM;
2936
Chris Mason8f18cf12008-04-25 16:53:30 -04002937 path->reada = 2;
2938
Chris Mason7d9eb122008-07-08 14:19:17 -04002939 lock_chunks(root);
2940
Chris Mason8f18cf12008-04-25 16:53:30 -04002941 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002942 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002943 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002944 spin_lock(&root->fs_info->free_chunk_lock);
2945 root->fs_info->free_chunk_space -= diff;
2946 spin_unlock(&root->fs_info->free_chunk_lock);
2947 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002948 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002949
Josef Bacikba1bf482009-09-11 16:11:19 -04002950again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002951 key.objectid = device->devid;
2952 key.offset = (u64)-1;
2953 key.type = BTRFS_DEV_EXTENT_KEY;
2954
2955 while (1) {
2956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2957 if (ret < 0)
2958 goto done;
2959
2960 ret = btrfs_previous_item(root, path, 0, key.type);
2961 if (ret < 0)
2962 goto done;
2963 if (ret) {
2964 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002965 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002966 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002967 }
2968
2969 l = path->nodes[0];
2970 slot = path->slots[0];
2971 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2972
Josef Bacikba1bf482009-09-11 16:11:19 -04002973 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002974 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002975 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002976 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002977
2978 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2979 length = btrfs_dev_extent_length(l, dev_extent);
2980
Josef Bacikba1bf482009-09-11 16:11:19 -04002981 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002982 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002983 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002984 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002985
2986 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2987 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2988 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002989 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002990
2991 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2992 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002993 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002994 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002995 if (ret == -ENOSPC)
2996 failed++;
2997 key.offset -= 1;
2998 }
2999
3000 if (failed && !retried) {
3001 failed = 0;
3002 retried = true;
3003 goto again;
3004 } else if (failed && retried) {
3005 ret = -ENOSPC;
3006 lock_chunks(root);
3007
3008 device->total_bytes = old_size;
3009 if (device->writeable)
3010 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003011 spin_lock(&root->fs_info->free_chunk_lock);
3012 root->fs_info->free_chunk_space += diff;
3013 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003014 unlock_chunks(root);
3015 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003016 }
3017
Chris Balld6397ba2009-04-27 07:29:03 -04003018 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003019 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003020 if (IS_ERR(trans)) {
3021 ret = PTR_ERR(trans);
3022 goto done;
3023 }
3024
Chris Balld6397ba2009-04-27 07:29:03 -04003025 lock_chunks(root);
3026
3027 device->disk_total_bytes = new_size;
3028 /* Now btrfs_update_device() will change the on-disk size. */
3029 ret = btrfs_update_device(trans, device);
3030 if (ret) {
3031 unlock_chunks(root);
3032 btrfs_end_transaction(trans, root);
3033 goto done;
3034 }
3035 WARN_ON(diff > old_total);
3036 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3037 unlock_chunks(root);
3038 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003039done:
3040 btrfs_free_path(path);
3041 return ret;
3042}
3043
Li Zefan125ccb02011-12-08 15:07:24 +08003044static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003045 struct btrfs_key *key,
3046 struct btrfs_chunk *chunk, int item_size)
3047{
David Sterba6c417612011-04-13 15:41:04 +02003048 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003049 struct btrfs_disk_key disk_key;
3050 u32 array_size;
3051 u8 *ptr;
3052
3053 array_size = btrfs_super_sys_array_size(super_copy);
3054 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3055 return -EFBIG;
3056
3057 ptr = super_copy->sys_chunk_array + array_size;
3058 btrfs_cpu_key_to_disk(&disk_key, key);
3059 memcpy(ptr, &disk_key, sizeof(disk_key));
3060 ptr += sizeof(disk_key);
3061 memcpy(ptr, chunk, item_size);
3062 item_size += sizeof(disk_key);
3063 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3064 return 0;
3065}
3066
Miao Xieb2117a32011-01-05 10:07:28 +00003067/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003068 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003069 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003070static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003071{
Arne Jansen73c5de02011-04-12 12:07:57 +02003072 const struct btrfs_device_info *di_a = a;
3073 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003074
Arne Jansen73c5de02011-04-12 12:07:57 +02003075 if (di_a->max_avail > di_b->max_avail)
3076 return -1;
3077 if (di_a->max_avail < di_b->max_avail)
3078 return 1;
3079 if (di_a->total_avail > di_b->total_avail)
3080 return -1;
3081 if (di_a->total_avail < di_b->total_avail)
3082 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003083 return 0;
3084}
3085
3086static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3087 struct btrfs_root *extent_root,
3088 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003089 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003090 u64 start, u64 type)
3091{
3092 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003093 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3094 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003095 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003096 struct extent_map_tree *em_tree;
3097 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003098 struct btrfs_device_info *devices_info = NULL;
3099 u64 total_avail;
3100 int num_stripes; /* total number of stripes to allocate */
3101 int sub_stripes; /* sub_stripes info for map */
3102 int dev_stripes; /* stripes per dev */
3103 int devs_max; /* max devs to use */
3104 int devs_min; /* min devs needed */
3105 int devs_increment; /* ndevs has to be a multiple of this */
3106 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003107 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003108 u64 max_stripe_size;
3109 u64 max_chunk_size;
3110 u64 stripe_size;
3111 u64 num_bytes;
3112 int ndevs;
3113 int i;
3114 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003115
3116 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
3117 (type & BTRFS_BLOCK_GROUP_DUP)) {
3118 WARN_ON(1);
3119 type &= ~BTRFS_BLOCK_GROUP_DUP;
3120 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003121
Miao Xieb2117a32011-01-05 10:07:28 +00003122 if (list_empty(&fs_devices->alloc_list))
3123 return -ENOSPC;
3124
Arne Jansen73c5de02011-04-12 12:07:57 +02003125 sub_stripes = 1;
3126 dev_stripes = 1;
3127 devs_increment = 1;
3128 ncopies = 1;
3129 devs_max = 0; /* 0 == as many as possible */
3130 devs_min = 1;
3131
3132 /*
3133 * define the properties of each RAID type.
3134 * FIXME: move this to a global table and use it in all RAID
3135 * calculation code
3136 */
3137 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3138 dev_stripes = 2;
3139 ncopies = 2;
3140 devs_max = 1;
3141 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3142 devs_min = 2;
3143 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3144 devs_increment = 2;
3145 ncopies = 2;
3146 devs_max = 2;
3147 devs_min = 2;
3148 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3149 sub_stripes = 2;
3150 devs_increment = 2;
3151 ncopies = 2;
3152 devs_min = 4;
3153 } else {
3154 devs_max = 1;
3155 }
3156
3157 if (type & BTRFS_BLOCK_GROUP_DATA) {
3158 max_stripe_size = 1024 * 1024 * 1024;
3159 max_chunk_size = 10 * max_stripe_size;
3160 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003161 /* for larger filesystems, use larger metadata chunks */
3162 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3163 max_stripe_size = 1024 * 1024 * 1024;
3164 else
3165 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003166 max_chunk_size = max_stripe_size;
3167 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003168 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003169 max_chunk_size = 2 * max_stripe_size;
3170 } else {
3171 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3172 type);
3173 BUG_ON(1);
3174 }
3175
3176 /* we don't want a chunk larger than 10% of writeable space */
3177 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3178 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003179
3180 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3181 GFP_NOFS);
3182 if (!devices_info)
3183 return -ENOMEM;
3184
Arne Jansen73c5de02011-04-12 12:07:57 +02003185 cur = fs_devices->alloc_list.next;
3186
3187 /*
3188 * in the first pass through the devices list, we gather information
3189 * about the available holes on each device.
3190 */
3191 ndevs = 0;
3192 while (cur != &fs_devices->alloc_list) {
3193 struct btrfs_device *device;
3194 u64 max_avail;
3195 u64 dev_offset;
3196
3197 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3198
3199 cur = cur->next;
3200
3201 if (!device->writeable) {
3202 printk(KERN_ERR
3203 "btrfs: read-only device in alloc_list\n");
3204 WARN_ON(1);
3205 continue;
3206 }
3207
3208 if (!device->in_fs_metadata)
3209 continue;
3210
3211 if (device->total_bytes > device->bytes_used)
3212 total_avail = device->total_bytes - device->bytes_used;
3213 else
3214 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003215
3216 /* If there is no space on this device, skip it. */
3217 if (total_avail == 0)
3218 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003219
Li Zefan125ccb02011-12-08 15:07:24 +08003220 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003221 max_stripe_size * dev_stripes,
3222 &dev_offset, &max_avail);
3223 if (ret && ret != -ENOSPC)
3224 goto error;
3225
3226 if (ret == 0)
3227 max_avail = max_stripe_size * dev_stripes;
3228
3229 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3230 continue;
3231
3232 devices_info[ndevs].dev_offset = dev_offset;
3233 devices_info[ndevs].max_avail = max_avail;
3234 devices_info[ndevs].total_avail = total_avail;
3235 devices_info[ndevs].dev = device;
3236 ++ndevs;
3237 }
3238
3239 /*
3240 * now sort the devices by hole size / available space
3241 */
3242 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3243 btrfs_cmp_device_info, NULL);
3244
3245 /* round down to number of usable stripes */
3246 ndevs -= ndevs % devs_increment;
3247
3248 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3249 ret = -ENOSPC;
3250 goto error;
3251 }
3252
3253 if (devs_max && ndevs > devs_max)
3254 ndevs = devs_max;
3255 /*
3256 * the primary goal is to maximize the number of stripes, so use as many
3257 * devices as possible, even if the stripes are not maximum sized.
3258 */
3259 stripe_size = devices_info[ndevs-1].max_avail;
3260 num_stripes = ndevs * dev_stripes;
3261
3262 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3263 stripe_size = max_chunk_size * ncopies;
3264 do_div(stripe_size, num_stripes);
3265 }
3266
3267 do_div(stripe_size, dev_stripes);
3268 do_div(stripe_size, BTRFS_STRIPE_LEN);
3269 stripe_size *= BTRFS_STRIPE_LEN;
3270
Miao Xieb2117a32011-01-05 10:07:28 +00003271 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3272 if (!map) {
3273 ret = -ENOMEM;
3274 goto error;
3275 }
3276 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003277
Arne Jansen73c5de02011-04-12 12:07:57 +02003278 for (i = 0; i < ndevs; ++i) {
3279 for (j = 0; j < dev_stripes; ++j) {
3280 int s = i * dev_stripes + j;
3281 map->stripes[s].dev = devices_info[i].dev;
3282 map->stripes[s].physical = devices_info[i].dev_offset +
3283 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003284 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003285 }
Chris Mason593060d2008-03-25 16:50:33 -04003286 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003287 map->stripe_len = BTRFS_STRIPE_LEN;
3288 map->io_align = BTRFS_STRIPE_LEN;
3289 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003290 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003291 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003292
Yan Zheng2b820322008-11-17 21:11:30 -05003293 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003294 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003295
Arne Jansen73c5de02011-04-12 12:07:57 +02003296 *stripe_size_out = stripe_size;
3297 *num_bytes_out = num_bytes;
3298
3299 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003300
David Sterba172ddd62011-04-21 00:48:27 +02003301 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003302 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003303 ret = -ENOMEM;
3304 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003305 }
Chris Mason0b86a832008-03-24 15:01:56 -04003306 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003307 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003308 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003309 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003310 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003311
Chris Mason0b86a832008-03-24 15:01:56 -04003312 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003313 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003314 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003315 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04003316 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003317 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05003318
3319 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3320 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003321 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05003322 BUG_ON(ret);
3323
Arne Jansen73c5de02011-04-12 12:07:57 +02003324 for (i = 0; i < map->num_stripes; ++i) {
3325 struct btrfs_device *device;
3326 u64 dev_offset;
3327
3328 device = map->stripes[i].dev;
3329 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003330
3331 ret = btrfs_alloc_dev_extent(trans, device,
3332 info->chunk_root->root_key.objectid,
3333 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003334 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003335 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003336 }
3337
Miao Xieb2117a32011-01-05 10:07:28 +00003338 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003339 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003340
3341error:
3342 kfree(map);
3343 kfree(devices_info);
3344 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003345}
3346
3347static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3348 struct btrfs_root *extent_root,
3349 struct map_lookup *map, u64 chunk_offset,
3350 u64 chunk_size, u64 stripe_size)
3351{
3352 u64 dev_offset;
3353 struct btrfs_key key;
3354 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3355 struct btrfs_device *device;
3356 struct btrfs_chunk *chunk;
3357 struct btrfs_stripe *stripe;
3358 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3359 int index = 0;
3360 int ret;
3361
3362 chunk = kzalloc(item_size, GFP_NOFS);
3363 if (!chunk)
3364 return -ENOMEM;
3365
3366 index = 0;
3367 while (index < map->num_stripes) {
3368 device = map->stripes[index].dev;
3369 device->bytes_used += stripe_size;
3370 ret = btrfs_update_device(trans, device);
3371 BUG_ON(ret);
3372 index++;
3373 }
3374
Josef Bacik2bf64752011-09-26 17:12:22 -04003375 spin_lock(&extent_root->fs_info->free_chunk_lock);
3376 extent_root->fs_info->free_chunk_space -= (stripe_size *
3377 map->num_stripes);
3378 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3379
Yan Zheng2b820322008-11-17 21:11:30 -05003380 index = 0;
3381 stripe = &chunk->stripe;
3382 while (index < map->num_stripes) {
3383 device = map->stripes[index].dev;
3384 dev_offset = map->stripes[index].physical;
3385
3386 btrfs_set_stack_stripe_devid(stripe, device->devid);
3387 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3388 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3389 stripe++;
3390 index++;
3391 }
3392
3393 btrfs_set_stack_chunk_length(chunk, chunk_size);
3394 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3395 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3396 btrfs_set_stack_chunk_type(chunk, map->type);
3397 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3398 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3399 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3400 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3401 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3402
3403 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3404 key.type = BTRFS_CHUNK_ITEM_KEY;
3405 key.offset = chunk_offset;
3406
3407 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
3408 BUG_ON(ret);
3409
3410 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Li Zefan125ccb02011-12-08 15:07:24 +08003411 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003412 item_size);
3413 BUG_ON(ret);
3414 }
liubo1abe9b82011-03-24 11:18:59 +00003415
Yan Zheng2b820322008-11-17 21:11:30 -05003416 kfree(chunk);
3417 return 0;
3418}
3419
3420/*
3421 * Chunk allocation falls into two parts. The first part does works
3422 * that make the new allocated chunk useable, but not do any operation
3423 * that modifies the chunk tree. The second part does the works that
3424 * require modifying the chunk tree. This division is important for the
3425 * bootstrap process of adding storage to a seed btrfs.
3426 */
3427int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3428 struct btrfs_root *extent_root, u64 type)
3429{
3430 u64 chunk_offset;
3431 u64 chunk_size;
3432 u64 stripe_size;
3433 struct map_lookup *map;
3434 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3435 int ret;
3436
3437 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3438 &chunk_offset);
3439 if (ret)
3440 return ret;
3441
3442 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3443 &stripe_size, chunk_offset, type);
3444 if (ret)
3445 return ret;
3446
3447 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3448 chunk_size, stripe_size);
3449 BUG_ON(ret);
3450 return 0;
3451}
3452
Chris Masond3977122009-01-05 21:25:51 -05003453static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003454 struct btrfs_root *root,
3455 struct btrfs_device *device)
3456{
3457 u64 chunk_offset;
3458 u64 sys_chunk_offset;
3459 u64 chunk_size;
3460 u64 sys_chunk_size;
3461 u64 stripe_size;
3462 u64 sys_stripe_size;
3463 u64 alloc_profile;
3464 struct map_lookup *map;
3465 struct map_lookup *sys_map;
3466 struct btrfs_fs_info *fs_info = root->fs_info;
3467 struct btrfs_root *extent_root = fs_info->extent_root;
3468 int ret;
3469
3470 ret = find_next_chunk(fs_info->chunk_root,
3471 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003472 if (ret)
3473 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003474
3475 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003476 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003477 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3478
3479 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3480 &stripe_size, chunk_offset, alloc_profile);
3481 BUG_ON(ret);
3482
3483 sys_chunk_offset = chunk_offset + chunk_size;
3484
3485 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003486 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003487 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3488
3489 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3490 &sys_chunk_size, &sys_stripe_size,
3491 sys_chunk_offset, alloc_profile);
3492 BUG_ON(ret);
3493
3494 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3495 BUG_ON(ret);
3496
3497 /*
3498 * Modifying chunk tree needs allocating new blocks from both
3499 * system block group and metadata block group. So we only can
3500 * do operations require modifying the chunk tree after both
3501 * block groups were created.
3502 */
3503 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3504 chunk_size, stripe_size);
3505 BUG_ON(ret);
3506
3507 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3508 sys_chunk_offset, sys_chunk_size,
3509 sys_stripe_size);
3510 BUG_ON(ret);
3511 return 0;
3512}
3513
3514int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3515{
3516 struct extent_map *em;
3517 struct map_lookup *map;
3518 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3519 int readonly = 0;
3520 int i;
3521
Chris Mason890871b2009-09-02 16:24:52 -04003522 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003523 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003524 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003525 if (!em)
3526 return 1;
3527
Josef Bacikf48b9072010-01-27 02:07:59 +00003528 if (btrfs_test_opt(root, DEGRADED)) {
3529 free_extent_map(em);
3530 return 0;
3531 }
3532
Yan Zheng2b820322008-11-17 21:11:30 -05003533 map = (struct map_lookup *)em->bdev;
3534 for (i = 0; i < map->num_stripes; i++) {
3535 if (!map->stripes[i].dev->writeable) {
3536 readonly = 1;
3537 break;
3538 }
3539 }
3540 free_extent_map(em);
3541 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003542}
3543
3544void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3545{
David Sterbaa8067e02011-04-21 00:34:43 +02003546 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003547}
3548
3549void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3550{
3551 struct extent_map *em;
3552
Chris Masond3977122009-01-05 21:25:51 -05003553 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003554 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003555 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3556 if (em)
3557 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003558 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003559 if (!em)
3560 break;
3561 kfree(em->bdev);
3562 /* once for us */
3563 free_extent_map(em);
3564 /* once for the tree */
3565 free_extent_map(em);
3566 }
3567}
3568
Chris Masonf1885912008-04-09 16:28:12 -04003569int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3570{
3571 struct extent_map *em;
3572 struct map_lookup *map;
3573 struct extent_map_tree *em_tree = &map_tree->map_tree;
3574 int ret;
3575
Chris Mason890871b2009-09-02 16:24:52 -04003576 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003577 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003578 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003579 BUG_ON(!em);
3580
3581 BUG_ON(em->start > logical || em->start + em->len < logical);
3582 map = (struct map_lookup *)em->bdev;
3583 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3584 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003585 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3586 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003587 else
3588 ret = 1;
3589 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003590 return ret;
3591}
3592
Chris Masondfe25022008-05-13 13:46:40 -04003593static int find_live_mirror(struct map_lookup *map, int first, int num,
3594 int optimal)
3595{
3596 int i;
3597 if (map->stripes[optimal].dev->bdev)
3598 return optimal;
3599 for (i = first; i < first + num; i++) {
3600 if (map->stripes[i].dev->bdev)
3601 return i;
3602 }
3603 /* we couldn't find one that doesn't fail. Just return something
3604 * and the io error handling code will clean up eventually
3605 */
3606 return optimal;
3607}
3608
Chris Masonf2d8d742008-04-21 10:03:05 -04003609static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3610 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003611 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003612 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003613{
3614 struct extent_map *em;
3615 struct map_lookup *map;
3616 struct extent_map_tree *em_tree = &map_tree->map_tree;
3617 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003618 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003619 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003620 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003621 u64 stripe_nr_orig;
3622 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003623 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003624 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003625 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003626 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003627 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003628 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003629
Chris Mason890871b2009-09-02 16:24:52 -04003630 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003631 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003632 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003633
Chris Mason3b951512008-04-17 11:29:12 -04003634 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003635 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3636 (unsigned long long)logical,
3637 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003638 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003639 }
Chris Mason0b86a832008-03-24 15:01:56 -04003640
3641 BUG_ON(em->start > logical || em->start + em->len < logical);
3642 map = (struct map_lookup *)em->bdev;
3643 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003644
Chris Masonf1885912008-04-09 16:28:12 -04003645 if (mirror_num > map->num_stripes)
3646 mirror_num = 0;
3647
Chris Mason593060d2008-03-25 16:50:33 -04003648 stripe_nr = offset;
3649 /*
3650 * stripe_nr counts the total number of stripes we have to stride
3651 * to get to this block
3652 */
3653 do_div(stripe_nr, map->stripe_len);
3654
3655 stripe_offset = stripe_nr * map->stripe_len;
3656 BUG_ON(offset < stripe_offset);
3657
3658 /* stripe_offset is the offset of this block in its stripe*/
3659 stripe_offset = offset - stripe_offset;
3660
Li Dongyangfce3bb92011-03-24 10:24:26 +00003661 if (rw & REQ_DISCARD)
3662 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003663 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003664 /* we limit the length of each bio to what fits in a stripe */
3665 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003666 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003667 } else {
3668 *length = em->len - offset;
3669 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003670
Jan Schmidta1d3c472011-08-04 17:15:33 +02003671 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003672 goto out;
3673
Chris Masonf2d8d742008-04-21 10:03:05 -04003674 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003675 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003676 stripe_nr_orig = stripe_nr;
3677 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3678 (~(map->stripe_len - 1));
3679 do_div(stripe_nr_end, map->stripe_len);
3680 stripe_end_offset = stripe_nr_end * map->stripe_len -
3681 (offset + *length);
3682 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3683 if (rw & REQ_DISCARD)
3684 num_stripes = min_t(u64, map->num_stripes,
3685 stripe_nr_end - stripe_nr_orig);
3686 stripe_index = do_div(stripe_nr, map->num_stripes);
3687 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003688 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003689 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003690 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003691 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003692 else {
3693 stripe_index = find_live_mirror(map, 0,
3694 map->num_stripes,
3695 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003696 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003697 }
Chris Mason2fff7342008-04-29 14:12:09 -04003698
Chris Mason611f0e02008-04-03 16:29:03 -04003699 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003700 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003701 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003702 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003703 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003704 } else {
3705 mirror_num = 1;
3706 }
Chris Mason2fff7342008-04-29 14:12:09 -04003707
Chris Mason321aecc2008-04-16 10:49:51 -04003708 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3709 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003710
3711 stripe_index = do_div(stripe_nr, factor);
3712 stripe_index *= map->sub_stripes;
3713
Jens Axboe7eaceac2011-03-10 08:52:07 +01003714 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003715 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003716 else if (rw & REQ_DISCARD)
3717 num_stripes = min_t(u64, map->sub_stripes *
3718 (stripe_nr_end - stripe_nr_orig),
3719 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003720 else if (mirror_num)
3721 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003722 else {
3723 stripe_index = find_live_mirror(map, stripe_index,
3724 map->sub_stripes, stripe_index +
3725 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003726 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003727 }
Chris Mason8790d502008-04-03 16:29:03 -04003728 } else {
3729 /*
3730 * after this do_div call, stripe_nr is the number of stripes
3731 * on this device we have to walk to find the data, and
3732 * stripe_index is the number of our device in the stripe array
3733 */
3734 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003735 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003736 }
Chris Mason593060d2008-03-25 16:50:33 -04003737 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003738
Li Zefande11cc12011-12-01 12:55:47 +08003739 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3740 if (!bbio) {
3741 ret = -ENOMEM;
3742 goto out;
3743 }
3744 atomic_set(&bbio->error, 0);
3745
Li Dongyangfce3bb92011-03-24 10:24:26 +00003746 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003747 int factor = 0;
3748 int sub_stripes = 0;
3749 u64 stripes_per_dev = 0;
3750 u32 remaining_stripes = 0;
3751
3752 if (map->type &
3753 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3754 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3755 sub_stripes = 1;
3756 else
3757 sub_stripes = map->sub_stripes;
3758
3759 factor = map->num_stripes / sub_stripes;
3760 stripes_per_dev = div_u64_rem(stripe_nr_end -
3761 stripe_nr_orig,
3762 factor,
3763 &remaining_stripes);
3764 }
3765
Li Dongyangfce3bb92011-03-24 10:24:26 +00003766 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003767 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003768 map->stripes[stripe_index].physical +
3769 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003770 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003771
Li Zefanec9ef7a2011-12-01 14:06:42 +08003772 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3773 BTRFS_BLOCK_GROUP_RAID10)) {
3774 bbio->stripes[i].length = stripes_per_dev *
3775 map->stripe_len;
3776 if (i / sub_stripes < remaining_stripes)
3777 bbio->stripes[i].length +=
3778 map->stripe_len;
3779 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003780 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003781 stripe_offset;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003782 if ((i / sub_stripes + 1) %
3783 sub_stripes == remaining_stripes)
3784 bbio->stripes[i].length -=
3785 stripe_end_offset;
3786 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003787 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003788 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003789 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003790
3791 stripe_index++;
3792 if (stripe_index == map->num_stripes) {
3793 /* This could only happen for RAID0/10 */
3794 stripe_index = 0;
3795 stripe_nr++;
3796 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003797 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003798 } else {
3799 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003800 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003801 map->stripes[stripe_index].physical +
3802 stripe_offset +
3803 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003804 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003805 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003806 stripe_index++;
3807 }
Chris Mason593060d2008-03-25 16:50:33 -04003808 }
Li Zefande11cc12011-12-01 12:55:47 +08003809
3810 if (rw & REQ_WRITE) {
3811 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3812 BTRFS_BLOCK_GROUP_RAID10 |
3813 BTRFS_BLOCK_GROUP_DUP)) {
3814 max_errors = 1;
3815 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003816 }
Li Zefande11cc12011-12-01 12:55:47 +08003817
3818 *bbio_ret = bbio;
3819 bbio->num_stripes = num_stripes;
3820 bbio->max_errors = max_errors;
3821 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003822out:
Chris Mason0b86a832008-03-24 15:01:56 -04003823 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003824 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003825}
3826
Chris Masonf2d8d742008-04-21 10:03:05 -04003827int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3828 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003829 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003830{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003831 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003832 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003833}
3834
Yan Zhenga512bbf2008-12-08 16:46:26 -05003835int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3836 u64 chunk_start, u64 physical, u64 devid,
3837 u64 **logical, int *naddrs, int *stripe_len)
3838{
3839 struct extent_map_tree *em_tree = &map_tree->map_tree;
3840 struct extent_map *em;
3841 struct map_lookup *map;
3842 u64 *buf;
3843 u64 bytenr;
3844 u64 length;
3845 u64 stripe_nr;
3846 int i, j, nr = 0;
3847
Chris Mason890871b2009-09-02 16:24:52 -04003848 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003849 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003850 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003851
3852 BUG_ON(!em || em->start != chunk_start);
3853 map = (struct map_lookup *)em->bdev;
3854
3855 length = em->len;
3856 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3857 do_div(length, map->num_stripes / map->sub_stripes);
3858 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3859 do_div(length, map->num_stripes);
3860
3861 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3862 BUG_ON(!buf);
3863
3864 for (i = 0; i < map->num_stripes; i++) {
3865 if (devid && map->stripes[i].dev->devid != devid)
3866 continue;
3867 if (map->stripes[i].physical > physical ||
3868 map->stripes[i].physical + length <= physical)
3869 continue;
3870
3871 stripe_nr = physical - map->stripes[i].physical;
3872 do_div(stripe_nr, map->stripe_len);
3873
3874 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3875 stripe_nr = stripe_nr * map->num_stripes + i;
3876 do_div(stripe_nr, map->sub_stripes);
3877 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3878 stripe_nr = stripe_nr * map->num_stripes + i;
3879 }
3880 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003881 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003882 for (j = 0; j < nr; j++) {
3883 if (buf[j] == bytenr)
3884 break;
3885 }
Chris Mason934d3752008-12-08 16:43:10 -05003886 if (j == nr) {
3887 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003888 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003889 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003890 }
3891
Yan Zhenga512bbf2008-12-08 16:46:26 -05003892 *logical = buf;
3893 *naddrs = nr;
3894 *stripe_len = map->stripe_len;
3895
3896 free_extent_map(em);
3897 return 0;
3898}
3899
Jan Schmidta1d3c472011-08-04 17:15:33 +02003900static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003901{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003902 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003903 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003904
Chris Mason8790d502008-04-03 16:29:03 -04003905 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003906 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003907
Jan Schmidta1d3c472011-08-04 17:15:33 +02003908 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003909 is_orig_bio = 1;
3910
Jan Schmidta1d3c472011-08-04 17:15:33 +02003911 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003912 if (!is_orig_bio) {
3913 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003914 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003915 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003916 bio->bi_private = bbio->private;
3917 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003918 bio->bi_bdev = (struct block_device *)
3919 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003920 /* only send an error to the higher layers if it is
3921 * beyond the tolerance of the multi-bio
3922 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003923 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003924 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003925 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003926 /*
3927 * this bio is actually up to date, we didn't
3928 * go over the max number of errors
3929 */
3930 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003931 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003932 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003933 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003934
3935 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003936 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003937 bio_put(bio);
3938 }
Chris Mason8790d502008-04-03 16:29:03 -04003939}
3940
Chris Mason8b712842008-06-11 16:50:36 -04003941struct async_sched {
3942 struct bio *bio;
3943 int rw;
3944 struct btrfs_fs_info *info;
3945 struct btrfs_work work;
3946};
3947
3948/*
3949 * see run_scheduled_bios for a description of why bios are collected for
3950 * async submit.
3951 *
3952 * This will add one bio to the pending list for a device and make sure
3953 * the work struct is scheduled.
3954 */
Chris Masond3977122009-01-05 21:25:51 -05003955static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003956 struct btrfs_device *device,
3957 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003958{
3959 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003960 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003961
3962 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003963 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003964 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01003965 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003966 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003967 return 0;
3968 }
3969
3970 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003971 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003972 * higher layers. Otherwise, the async bio makes it appear we have
3973 * made progress against dirty pages when we've really just put it
3974 * on a queue for later
3975 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003976 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003977 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003978 bio->bi_next = NULL;
3979 bio->bi_rw |= rw;
3980
3981 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003982 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003983 pending_bios = &device->pending_sync_bios;
3984 else
3985 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003986
Chris Masonffbd5172009-04-20 15:50:09 -04003987 if (pending_bios->tail)
3988 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003989
Chris Masonffbd5172009-04-20 15:50:09 -04003990 pending_bios->tail = bio;
3991 if (!pending_bios->head)
3992 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003993 if (device->running_pending)
3994 should_queue = 0;
3995
3996 spin_unlock(&device->io_lock);
3997
3998 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003999 btrfs_queue_worker(&root->fs_info->submit_workers,
4000 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004001 return 0;
4002}
4003
Chris Masonf1885912008-04-09 16:28:12 -04004004int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004005 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004006{
4007 struct btrfs_mapping_tree *map_tree;
4008 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004009 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004010 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004011 u64 length = 0;
4012 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004013 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004014 int dev_nr = 0;
4015 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004016 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004017
Chris Masonf2d8d742008-04-21 10:03:05 -04004018 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004019 map_tree = &root->fs_info->mapping_tree;
4020 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004021
Jan Schmidta1d3c472011-08-04 17:15:33 +02004022 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004023 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04004024 BUG_ON(ret);
4025
Jan Schmidta1d3c472011-08-04 17:15:33 +02004026 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004027 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004028 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4029 "len %llu\n", (unsigned long long)logical,
4030 (unsigned long long)length,
4031 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004032 BUG();
4033 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004034
4035 bbio->orig_bio = first_bio;
4036 bbio->private = first_bio->bi_private;
4037 bbio->end_io = first_bio->bi_end_io;
4038 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004039
Chris Masond3977122009-01-05 21:25:51 -05004040 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004041 if (dev_nr < total_devs - 1) {
4042 bio = bio_clone(first_bio, GFP_NOFS);
4043 BUG_ON(!bio);
4044 } else {
4045 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004046 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004047 bio->bi_private = bbio;
4048 bio->bi_end_io = btrfs_end_bio;
4049 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4050 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004051 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004052 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4053 "(%s id %llu), size=%u\n", rw,
4054 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4055 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004056 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004057 if (async_submit)
4058 schedule_bio(root, dev, rw, bio);
4059 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004060 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004061 } else {
4062 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4063 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004064 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004065 }
Chris Mason8790d502008-04-03 16:29:03 -04004066 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004067 }
Chris Mason0b86a832008-03-24 15:01:56 -04004068 return 0;
4069}
4070
Chris Masona4437552008-04-18 10:29:38 -04004071struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004072 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004073{
Yan Zheng2b820322008-11-17 21:11:30 -05004074 struct btrfs_device *device;
4075 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004076
Yan Zheng2b820322008-11-17 21:11:30 -05004077 cur_devices = root->fs_info->fs_devices;
4078 while (cur_devices) {
4079 if (!fsid ||
4080 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4081 device = __find_device(&cur_devices->devices,
4082 devid, uuid);
4083 if (device)
4084 return device;
4085 }
4086 cur_devices = cur_devices->seed;
4087 }
4088 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004089}
4090
Chris Masondfe25022008-05-13 13:46:40 -04004091static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4092 u64 devid, u8 *dev_uuid)
4093{
4094 struct btrfs_device *device;
4095 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4096
4097 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004098 if (!device)
4099 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004100 list_add(&device->dev_list,
4101 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004102 device->dev_root = root->fs_info->dev_root;
4103 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004104 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004105 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004106 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004107 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004108 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004109 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004110 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004111 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4112 return device;
4113}
4114
Chris Mason0b86a832008-03-24 15:01:56 -04004115static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4116 struct extent_buffer *leaf,
4117 struct btrfs_chunk *chunk)
4118{
4119 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4120 struct map_lookup *map;
4121 struct extent_map *em;
4122 u64 logical;
4123 u64 length;
4124 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004125 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004126 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004127 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004128 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004129
Chris Masone17cade2008-04-15 15:41:47 -04004130 logical = key->offset;
4131 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004132
Chris Mason890871b2009-09-02 16:24:52 -04004133 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004134 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004135 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004136
4137 /* already mapped? */
4138 if (em && em->start <= logical && em->start + em->len > logical) {
4139 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004140 return 0;
4141 } else if (em) {
4142 free_extent_map(em);
4143 }
Chris Mason0b86a832008-03-24 15:01:56 -04004144
David Sterba172ddd62011-04-21 00:48:27 +02004145 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004146 if (!em)
4147 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004148 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4149 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004150 if (!map) {
4151 free_extent_map(em);
4152 return -ENOMEM;
4153 }
4154
4155 em->bdev = (struct block_device *)map;
4156 em->start = logical;
4157 em->len = length;
4158 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004159 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004160
Chris Mason593060d2008-03-25 16:50:33 -04004161 map->num_stripes = num_stripes;
4162 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4163 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4164 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4165 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4166 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004167 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004168 for (i = 0; i < num_stripes; i++) {
4169 map->stripes[i].physical =
4170 btrfs_stripe_offset_nr(leaf, chunk, i);
4171 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004172 read_extent_buffer(leaf, uuid, (unsigned long)
4173 btrfs_stripe_dev_uuid_nr(chunk, i),
4174 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004175 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4176 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004177 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004178 kfree(map);
4179 free_extent_map(em);
4180 return -EIO;
4181 }
Chris Masondfe25022008-05-13 13:46:40 -04004182 if (!map->stripes[i].dev) {
4183 map->stripes[i].dev =
4184 add_missing_dev(root, devid, uuid);
4185 if (!map->stripes[i].dev) {
4186 kfree(map);
4187 free_extent_map(em);
4188 return -EIO;
4189 }
4190 }
4191 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004192 }
4193
Chris Mason890871b2009-09-02 16:24:52 -04004194 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004195 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004196 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04004197 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04004198 free_extent_map(em);
4199
4200 return 0;
4201}
4202
4203static int fill_device_from_item(struct extent_buffer *leaf,
4204 struct btrfs_dev_item *dev_item,
4205 struct btrfs_device *device)
4206{
4207 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004208
4209 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004210 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4211 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004212 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4213 device->type = btrfs_device_type(leaf, dev_item);
4214 device->io_align = btrfs_device_io_align(leaf, dev_item);
4215 device->io_width = btrfs_device_io_width(leaf, dev_item);
4216 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004217
4218 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004219 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004220
Chris Mason0b86a832008-03-24 15:01:56 -04004221 return 0;
4222}
4223
Yan Zheng2b820322008-11-17 21:11:30 -05004224static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4225{
4226 struct btrfs_fs_devices *fs_devices;
4227 int ret;
4228
Li Zefanb367e472011-12-07 11:38:24 +08004229 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004230
4231 fs_devices = root->fs_info->fs_devices->seed;
4232 while (fs_devices) {
4233 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4234 ret = 0;
4235 goto out;
4236 }
4237 fs_devices = fs_devices->seed;
4238 }
4239
4240 fs_devices = find_fsid(fsid);
4241 if (!fs_devices) {
4242 ret = -ENOENT;
4243 goto out;
4244 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004245
4246 fs_devices = clone_fs_devices(fs_devices);
4247 if (IS_ERR(fs_devices)) {
4248 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004249 goto out;
4250 }
4251
Christoph Hellwig97288f22008-12-02 06:36:09 -05004252 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004253 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004254 if (ret)
4255 goto out;
4256
4257 if (!fs_devices->seeding) {
4258 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004259 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004260 ret = -EINVAL;
4261 goto out;
4262 }
4263
4264 fs_devices->seed = root->fs_info->fs_devices->seed;
4265 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004266out:
Yan Zheng2b820322008-11-17 21:11:30 -05004267 return ret;
4268}
4269
Chris Mason0d81ba52008-03-24 15:02:07 -04004270static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004271 struct extent_buffer *leaf,
4272 struct btrfs_dev_item *dev_item)
4273{
4274 struct btrfs_device *device;
4275 u64 devid;
4276 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004277 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004278 u8 dev_uuid[BTRFS_UUID_SIZE];
4279
Chris Mason0b86a832008-03-24 15:01:56 -04004280 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004281 read_extent_buffer(leaf, dev_uuid,
4282 (unsigned long)btrfs_device_uuid(dev_item),
4283 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004284 read_extent_buffer(leaf, fs_uuid,
4285 (unsigned long)btrfs_device_fsid(dev_item),
4286 BTRFS_UUID_SIZE);
4287
4288 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4289 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004290 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004291 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004292 }
4293
4294 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4295 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004296 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004297 return -EIO;
4298
4299 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004300 printk(KERN_WARNING "warning devid %llu missing\n",
4301 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004302 device = add_missing_dev(root, devid, dev_uuid);
4303 if (!device)
4304 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004305 } else if (!device->missing) {
4306 /*
4307 * this happens when a device that was properly setup
4308 * in the device info lists suddenly goes bad.
4309 * device->bdev is NULL, and so we have to set
4310 * device->missing to one here
4311 */
4312 root->fs_info->fs_devices->missing_devices++;
4313 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004314 }
4315 }
4316
4317 if (device->fs_devices != root->fs_info->fs_devices) {
4318 BUG_ON(device->writeable);
4319 if (device->generation !=
4320 btrfs_device_generation(leaf, dev_item))
4321 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004322 }
Chris Mason0b86a832008-03-24 15:01:56 -04004323
4324 fill_device_from_item(leaf, dev_item, device);
4325 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004326 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004327 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004328 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004329 spin_lock(&root->fs_info->free_chunk_lock);
4330 root->fs_info->free_chunk_space += device->total_bytes -
4331 device->bytes_used;
4332 spin_unlock(&root->fs_info->free_chunk_lock);
4333 }
Chris Mason0b86a832008-03-24 15:01:56 -04004334 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004335 return ret;
4336}
4337
Yan Zhenge4404d62008-12-12 10:03:26 -05004338int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004339{
David Sterba6c417612011-04-13 15:41:04 +02004340 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004341 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004342 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004343 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004344 u8 *ptr;
4345 unsigned long sb_ptr;
4346 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004347 u32 num_stripes;
4348 u32 array_size;
4349 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004350 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004351 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004352
Yan Zhenge4404d62008-12-12 10:03:26 -05004353 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004354 BTRFS_SUPER_INFO_SIZE);
4355 if (!sb)
4356 return -ENOMEM;
4357 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004358 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05004359
Chris Masona061fc82008-05-07 11:43:44 -04004360 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004361 array_size = btrfs_super_sys_array_size(super_copy);
4362
Chris Mason0b86a832008-03-24 15:01:56 -04004363 ptr = super_copy->sys_chunk_array;
4364 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4365 cur = 0;
4366
4367 while (cur < array_size) {
4368 disk_key = (struct btrfs_disk_key *)ptr;
4369 btrfs_disk_key_to_cpu(&key, disk_key);
4370
Chris Masona061fc82008-05-07 11:43:44 -04004371 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004372 sb_ptr += len;
4373 cur += len;
4374
Chris Mason0d81ba52008-03-24 15:02:07 -04004375 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004376 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004377 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004378 if (ret)
4379 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004380 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4381 len = btrfs_chunk_item_size(num_stripes);
4382 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004383 ret = -EIO;
4384 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004385 }
4386 ptr += len;
4387 sb_ptr += len;
4388 cur += len;
4389 }
Chris Masona061fc82008-05-07 11:43:44 -04004390 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004391 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004392}
4393
4394int btrfs_read_chunk_tree(struct btrfs_root *root)
4395{
4396 struct btrfs_path *path;
4397 struct extent_buffer *leaf;
4398 struct btrfs_key key;
4399 struct btrfs_key found_key;
4400 int ret;
4401 int slot;
4402
4403 root = root->fs_info->chunk_root;
4404
4405 path = btrfs_alloc_path();
4406 if (!path)
4407 return -ENOMEM;
4408
Li Zefanb367e472011-12-07 11:38:24 +08004409 mutex_lock(&uuid_mutex);
4410 lock_chunks(root);
4411
Chris Mason0b86a832008-03-24 15:01:56 -04004412 /* first we search for all of the device items, and then we
4413 * read in all of the chunk items. This way we can create chunk
4414 * mappings that reference all of the devices that are afound
4415 */
4416 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4417 key.offset = 0;
4418 key.type = 0;
4419again:
4420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004421 if (ret < 0)
4422 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004423 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004424 leaf = path->nodes[0];
4425 slot = path->slots[0];
4426 if (slot >= btrfs_header_nritems(leaf)) {
4427 ret = btrfs_next_leaf(root, path);
4428 if (ret == 0)
4429 continue;
4430 if (ret < 0)
4431 goto error;
4432 break;
4433 }
4434 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4435 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4436 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4437 break;
4438 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4439 struct btrfs_dev_item *dev_item;
4440 dev_item = btrfs_item_ptr(leaf, slot,
4441 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004442 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004443 if (ret)
4444 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004445 }
4446 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4447 struct btrfs_chunk *chunk;
4448 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4449 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004450 if (ret)
4451 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004452 }
4453 path->slots[0]++;
4454 }
4455 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4456 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004457 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004458 goto again;
4459 }
Chris Mason0b86a832008-03-24 15:01:56 -04004460 ret = 0;
4461error:
Li Zefanb367e472011-12-07 11:38:24 +08004462 unlock_chunks(root);
4463 mutex_unlock(&uuid_mutex);
4464
Yan Zheng2b820322008-11-17 21:11:30 -05004465 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004466 return ret;
4467}