blob: c4ea7d8bea0fa447f513e557ec43598d44856e2f [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
Jeff Mahoney143bede2012-03-01 14:56:26 +010070void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040071{
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 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040080}
81
Chris Masona1b32a52008-09-05 16:09:51 -040082static noinline struct btrfs_device *__find_device(struct list_head *head,
83 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040084{
85 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040086
Qinghuang Fengc6e30872009-01-21 10:59:08 -050087 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040088 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040089 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 return dev;
Chris Masona4437552008-04-18 10:29:38 -040091 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040092 }
93 return NULL;
94}
95
Chris Masona1b32a52008-09-05 16:09:51 -040096static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040097{
Chris Mason8a4b83c2008-03-24 15:02:07 -040098 struct btrfs_fs_devices *fs_devices;
99
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500100 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
102 return fs_devices;
103 }
104 return NULL;
105}
106
Chris Masonffbd5172009-04-20 15:50:09 -0400107static void requeue_list(struct btrfs_pending_bios *pending_bios,
108 struct bio *head, struct bio *tail)
109{
110
111 struct bio *old_head;
112
113 old_head = pending_bios->head;
114 pending_bios->head = head;
115 if (pending_bios->tail)
116 tail->bi_next = old_head;
117 else
118 pending_bios->tail = tail;
119}
120
Chris Mason8b712842008-06-11 16:50:36 -0400121/*
122 * we try to collect pending bios for a device so we don't get a large
123 * number of procs sending bios down to the same device. This greatly
124 * improves the schedulers ability to collect and merge the bios.
125 *
126 * But, it also turns into a long list of bios to process and that is sure
127 * to eventually make the worker thread block. The solution here is to
128 * make some progress and then put this work struct back at the end of
129 * the list if the block device is congested. This way, multiple devices
130 * can make progress from a single worker thread.
131 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100132static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400133{
134 struct bio *pending;
135 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400136 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400137 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400138 struct bio *tail;
139 struct bio *cur;
140 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400141 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400142 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400143 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400144 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400145 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000146 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400147 struct blk_plug plug;
148
149 /*
150 * this function runs all the bios we've collected for
151 * a particular device. We don't want to wander off to
152 * another device without first sending all of these down.
153 * So, setup a plug here and finish it off before we return
154 */
155 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400156
Chris Masonbedf7622009-04-03 10:32:58 -0400157 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400158 fs_info = device->dev_root->fs_info;
159 limit = btrfs_async_submit_limit(fs_info);
160 limit = limit * 2 / 3;
161
Chris Mason8b712842008-06-11 16:50:36 -0400162loop:
163 spin_lock(&device->io_lock);
164
Chris Masona6837052009-02-04 09:19:41 -0500165loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400166 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400167
Chris Mason8b712842008-06-11 16:50:36 -0400168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
172 */
Chris Masond84275c2009-06-09 15:39:08 -0400173 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400174 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400175 force_reg = 1;
176 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400177 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400178 force_reg = 0;
179 }
Chris Masonffbd5172009-04-20 15:50:09 -0400180
181 pending = pending_bios->head;
182 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400183 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400184
185 /*
186 * if pending was null this time around, no bios need processing
187 * at all and we can stop. Otherwise it'll loop back up again
188 * and do an additional check so no bios are missed.
189 *
190 * device->running_pending is used to synchronize with the
191 * schedule_bio code.
192 */
Chris Masonffbd5172009-04-20 15:50:09 -0400193 if (device->pending_sync_bios.head == NULL &&
194 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400195 again = 0;
196 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400197 } else {
198 again = 1;
199 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400200 }
Chris Masonffbd5172009-04-20 15:50:09 -0400201
202 pending_bios->head = NULL;
203 pending_bios->tail = NULL;
204
Chris Mason8b712842008-06-11 16:50:36 -0400205 spin_unlock(&device->io_lock);
206
Chris Masond3977122009-01-05 21:25:51 -0500207 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400208
209 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400210 /* we want to work on both lists, but do more bios on the
211 * sync list than the regular list
212 */
213 if ((num_run > 32 &&
214 pending_bios != &device->pending_sync_bios &&
215 device->pending_sync_bios.head) ||
216 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
217 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400218 spin_lock(&device->io_lock);
219 requeue_list(pending_bios, pending, tail);
220 goto loop_lock;
221 }
222
Chris Mason8b712842008-06-11 16:50:36 -0400223 cur = pending;
224 pending = pending->bi_next;
225 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400226 atomic_dec(&fs_info->nr_async_bios);
227
228 if (atomic_read(&fs_info->nr_async_bios) < limit &&
229 waitqueue_active(&fs_info->async_submit_wait))
230 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400231
232 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400233
Chris Mason2ab1ba62011-08-04 14:28:36 -0400234 /*
235 * if we're doing the sync list, record that our
236 * plug has some sync requests on it
237 *
238 * If we're doing the regular list and there are
239 * sync requests sitting around, unplug before
240 * we add more
241 */
242 if (pending_bios == &device->pending_sync_bios) {
243 sync_pending = 1;
244 } else if (sync_pending) {
245 blk_finish_plug(&plug);
246 blk_start_plug(&plug);
247 sync_pending = 0;
248 }
249
Stefan Behrens21adbd52011-11-09 13:44:05 +0100250 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400251 num_run++;
252 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100253 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400254 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400255
256 /*
257 * we made progress, there is more work to do and the bdi
258 * is now congested. Back off and let other work structs
259 * run instead
260 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400261 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500262 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400263 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400264
Chris Masonb765ead2009-04-03 10:27:10 -0400265 ioc = current->io_context;
266
267 /*
268 * the main goal here is that we don't want to
269 * block if we're going to be able to submit
270 * more requests without blocking.
271 *
272 * This code does two great things, it pokes into
273 * the elevator code from a filesystem _and_
274 * it makes assumptions about how batching works.
275 */
276 if (ioc && ioc->nr_batch_requests > 0 &&
277 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
278 (last_waited == 0 ||
279 ioc->last_waited == last_waited)) {
280 /*
281 * we want to go through our batch of
282 * requests and stop. So, we copy out
283 * the ioc->last_waited time and test
284 * against it before looping
285 */
286 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100287 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400288 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400289 continue;
290 }
Chris Mason8b712842008-06-11 16:50:36 -0400291 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400292 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500293 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400294
295 spin_unlock(&device->io_lock);
296 btrfs_requeue_work(&device->work);
297 goto done;
298 }
Chris Masond85c8a62011-12-15 15:38:41 -0500299 /* unplug every 64 requests just for good measure */
300 if (batch_run % 64 == 0) {
301 blk_finish_plug(&plug);
302 blk_start_plug(&plug);
303 sync_pending = 0;
304 }
Chris Mason8b712842008-06-11 16:50:36 -0400305 }
Chris Masonffbd5172009-04-20 15:50:09 -0400306
Chris Mason51684082010-03-10 15:33:32 -0500307 cond_resched();
308 if (again)
309 goto loop;
310
311 spin_lock(&device->io_lock);
312 if (device->pending_bios.head || device->pending_sync_bios.head)
313 goto loop_lock;
314 spin_unlock(&device->io_lock);
315
Chris Mason8b712842008-06-11 16:50:36 -0400316done:
Chris Mason211588a2011-04-19 20:12:40 -0400317 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400318}
319
Christoph Hellwigb2950862008-12-02 09:54:17 -0500320static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400321{
322 struct btrfs_device *device;
323
324 device = container_of(work, struct btrfs_device, work);
325 run_scheduled_bios(device);
326}
327
Chris Masona1b32a52008-09-05 16:09:51 -0400328static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400329 struct btrfs_super_block *disk_super,
330 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
331{
332 struct btrfs_device *device;
333 struct btrfs_fs_devices *fs_devices;
334 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000335 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400336
337 fs_devices = find_fsid(disk_super->fsid);
338 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400339 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400340 if (!fs_devices)
341 return -ENOMEM;
342 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400343 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344 list_add(&fs_devices->list, &fs_uuids);
345 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
346 fs_devices->latest_devid = devid;
347 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400348 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400349 device = NULL;
350 } else {
Chris Masona4437552008-04-18 10:29:38 -0400351 device = __find_device(&fs_devices->devices, devid,
352 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400353 }
354 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500355 if (fs_devices->opened)
356 return -EBUSY;
357
Chris Mason8a4b83c2008-03-24 15:02:07 -0400358 device = kzalloc(sizeof(*device), GFP_NOFS);
359 if (!device) {
360 /* we can safely leave the fs_devices entry around */
361 return -ENOMEM;
362 }
363 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400364 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400365 memcpy(device->uuid, disk_super->dev_item.uuid,
366 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400367 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 device->name = kstrdup(path, GFP_NOFS);
369 if (!device->name) {
370 kfree(device);
371 return -ENOMEM;
372 }
Yan Zheng2b820322008-11-17 21:11:30 -0500373 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400374
Arne Jansen90519d62011-05-23 14:30:00 +0200375 /* init readahead state */
376 spin_lock_init(&device->reada_lock);
377 device->reada_curr_zone = NULL;
378 atomic_set(&device->reada_in_flight, 0);
379 device->reada_next = 0;
380 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
381 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
382
Chris Masone5e9a522009-06-10 15:17:02 -0400383 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000384 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400385 mutex_unlock(&fs_devices->device_list_mutex);
386
Yan Zheng2b820322008-11-17 21:11:30 -0500387 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500389 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000390 name = kstrdup(path, GFP_NOFS);
391 if (!name)
392 return -ENOMEM;
393 kfree(device->name);
394 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500395 if (device->missing) {
396 fs_devices->missing_devices--;
397 device->missing = 0;
398 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 }
400
401 if (found_transid > fs_devices->latest_trans) {
402 fs_devices->latest_devid = devid;
403 fs_devices->latest_trans = found_transid;
404 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405 *fs_devices_ret = fs_devices;
406 return 0;
407}
408
Yan Zhenge4404d62008-12-12 10:03:26 -0500409static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
410{
411 struct btrfs_fs_devices *fs_devices;
412 struct btrfs_device *device;
413 struct btrfs_device *orig_dev;
414
415 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
416 if (!fs_devices)
417 return ERR_PTR(-ENOMEM);
418
419 INIT_LIST_HEAD(&fs_devices->devices);
420 INIT_LIST_HEAD(&fs_devices->alloc_list);
421 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400422 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500423 fs_devices->latest_devid = orig->latest_devid;
424 fs_devices->latest_trans = orig->latest_trans;
425 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
426
Xiao Guangrong46224702011-04-20 10:08:47 +0000427 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500428 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
429 device = kzalloc(sizeof(*device), GFP_NOFS);
430 if (!device)
431 goto error;
432
433 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400434 if (!device->name) {
435 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400437 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500438
439 device->devid = orig_dev->devid;
440 device->work.func = pending_bios_fn;
441 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500442 spin_lock_init(&device->io_lock);
443 INIT_LIST_HEAD(&device->dev_list);
444 INIT_LIST_HEAD(&device->dev_alloc_list);
445
446 list_add(&device->dev_list, &fs_devices->devices);
447 device->fs_devices = fs_devices;
448 fs_devices->num_devices++;
449 }
450 return fs_devices;
451error:
452 free_fs_devices(fs_devices);
453 return ERR_PTR(-ENOMEM);
454}
455
Jeff Mahoney143bede2012-03-01 14:56:26 +0100456void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400457{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500458 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400459
Chris Masona6b0d5c2012-02-20 20:53:43 -0500460 struct block_device *latest_bdev = NULL;
461 u64 latest_devid = 0;
462 u64 latest_transid = 0;
463
Chris Masondfe25022008-05-13 13:46:40 -0400464 mutex_lock(&uuid_mutex);
465again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000466 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500467 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500468 if (device->in_fs_metadata) {
469 if (!latest_transid ||
470 device->generation > latest_transid) {
471 latest_devid = device->devid;
472 latest_transid = device->generation;
473 latest_bdev = device->bdev;
474 }
Yan Zheng2b820322008-11-17 21:11:30 -0500475 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500476 }
Yan Zheng2b820322008-11-17 21:11:30 -0500477
478 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100479 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500480 device->bdev = NULL;
481 fs_devices->open_devices--;
482 }
483 if (device->writeable) {
484 list_del_init(&device->dev_alloc_list);
485 device->writeable = 0;
486 fs_devices->rw_devices--;
487 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500488 list_del_init(&device->dev_list);
489 fs_devices->num_devices--;
490 kfree(device->name);
491 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400492 }
Yan Zheng2b820322008-11-17 21:11:30 -0500493
494 if (fs_devices->seed) {
495 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500496 goto again;
497 }
498
Chris Masona6b0d5c2012-02-20 20:53:43 -0500499 fs_devices->latest_bdev = latest_bdev;
500 fs_devices->latest_devid = latest_devid;
501 fs_devices->latest_trans = latest_transid;
502
Chris Masondfe25022008-05-13 13:46:40 -0400503 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400504}
Chris Masona0af4692008-05-13 16:03:06 -0400505
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506static void __free_device(struct work_struct *work)
507{
508 struct btrfs_device *device;
509
510 device = container_of(work, struct btrfs_device, rcu_work);
511
512 if (device->bdev)
513 blkdev_put(device->bdev, device->mode);
514
515 kfree(device->name);
516 kfree(device);
517}
518
519static void free_device(struct rcu_head *head)
520{
521 struct btrfs_device *device;
522
523 device = container_of(head, struct btrfs_device, rcu);
524
525 INIT_WORK(&device->rcu_work, __free_device);
526 schedule_work(&device->rcu_work);
527}
528
Yan Zheng2b820322008-11-17 21:11:30 -0500529static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400530{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500532
Yan Zheng2b820322008-11-17 21:11:30 -0500533 if (--fs_devices->opened > 0)
534 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000536 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500537 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000538 struct btrfs_device *new_device;
539
540 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400541 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000542
Yan Zheng2b820322008-11-17 21:11:30 -0500543 if (device->writeable) {
544 list_del_init(&device->dev_alloc_list);
545 fs_devices->rw_devices--;
546 }
547
Josef Bacikd5e20032011-08-04 14:52:27 +0000548 if (device->can_discard)
549 fs_devices->num_can_discard--;
550
Xiao Guangrong1f781602011-04-20 10:09:16 +0000551 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
552 BUG_ON(!new_device);
553 memcpy(new_device, device, sizeof(*new_device));
554 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000555 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000556 new_device->bdev = NULL;
557 new_device->writeable = 0;
558 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000559 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000560 list_replace_rcu(&device->dev_list, &new_device->dev_list);
561
562 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400563 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000564 mutex_unlock(&fs_devices->device_list_mutex);
565
Yan Zhenge4404d62008-12-12 10:03:26 -0500566 WARN_ON(fs_devices->open_devices);
567 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500568 fs_devices->opened = 0;
569 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500570
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571 return 0;
572}
573
Yan Zheng2b820322008-11-17 21:11:30 -0500574int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
575{
Yan Zhenge4404d62008-12-12 10:03:26 -0500576 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500577 int ret;
578
579 mutex_lock(&uuid_mutex);
580 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500581 if (!fs_devices->opened) {
582 seed_devices = fs_devices->seed;
583 fs_devices->seed = NULL;
584 }
Yan Zheng2b820322008-11-17 21:11:30 -0500585 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500586
587 while (seed_devices) {
588 fs_devices = seed_devices;
589 seed_devices = fs_devices->seed;
590 __btrfs_close_devices(fs_devices);
591 free_fs_devices(fs_devices);
592 }
Yan Zheng2b820322008-11-17 21:11:30 -0500593 return ret;
594}
595
Yan Zhenge4404d62008-12-12 10:03:26 -0500596static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
597 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400598{
Josef Bacikd5e20032011-08-04 14:52:27 +0000599 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400600 struct block_device *bdev;
601 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400602 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400603 struct block_device *latest_bdev = NULL;
604 struct buffer_head *bh;
605 struct btrfs_super_block *disk_super;
606 u64 latest_devid = 0;
607 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400608 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500609 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400610 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400611
Tejun Heod4d77622010-11-13 11:55:18 +0100612 flags |= FMODE_EXCL;
613
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500614 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400615 if (device->bdev)
616 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400617 if (!device->name)
618 continue;
619
Tejun Heod4d77622010-11-13 11:55:18 +0100620 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500622 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400623 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 }
Chris Masona061fc82008-05-07 11:43:44 -0400625 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400626
Yan Zhenga512bbf2008-12-08 16:46:26 -0500627 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300628 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400629 goto error_close;
630
631 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000632 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400633 if (devid != device->devid)
634 goto error_brelse;
635
Yan Zheng2b820322008-11-17 21:11:30 -0500636 if (memcmp(device->uuid, disk_super->dev_item.uuid,
637 BTRFS_UUID_SIZE))
638 goto error_brelse;
639
640 device->generation = btrfs_super_generation(disk_super);
641 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400642 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500643 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400644 latest_bdev = bdev;
645 }
646
Yan Zheng2b820322008-11-17 21:11:30 -0500647 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
648 device->writeable = 0;
649 } else {
650 device->writeable = !bdev_read_only(bdev);
651 seeding = 0;
652 }
653
Josef Bacikd5e20032011-08-04 14:52:27 +0000654 q = bdev_get_queue(bdev);
655 if (blk_queue_discard(q)) {
656 device->can_discard = 1;
657 fs_devices->num_can_discard++;
658 }
659
Chris Mason8a4b83c2008-03-24 15:02:07 -0400660 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400661 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500662 device->mode = flags;
663
Chris Masonc2898112009-06-10 09:51:32 -0400664 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
665 fs_devices->rotating = 1;
666
Chris Masona0af4692008-05-13 16:03:06 -0400667 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500668 if (device->writeable) {
669 fs_devices->rw_devices++;
670 list_add(&device->dev_alloc_list,
671 &fs_devices->alloc_list);
672 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000673 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400674 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400675
Chris Masona0af4692008-05-13 16:03:06 -0400676error_brelse:
677 brelse(bh);
678error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100679 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400680error:
681 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400682 }
Chris Masona0af4692008-05-13 16:03:06 -0400683 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300684 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400685 goto out;
686 }
Yan Zheng2b820322008-11-17 21:11:30 -0500687 fs_devices->seeding = seeding;
688 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400689 fs_devices->latest_bdev = latest_bdev;
690 fs_devices->latest_devid = latest_devid;
691 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500692 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400693out:
Yan Zheng2b820322008-11-17 21:11:30 -0500694 return ret;
695}
696
697int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500698 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500699{
700 int ret;
701
702 mutex_lock(&uuid_mutex);
703 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500704 fs_devices->opened++;
705 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500706 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500707 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500708 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400710 return ret;
711}
712
Christoph Hellwig97288f22008-12-02 06:36:09 -0500713int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400714 struct btrfs_fs_devices **fs_devices_ret)
715{
716 struct btrfs_super_block *disk_super;
717 struct block_device *bdev;
718 struct buffer_head *bh;
719 int ret;
720 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400721 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400722
Tejun Heod4d77622010-11-13 11:55:18 +0100723 flags |= FMODE_EXCL;
724 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400725
726 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400727 ret = PTR_ERR(bdev);
728 goto error;
729 }
730
Al Viro10f63272011-11-17 15:05:22 -0500731 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 ret = set_blocksize(bdev, 4096);
733 if (ret)
734 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500735 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000737 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400738 goto error_close;
739 }
740 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000741 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400742 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400743 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500744 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200745 else
746 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500747 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500748 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400749 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
750
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 brelse(bh);
752error_close:
Al Viro10f63272011-11-17 15:05:22 -0500753 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100754 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400755error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756 return ret;
757}
Chris Mason0b86a832008-03-24 15:01:56 -0400758
Miao Xie6d07bce2011-01-05 10:07:31 +0000759/* helper to account the used device space in the range */
760int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
761 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400762{
763 struct btrfs_key key;
764 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000765 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500766 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000767 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400768 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000769 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400770 struct extent_buffer *l;
771
Miao Xie6d07bce2011-01-05 10:07:31 +0000772 *length = 0;
773
774 if (start >= device->total_bytes)
775 return 0;
776
Yan Zheng2b820322008-11-17 21:11:30 -0500777 path = btrfs_alloc_path();
778 if (!path)
779 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400780 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400781
Chris Mason0b86a832008-03-24 15:01:56 -0400782 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000783 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400784 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000785
786 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400787 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000788 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400789 if (ret > 0) {
790 ret = btrfs_previous_item(root, path, key.objectid, key.type);
791 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000792 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400793 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000794
Chris Mason0b86a832008-03-24 15:01:56 -0400795 while (1) {
796 l = path->nodes[0];
797 slot = path->slots[0];
798 if (slot >= btrfs_header_nritems(l)) {
799 ret = btrfs_next_leaf(root, path);
800 if (ret == 0)
801 continue;
802 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000803 goto out;
804
805 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400806 }
807 btrfs_item_key_to_cpu(l, &key, slot);
808
809 if (key.objectid < device->devid)
810 goto next;
811
812 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000813 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400814
Chris Masond3977122009-01-05 21:25:51 -0500815 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400816 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400817
Chris Mason0b86a832008-03-24 15:01:56 -0400818 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000819 extent_end = key.offset + btrfs_dev_extent_length(l,
820 dev_extent);
821 if (key.offset <= start && extent_end > end) {
822 *length = end - start + 1;
823 break;
824 } else if (key.offset <= start && extent_end > start)
825 *length += extent_end - start;
826 else if (key.offset > start && extent_end <= end)
827 *length += extent_end - key.offset;
828 else if (key.offset > start && key.offset <= end) {
829 *length += end - key.offset + 1;
830 break;
831 } else if (key.offset > end)
832 break;
833
834next:
835 path->slots[0]++;
836 }
837 ret = 0;
838out:
839 btrfs_free_path(path);
840 return ret;
841}
842
Chris Mason0b86a832008-03-24 15:01:56 -0400843/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000844 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000845 * @device: the device which we search the free space in
846 * @num_bytes: the size of the free space that we need
847 * @start: store the start of the free space.
848 * @len: the size of the free space. that we find, or the size of the max
849 * free space if we don't find suitable free space
850 *
Chris Mason0b86a832008-03-24 15:01:56 -0400851 * this uses a pretty simple search, the expectation is that it is
852 * called very infrequently and that a given device has a small number
853 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 *
855 * @start is used to store the start of the free space if we find. But if we
856 * don't find suitable free space, it will be used to store the start position
857 * of the max free space.
858 *
859 * @len is used to store the size of the free space that we find.
860 * But if we don't find suitable free space, it is used to store the size of
861 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400862 */
Li Zefan125ccb02011-12-08 15:07:24 +0800863int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000864 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400865{
866 struct btrfs_key key;
867 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000868 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400869 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000870 u64 hole_size;
871 u64 max_hole_start;
872 u64 max_hole_size;
873 u64 extent_end;
874 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400875 u64 search_end = device->total_bytes;
876 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000877 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400878 struct extent_buffer *l;
879
Chris Mason0b86a832008-03-24 15:01:56 -0400880 /* FIXME use last free of some kind */
881
882 /* we don't want to overwrite the superblock on the drive,
883 * so we make sure to start at an offset of at least 1MB
884 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200885 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400886
Miao Xie7bfc8372011-01-05 10:07:26 +0000887 max_hole_start = search_start;
888 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000889 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000890
891 if (search_start >= search_end) {
892 ret = -ENOSPC;
893 goto error;
894 }
895
896 path = btrfs_alloc_path();
897 if (!path) {
898 ret = -ENOMEM;
899 goto error;
900 }
901 path->reada = 2;
902
Chris Mason0b86a832008-03-24 15:01:56 -0400903 key.objectid = device->devid;
904 key.offset = search_start;
905 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000906
Li Zefan125ccb02011-12-08 15:07:24 +0800907 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400908 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000909 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400910 if (ret > 0) {
911 ret = btrfs_previous_item(root, path, key.objectid, key.type);
912 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000913 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400914 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000915
Chris Mason0b86a832008-03-24 15:01:56 -0400916 while (1) {
917 l = path->nodes[0];
918 slot = path->slots[0];
919 if (slot >= btrfs_header_nritems(l)) {
920 ret = btrfs_next_leaf(root, path);
921 if (ret == 0)
922 continue;
923 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000924 goto out;
925
926 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400927 }
928 btrfs_item_key_to_cpu(l, &key, slot);
929
930 if (key.objectid < device->devid)
931 goto next;
932
933 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000934 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400935
Chris Mason0b86a832008-03-24 15:01:56 -0400936 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
937 goto next;
938
Miao Xie7bfc8372011-01-05 10:07:26 +0000939 if (key.offset > search_start) {
940 hole_size = key.offset - search_start;
941
942 if (hole_size > max_hole_size) {
943 max_hole_start = search_start;
944 max_hole_size = hole_size;
945 }
946
947 /*
948 * If this free space is greater than which we need,
949 * it must be the max free space that we have found
950 * until now, so max_hole_start must point to the start
951 * of this free space and the length of this free space
952 * is stored in max_hole_size. Thus, we return
953 * max_hole_start and max_hole_size and go back to the
954 * caller.
955 */
956 if (hole_size >= num_bytes) {
957 ret = 0;
958 goto out;
959 }
960 }
961
Chris Mason0b86a832008-03-24 15:01:56 -0400962 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000963 extent_end = key.offset + btrfs_dev_extent_length(l,
964 dev_extent);
965 if (extent_end > search_start)
966 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400967next:
968 path->slots[0]++;
969 cond_resched();
970 }
Chris Mason0b86a832008-03-24 15:01:56 -0400971
liubo38c01b92011-08-02 02:39:03 +0000972 /*
973 * At this point, search_start should be the end of
974 * allocated dev extents, and when shrinking the device,
975 * search_end may be smaller than search_start.
976 */
977 if (search_end > search_start)
978 hole_size = search_end - search_start;
979
Miao Xie7bfc8372011-01-05 10:07:26 +0000980 if (hole_size > max_hole_size) {
981 max_hole_start = search_start;
982 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400983 }
Chris Mason0b86a832008-03-24 15:01:56 -0400984
Miao Xie7bfc8372011-01-05 10:07:26 +0000985 /* See above. */
986 if (hole_size < num_bytes)
987 ret = -ENOSPC;
988 else
989 ret = 0;
990
991out:
Yan Zheng2b820322008-11-17 21:11:30 -0500992 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000993error:
994 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000995 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000996 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400997 return ret;
998}
999
Christoph Hellwigb2950862008-12-02 09:54:17 -05001000static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001001 struct btrfs_device *device,
1002 u64 start)
1003{
1004 int ret;
1005 struct btrfs_path *path;
1006 struct btrfs_root *root = device->dev_root;
1007 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001008 struct btrfs_key found_key;
1009 struct extent_buffer *leaf = NULL;
1010 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001011
1012 path = btrfs_alloc_path();
1013 if (!path)
1014 return -ENOMEM;
1015
1016 key.objectid = device->devid;
1017 key.offset = start;
1018 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001019again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001020 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001021 if (ret > 0) {
1022 ret = btrfs_previous_item(root, path, key.objectid,
1023 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001024 if (ret)
1025 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001026 leaf = path->nodes[0];
1027 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1028 extent = btrfs_item_ptr(leaf, path->slots[0],
1029 struct btrfs_dev_extent);
1030 BUG_ON(found_key.offset > start || found_key.offset +
1031 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001032 key = found_key;
1033 btrfs_release_path(path);
1034 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001035 } else if (ret == 0) {
1036 leaf = path->nodes[0];
1037 extent = btrfs_item_ptr(leaf, path->slots[0],
1038 struct btrfs_dev_extent);
1039 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001040 BUG_ON(ret);
1041
Josef Bacik2bf64752011-09-26 17:12:22 -04001042 if (device->bytes_used > 0) {
1043 u64 len = btrfs_dev_extent_length(leaf, extent);
1044 device->bytes_used -= len;
1045 spin_lock(&root->fs_info->free_chunk_lock);
1046 root->fs_info->free_chunk_space += len;
1047 spin_unlock(&root->fs_info->free_chunk_lock);
1048 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001049 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001050
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001051out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001052 btrfs_free_path(path);
1053 return ret;
1054}
1055
Yan Zheng2b820322008-11-17 21:11:30 -05001056int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001057 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001058 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001059 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001060{
1061 int ret;
1062 struct btrfs_path *path;
1063 struct btrfs_root *root = device->dev_root;
1064 struct btrfs_dev_extent *extent;
1065 struct extent_buffer *leaf;
1066 struct btrfs_key key;
1067
Chris Masondfe25022008-05-13 13:46:40 -04001068 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001069 path = btrfs_alloc_path();
1070 if (!path)
1071 return -ENOMEM;
1072
Chris Mason0b86a832008-03-24 15:01:56 -04001073 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001074 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001075 key.type = BTRFS_DEV_EXTENT_KEY;
1076 ret = btrfs_insert_empty_item(trans, root, path, &key,
1077 sizeof(*extent));
1078 BUG_ON(ret);
1079
1080 leaf = path->nodes[0];
1081 extent = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001083 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1084 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1085 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1086
1087 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1088 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1089 BTRFS_UUID_SIZE);
1090
Chris Mason0b86a832008-03-24 15:01:56 -04001091 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1092 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001093 btrfs_free_path(path);
1094 return ret;
1095}
1096
Chris Masona1b32a52008-09-05 16:09:51 -04001097static noinline int find_next_chunk(struct btrfs_root *root,
1098 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001099{
1100 struct btrfs_path *path;
1101 int ret;
1102 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001103 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001104 struct btrfs_key found_key;
1105
1106 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001107 if (!path)
1108 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001109
Chris Masone17cade2008-04-15 15:41:47 -04001110 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001111 key.offset = (u64)-1;
1112 key.type = BTRFS_CHUNK_ITEM_KEY;
1113
1114 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1115 if (ret < 0)
1116 goto error;
1117
1118 BUG_ON(ret == 0);
1119
1120 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1121 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001122 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001123 } else {
1124 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1125 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001126 if (found_key.objectid != objectid)
1127 *offset = 0;
1128 else {
1129 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1130 struct btrfs_chunk);
1131 *offset = found_key.offset +
1132 btrfs_chunk_length(path->nodes[0], chunk);
1133 }
Chris Mason0b86a832008-03-24 15:01:56 -04001134 }
1135 ret = 0;
1136error:
1137 btrfs_free_path(path);
1138 return ret;
1139}
1140
Yan Zheng2b820322008-11-17 21:11:30 -05001141static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001142{
1143 int ret;
1144 struct btrfs_key key;
1145 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001146 struct btrfs_path *path;
1147
1148 root = root->fs_info->chunk_root;
1149
1150 path = btrfs_alloc_path();
1151 if (!path)
1152 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001153
1154 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1155 key.type = BTRFS_DEV_ITEM_KEY;
1156 key.offset = (u64)-1;
1157
1158 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1159 if (ret < 0)
1160 goto error;
1161
1162 BUG_ON(ret == 0);
1163
1164 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1165 BTRFS_DEV_ITEM_KEY);
1166 if (ret) {
1167 *objectid = 1;
1168 } else {
1169 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1170 path->slots[0]);
1171 *objectid = found_key.offset + 1;
1172 }
1173 ret = 0;
1174error:
Yan Zheng2b820322008-11-17 21:11:30 -05001175 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001176 return ret;
1177}
1178
1179/*
1180 * the device information is stored in the chunk root
1181 * the btrfs_device struct should be fully filled in
1182 */
1183int btrfs_add_device(struct btrfs_trans_handle *trans,
1184 struct btrfs_root *root,
1185 struct btrfs_device *device)
1186{
1187 int ret;
1188 struct btrfs_path *path;
1189 struct btrfs_dev_item *dev_item;
1190 struct extent_buffer *leaf;
1191 struct btrfs_key key;
1192 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001193
1194 root = root->fs_info->chunk_root;
1195
1196 path = btrfs_alloc_path();
1197 if (!path)
1198 return -ENOMEM;
1199
Chris Mason0b86a832008-03-24 15:01:56 -04001200 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1201 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001202 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001203
1204 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001205 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001206 if (ret)
1207 goto out;
1208
1209 leaf = path->nodes[0];
1210 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1211
1212 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001213 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001214 btrfs_set_device_type(leaf, dev_item, device->type);
1215 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1216 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1217 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001218 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1219 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001220 btrfs_set_device_group(leaf, dev_item, 0);
1221 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1222 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001223 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001224
Chris Mason0b86a832008-03-24 15:01:56 -04001225 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001226 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001227 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1228 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001229 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001230
Yan Zheng2b820322008-11-17 21:11:30 -05001231 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001232out:
1233 btrfs_free_path(path);
1234 return ret;
1235}
Chris Mason8f18cf12008-04-25 16:53:30 -04001236
Chris Masona061fc82008-05-07 11:43:44 -04001237static int btrfs_rm_dev_item(struct btrfs_root *root,
1238 struct btrfs_device *device)
1239{
1240 int ret;
1241 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001242 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001243 struct btrfs_trans_handle *trans;
1244
1245 root = root->fs_info->chunk_root;
1246
1247 path = btrfs_alloc_path();
1248 if (!path)
1249 return -ENOMEM;
1250
Yan, Zhenga22285a2010-05-16 10:48:46 -04001251 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001252 if (IS_ERR(trans)) {
1253 btrfs_free_path(path);
1254 return PTR_ERR(trans);
1255 }
Chris Masona061fc82008-05-07 11:43:44 -04001256 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1257 key.type = BTRFS_DEV_ITEM_KEY;
1258 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001259 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001260
1261 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1262 if (ret < 0)
1263 goto out;
1264
1265 if (ret > 0) {
1266 ret = -ENOENT;
1267 goto out;
1268 }
1269
1270 ret = btrfs_del_item(trans, root, path);
1271 if (ret)
1272 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001273out:
1274 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001275 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001276 btrfs_commit_transaction(trans, root);
1277 return ret;
1278}
1279
1280int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1281{
1282 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001283 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001284 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001285 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001286 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001287 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001288 u64 all_avail;
1289 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001290 u64 num_devices;
1291 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001292 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001293 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001294
Chris Masona061fc82008-05-07 11:43:44 -04001295 mutex_lock(&uuid_mutex);
1296
1297 all_avail = root->fs_info->avail_data_alloc_bits |
1298 root->fs_info->avail_system_alloc_bits |
1299 root->fs_info->avail_metadata_alloc_bits;
1300
1301 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001302 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001303 printk(KERN_ERR "btrfs: unable to go below four devices "
1304 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001305 ret = -EINVAL;
1306 goto out;
1307 }
1308
1309 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001310 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001311 printk(KERN_ERR "btrfs: unable to go below two "
1312 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001313 ret = -EINVAL;
1314 goto out;
1315 }
1316
Chris Masondfe25022008-05-13 13:46:40 -04001317 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001318 struct list_head *devices;
1319 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001320
Chris Masondfe25022008-05-13 13:46:40 -04001321 device = NULL;
1322 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001323 /*
1324 * It is safe to read the devices since the volume_mutex
1325 * is held.
1326 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001327 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001328 if (tmp->in_fs_metadata && !tmp->bdev) {
1329 device = tmp;
1330 break;
1331 }
1332 }
1333 bdev = NULL;
1334 bh = NULL;
1335 disk_super = NULL;
1336 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001337 printk(KERN_ERR "btrfs: no missing devices found to "
1338 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001339 goto out;
1340 }
Chris Masondfe25022008-05-13 13:46:40 -04001341 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001342 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1343 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001344 if (IS_ERR(bdev)) {
1345 ret = PTR_ERR(bdev);
1346 goto out;
1347 }
1348
Yan Zheng2b820322008-11-17 21:11:30 -05001349 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001350 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001351 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001352 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001353 goto error_close;
1354 }
1355 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001356 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001357 dev_uuid = disk_super->dev_item.uuid;
1358 device = btrfs_find_device(root, devid, dev_uuid,
1359 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001360 if (!device) {
1361 ret = -ENOENT;
1362 goto error_brelse;
1363 }
Chris Masondfe25022008-05-13 13:46:40 -04001364 }
Yan Zheng2b820322008-11-17 21:11:30 -05001365
1366 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001367 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1368 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001369 ret = -EINVAL;
1370 goto error_brelse;
1371 }
1372
1373 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001374 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001375 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001376 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001377 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001378 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001379 }
Chris Masona061fc82008-05-07 11:43:44 -04001380
1381 ret = btrfs_shrink_device(device, 0);
1382 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001383 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001384
Chris Masona061fc82008-05-07 11:43:44 -04001385 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1386 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001387 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001388
Josef Bacik2bf64752011-09-26 17:12:22 -04001389 spin_lock(&root->fs_info->free_chunk_lock);
1390 root->fs_info->free_chunk_space = device->total_bytes -
1391 device->bytes_used;
1392 spin_unlock(&root->fs_info->free_chunk_lock);
1393
Yan Zheng2b820322008-11-17 21:11:30 -05001394 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001395 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001396
1397 /*
1398 * the device list mutex makes sure that we don't change
1399 * the device list while someone else is writing out all
1400 * the device supers.
1401 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001402
1403 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001404 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001405 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001406
Yan Zhenge4404d62008-12-12 10:03:26 -05001407 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001408
Chris Masoncd02dca2010-12-13 14:56:23 -05001409 if (device->missing)
1410 root->fs_info->fs_devices->missing_devices--;
1411
Yan Zheng2b820322008-11-17 21:11:30 -05001412 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1413 struct btrfs_device, dev_list);
1414 if (device->bdev == root->fs_info->sb->s_bdev)
1415 root->fs_info->sb->s_bdev = next_device->bdev;
1416 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1417 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1418
Xiao Guangrong1f781602011-04-20 10:09:16 +00001419 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001420 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001421
1422 call_rcu(&device->rcu, free_device);
1423 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001424
David Sterba6c417612011-04-13 15:41:04 +02001425 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1426 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001427
Xiao Guangrong1f781602011-04-20 10:09:16 +00001428 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001429 struct btrfs_fs_devices *fs_devices;
1430 fs_devices = root->fs_info->fs_devices;
1431 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001432 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001433 break;
1434 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001435 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001436 fs_devices->seed = cur_devices->seed;
1437 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001438 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001439 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001440 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001441 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001442 }
1443
1444 /*
1445 * at this point, the device is zero sized. We want to
1446 * remove it from the devices list and zero out the old super
1447 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001448 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001449 /* make sure this device isn't detected as part of
1450 * the FS anymore
1451 */
1452 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1453 set_buffer_dirty(bh);
1454 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001455 }
Chris Masona061fc82008-05-07 11:43:44 -04001456
Chris Masona061fc82008-05-07 11:43:44 -04001457 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001458
1459error_brelse:
1460 brelse(bh);
1461error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001462 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001463 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001464out:
1465 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001466 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001467error_undo:
1468 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001469 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001470 list_add(&device->dev_alloc_list,
1471 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001472 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001473 root->fs_info->fs_devices->rw_devices++;
1474 }
1475 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001476}
1477
Yan Zheng2b820322008-11-17 21:11:30 -05001478/*
1479 * does all the dirty work required for changing file system's UUID.
1480 */
Li Zefan125ccb02011-12-08 15:07:24 +08001481static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001482{
1483 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1484 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001485 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001486 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001487 struct btrfs_device *device;
1488 u64 super_flags;
1489
1490 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001491 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001492 return -EINVAL;
1493
Yan Zhenge4404d62008-12-12 10:03:26 -05001494 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1495 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001496 return -ENOMEM;
1497
Yan Zhenge4404d62008-12-12 10:03:26 -05001498 old_devices = clone_fs_devices(fs_devices);
1499 if (IS_ERR(old_devices)) {
1500 kfree(seed_devices);
1501 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001502 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001503
Yan Zheng2b820322008-11-17 21:11:30 -05001504 list_add(&old_devices->list, &fs_uuids);
1505
Yan Zhenge4404d62008-12-12 10:03:26 -05001506 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1507 seed_devices->opened = 1;
1508 INIT_LIST_HEAD(&seed_devices->devices);
1509 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001510 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001511
1512 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001513 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1514 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001515 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1516
Yan Zhenge4404d62008-12-12 10:03:26 -05001517 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1518 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1519 device->fs_devices = seed_devices;
1520 }
1521
Yan Zheng2b820322008-11-17 21:11:30 -05001522 fs_devices->seeding = 0;
1523 fs_devices->num_devices = 0;
1524 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001525 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001526
1527 generate_random_uuid(fs_devices->fsid);
1528 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1529 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1530 super_flags = btrfs_super_flags(disk_super) &
1531 ~BTRFS_SUPER_FLAG_SEEDING;
1532 btrfs_set_super_flags(disk_super, super_flags);
1533
1534 return 0;
1535}
1536
1537/*
1538 * strore the expected generation for seed devices in device items.
1539 */
1540static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1541 struct btrfs_root *root)
1542{
1543 struct btrfs_path *path;
1544 struct extent_buffer *leaf;
1545 struct btrfs_dev_item *dev_item;
1546 struct btrfs_device *device;
1547 struct btrfs_key key;
1548 u8 fs_uuid[BTRFS_UUID_SIZE];
1549 u8 dev_uuid[BTRFS_UUID_SIZE];
1550 u64 devid;
1551 int ret;
1552
1553 path = btrfs_alloc_path();
1554 if (!path)
1555 return -ENOMEM;
1556
1557 root = root->fs_info->chunk_root;
1558 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1559 key.offset = 0;
1560 key.type = BTRFS_DEV_ITEM_KEY;
1561
1562 while (1) {
1563 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1564 if (ret < 0)
1565 goto error;
1566
1567 leaf = path->nodes[0];
1568next_slot:
1569 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1570 ret = btrfs_next_leaf(root, path);
1571 if (ret > 0)
1572 break;
1573 if (ret < 0)
1574 goto error;
1575 leaf = path->nodes[0];
1576 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001577 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001578 continue;
1579 }
1580
1581 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1582 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1583 key.type != BTRFS_DEV_ITEM_KEY)
1584 break;
1585
1586 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1587 struct btrfs_dev_item);
1588 devid = btrfs_device_id(leaf, dev_item);
1589 read_extent_buffer(leaf, dev_uuid,
1590 (unsigned long)btrfs_device_uuid(dev_item),
1591 BTRFS_UUID_SIZE);
1592 read_extent_buffer(leaf, fs_uuid,
1593 (unsigned long)btrfs_device_fsid(dev_item),
1594 BTRFS_UUID_SIZE);
1595 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1596 BUG_ON(!device);
1597
1598 if (device->fs_devices->seeding) {
1599 btrfs_set_device_generation(leaf, dev_item,
1600 device->generation);
1601 btrfs_mark_buffer_dirty(leaf);
1602 }
1603
1604 path->slots[0]++;
1605 goto next_slot;
1606 }
1607 ret = 0;
1608error:
1609 btrfs_free_path(path);
1610 return ret;
1611}
1612
Chris Mason788f20e2008-04-28 15:29:42 -04001613int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1614{
Josef Bacikd5e20032011-08-04 14:52:27 +00001615 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001616 struct btrfs_trans_handle *trans;
1617 struct btrfs_device *device;
1618 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001619 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001620 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001621 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001622 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001623 int ret = 0;
1624
Yan Zheng2b820322008-11-17 21:11:30 -05001625 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1626 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001627
Li Zefana5d16332011-12-07 20:08:40 -05001628 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001629 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001630 if (IS_ERR(bdev))
1631 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001632
Yan Zheng2b820322008-11-17 21:11:30 -05001633 if (root->fs_info->fs_devices->seeding) {
1634 seeding_dev = 1;
1635 down_write(&sb->s_umount);
1636 mutex_lock(&uuid_mutex);
1637 }
1638
Chris Mason8c8bee12008-09-29 11:19:10 -04001639 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001640
Chris Mason788f20e2008-04-28 15:29:42 -04001641 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001642 /*
1643 * we have the volume lock, so we don't need the extra
1644 * device list mutex while reading the list here.
1645 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001646 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001647 if (device->bdev == bdev) {
1648 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001649 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001650 }
1651 }
1652
1653 device = kzalloc(sizeof(*device), GFP_NOFS);
1654 if (!device) {
1655 /* we can safely leave the fs_devices entry around */
1656 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001657 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001658 }
1659
Chris Mason788f20e2008-04-28 15:29:42 -04001660 device->name = kstrdup(device_path, GFP_NOFS);
1661 if (!device->name) {
1662 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001663 ret = -ENOMEM;
1664 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001665 }
Yan Zheng2b820322008-11-17 21:11:30 -05001666
1667 ret = find_next_devid(root, &device->devid);
1668 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001669 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001670 kfree(device);
1671 goto error;
1672 }
1673
Yan, Zhenga22285a2010-05-16 10:48:46 -04001674 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001675 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001676 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001677 kfree(device);
1678 ret = PTR_ERR(trans);
1679 goto error;
1680 }
1681
Yan Zheng2b820322008-11-17 21:11:30 -05001682 lock_chunks(root);
1683
Josef Bacikd5e20032011-08-04 14:52:27 +00001684 q = bdev_get_queue(bdev);
1685 if (blk_queue_discard(q))
1686 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001687 device->writeable = 1;
1688 device->work.func = pending_bios_fn;
1689 generate_random_uuid(device->uuid);
1690 spin_lock_init(&device->io_lock);
1691 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001692 device->io_width = root->sectorsize;
1693 device->io_align = root->sectorsize;
1694 device->sector_size = root->sectorsize;
1695 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001696 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001697 device->dev_root = root->fs_info->dev_root;
1698 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001699 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001700 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001701 set_blocksize(device->bdev, 4096);
1702
Yan Zheng2b820322008-11-17 21:11:30 -05001703 if (seeding_dev) {
1704 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001705 ret = btrfs_prepare_sprout(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001706 BUG_ON(ret);
1707 }
1708
1709 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001710
1711 /*
1712 * we don't want write_supers to jump in here with our device
1713 * half setup
1714 */
1715 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001716 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001717 list_add(&device->dev_alloc_list,
1718 &root->fs_info->fs_devices->alloc_list);
1719 root->fs_info->fs_devices->num_devices++;
1720 root->fs_info->fs_devices->open_devices++;
1721 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001722 if (device->can_discard)
1723 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001724 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1725
Josef Bacik2bf64752011-09-26 17:12:22 -04001726 spin_lock(&root->fs_info->free_chunk_lock);
1727 root->fs_info->free_chunk_space += device->total_bytes;
1728 spin_unlock(&root->fs_info->free_chunk_lock);
1729
Chris Masonc2898112009-06-10 09:51:32 -04001730 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1731 root->fs_info->fs_devices->rotating = 1;
1732
David Sterba6c417612011-04-13 15:41:04 +02001733 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1734 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001735 total_bytes + device->total_bytes);
1736
David Sterba6c417612011-04-13 15:41:04 +02001737 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1738 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001739 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001740 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001741
Yan Zheng2b820322008-11-17 21:11:30 -05001742 if (seeding_dev) {
1743 ret = init_first_rw_device(trans, root, device);
1744 BUG_ON(ret);
1745 ret = btrfs_finish_sprout(trans, root);
1746 BUG_ON(ret);
1747 } else {
1748 ret = btrfs_add_device(trans, root, device);
1749 }
1750
Chris Mason913d9522009-03-10 13:17:18 -04001751 /*
1752 * we've got more storage, clear any full flags on the space
1753 * infos
1754 */
1755 btrfs_clear_space_info_full(root->fs_info);
1756
Chris Mason7d9eb122008-07-08 14:19:17 -04001757 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001758 btrfs_commit_transaction(trans, root);
1759
1760 if (seeding_dev) {
1761 mutex_unlock(&uuid_mutex);
1762 up_write(&sb->s_umount);
1763
1764 ret = btrfs_relocate_sys_chunks(root);
1765 BUG_ON(ret);
1766 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001767
Chris Mason788f20e2008-04-28 15:29:42 -04001768 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001769error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001770 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001771 if (seeding_dev) {
1772 mutex_unlock(&uuid_mutex);
1773 up_write(&sb->s_umount);
1774 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001775 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001776}
1777
Chris Masond3977122009-01-05 21:25:51 -05001778static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1779 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001780{
1781 int ret;
1782 struct btrfs_path *path;
1783 struct btrfs_root *root;
1784 struct btrfs_dev_item *dev_item;
1785 struct extent_buffer *leaf;
1786 struct btrfs_key key;
1787
1788 root = device->dev_root->fs_info->chunk_root;
1789
1790 path = btrfs_alloc_path();
1791 if (!path)
1792 return -ENOMEM;
1793
1794 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1795 key.type = BTRFS_DEV_ITEM_KEY;
1796 key.offset = device->devid;
1797
1798 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1799 if (ret < 0)
1800 goto out;
1801
1802 if (ret > 0) {
1803 ret = -ENOENT;
1804 goto out;
1805 }
1806
1807 leaf = path->nodes[0];
1808 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1809
1810 btrfs_set_device_id(leaf, dev_item, device->devid);
1811 btrfs_set_device_type(leaf, dev_item, device->type);
1812 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1813 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1814 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001815 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001816 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1817 btrfs_mark_buffer_dirty(leaf);
1818
1819out:
1820 btrfs_free_path(path);
1821 return ret;
1822}
1823
Chris Mason7d9eb122008-07-08 14:19:17 -04001824static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001825 struct btrfs_device *device, u64 new_size)
1826{
1827 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001828 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001829 u64 old_total = btrfs_super_total_bytes(super_copy);
1830 u64 diff = new_size - device->total_bytes;
1831
Yan Zheng2b820322008-11-17 21:11:30 -05001832 if (!device->writeable)
1833 return -EACCES;
1834 if (new_size <= device->total_bytes)
1835 return -EINVAL;
1836
Chris Mason8f18cf12008-04-25 16:53:30 -04001837 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001838 device->fs_devices->total_rw_bytes += diff;
1839
1840 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001841 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001842 btrfs_clear_space_info_full(device->dev_root->fs_info);
1843
Chris Mason8f18cf12008-04-25 16:53:30 -04001844 return btrfs_update_device(trans, device);
1845}
1846
Chris Mason7d9eb122008-07-08 14:19:17 -04001847int btrfs_grow_device(struct btrfs_trans_handle *trans,
1848 struct btrfs_device *device, u64 new_size)
1849{
1850 int ret;
1851 lock_chunks(device->dev_root);
1852 ret = __btrfs_grow_device(trans, device, new_size);
1853 unlock_chunks(device->dev_root);
1854 return ret;
1855}
1856
Chris Mason8f18cf12008-04-25 16:53:30 -04001857static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root,
1859 u64 chunk_tree, u64 chunk_objectid,
1860 u64 chunk_offset)
1861{
1862 int ret;
1863 struct btrfs_path *path;
1864 struct btrfs_key key;
1865
1866 root = root->fs_info->chunk_root;
1867 path = btrfs_alloc_path();
1868 if (!path)
1869 return -ENOMEM;
1870
1871 key.objectid = chunk_objectid;
1872 key.offset = chunk_offset;
1873 key.type = BTRFS_CHUNK_ITEM_KEY;
1874
1875 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1876 BUG_ON(ret);
1877
1878 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001879
1880 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001881 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001882}
1883
Christoph Hellwigb2950862008-12-02 09:54:17 -05001884static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001885 chunk_offset)
1886{
David Sterba6c417612011-04-13 15:41:04 +02001887 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001888 struct btrfs_disk_key *disk_key;
1889 struct btrfs_chunk *chunk;
1890 u8 *ptr;
1891 int ret = 0;
1892 u32 num_stripes;
1893 u32 array_size;
1894 u32 len = 0;
1895 u32 cur;
1896 struct btrfs_key key;
1897
1898 array_size = btrfs_super_sys_array_size(super_copy);
1899
1900 ptr = super_copy->sys_chunk_array;
1901 cur = 0;
1902
1903 while (cur < array_size) {
1904 disk_key = (struct btrfs_disk_key *)ptr;
1905 btrfs_disk_key_to_cpu(&key, disk_key);
1906
1907 len = sizeof(*disk_key);
1908
1909 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1910 chunk = (struct btrfs_chunk *)(ptr + len);
1911 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1912 len += btrfs_chunk_item_size(num_stripes);
1913 } else {
1914 ret = -EIO;
1915 break;
1916 }
1917 if (key.objectid == chunk_objectid &&
1918 key.offset == chunk_offset) {
1919 memmove(ptr, ptr + len, array_size - (cur + len));
1920 array_size -= len;
1921 btrfs_set_super_sys_array_size(super_copy, array_size);
1922 } else {
1923 ptr += len;
1924 cur += len;
1925 }
1926 }
1927 return ret;
1928}
1929
Christoph Hellwigb2950862008-12-02 09:54:17 -05001930static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001931 u64 chunk_tree, u64 chunk_objectid,
1932 u64 chunk_offset)
1933{
1934 struct extent_map_tree *em_tree;
1935 struct btrfs_root *extent_root;
1936 struct btrfs_trans_handle *trans;
1937 struct extent_map *em;
1938 struct map_lookup *map;
1939 int ret;
1940 int i;
1941
1942 root = root->fs_info->chunk_root;
1943 extent_root = root->fs_info->extent_root;
1944 em_tree = &root->fs_info->mapping_tree.map_tree;
1945
Josef Bacikba1bf482009-09-11 16:11:19 -04001946 ret = btrfs_can_relocate(extent_root, chunk_offset);
1947 if (ret)
1948 return -ENOSPC;
1949
Chris Mason8f18cf12008-04-25 16:53:30 -04001950 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001951 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001952 if (ret)
1953 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001954
Yan, Zhenga22285a2010-05-16 10:48:46 -04001955 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001956 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001957
Chris Mason7d9eb122008-07-08 14:19:17 -04001958 lock_chunks(root);
1959
Chris Mason8f18cf12008-04-25 16:53:30 -04001960 /*
1961 * step two, delete the device extents and the
1962 * chunk tree entries
1963 */
Chris Mason890871b2009-09-02 16:24:52 -04001964 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001965 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001966 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001967
Tsutomu Itoh285190d2012-02-16 16:23:58 +09001968 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04001969 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001970 map = (struct map_lookup *)em->bdev;
1971
1972 for (i = 0; i < map->num_stripes; i++) {
1973 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1974 map->stripes[i].physical);
1975 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001976
Chris Masondfe25022008-05-13 13:46:40 -04001977 if (map->stripes[i].dev) {
1978 ret = btrfs_update_device(trans, map->stripes[i].dev);
1979 BUG_ON(ret);
1980 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001981 }
1982 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1983 chunk_offset);
1984
1985 BUG_ON(ret);
1986
liubo1abe9b82011-03-24 11:18:59 +00001987 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1988
Chris Mason8f18cf12008-04-25 16:53:30 -04001989 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1990 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1991 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001992 }
1993
Zheng Yan1a40e232008-09-26 10:09:34 -04001994 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1995 BUG_ON(ret);
1996
Chris Mason890871b2009-09-02 16:24:52 -04001997 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001998 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001999 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002000
Chris Mason8f18cf12008-04-25 16:53:30 -04002001 kfree(map);
2002 em->bdev = NULL;
2003
2004 /* once for the tree */
2005 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002006 /* once for us */
2007 free_extent_map(em);
2008
Chris Mason7d9eb122008-07-08 14:19:17 -04002009 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002010 btrfs_end_transaction(trans, root);
2011 return 0;
2012}
2013
Yan Zheng2b820322008-11-17 21:11:30 -05002014static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2015{
2016 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2017 struct btrfs_path *path;
2018 struct extent_buffer *leaf;
2019 struct btrfs_chunk *chunk;
2020 struct btrfs_key key;
2021 struct btrfs_key found_key;
2022 u64 chunk_tree = chunk_root->root_key.objectid;
2023 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002024 bool retried = false;
2025 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002026 int ret;
2027
2028 path = btrfs_alloc_path();
2029 if (!path)
2030 return -ENOMEM;
2031
Josef Bacikba1bf482009-09-11 16:11:19 -04002032again:
Yan Zheng2b820322008-11-17 21:11:30 -05002033 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2034 key.offset = (u64)-1;
2035 key.type = BTRFS_CHUNK_ITEM_KEY;
2036
2037 while (1) {
2038 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2039 if (ret < 0)
2040 goto error;
2041 BUG_ON(ret == 0);
2042
2043 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2044 key.type);
2045 if (ret < 0)
2046 goto error;
2047 if (ret > 0)
2048 break;
2049
2050 leaf = path->nodes[0];
2051 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2052
2053 chunk = btrfs_item_ptr(leaf, path->slots[0],
2054 struct btrfs_chunk);
2055 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002056 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002057
2058 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2059 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2060 found_key.objectid,
2061 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002062 if (ret == -ENOSPC)
2063 failed++;
2064 else if (ret)
2065 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002066 }
2067
2068 if (found_key.offset == 0)
2069 break;
2070 key.offset = found_key.offset - 1;
2071 }
2072 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002073 if (failed && !retried) {
2074 failed = 0;
2075 retried = true;
2076 goto again;
2077 } else if (failed && retried) {
2078 WARN_ON(1);
2079 ret = -ENOSPC;
2080 }
Yan Zheng2b820322008-11-17 21:11:30 -05002081error:
2082 btrfs_free_path(path);
2083 return ret;
2084}
2085
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002086static int insert_balance_item(struct btrfs_root *root,
2087 struct btrfs_balance_control *bctl)
2088{
2089 struct btrfs_trans_handle *trans;
2090 struct btrfs_balance_item *item;
2091 struct btrfs_disk_balance_args disk_bargs;
2092 struct btrfs_path *path;
2093 struct extent_buffer *leaf;
2094 struct btrfs_key key;
2095 int ret, err;
2096
2097 path = btrfs_alloc_path();
2098 if (!path)
2099 return -ENOMEM;
2100
2101 trans = btrfs_start_transaction(root, 0);
2102 if (IS_ERR(trans)) {
2103 btrfs_free_path(path);
2104 return PTR_ERR(trans);
2105 }
2106
2107 key.objectid = BTRFS_BALANCE_OBJECTID;
2108 key.type = BTRFS_BALANCE_ITEM_KEY;
2109 key.offset = 0;
2110
2111 ret = btrfs_insert_empty_item(trans, root, path, &key,
2112 sizeof(*item));
2113 if (ret)
2114 goto out;
2115
2116 leaf = path->nodes[0];
2117 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2118
2119 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2120
2121 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2122 btrfs_set_balance_data(leaf, item, &disk_bargs);
2123 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2124 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2125 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2126 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2127
2128 btrfs_set_balance_flags(leaf, item, bctl->flags);
2129
2130 btrfs_mark_buffer_dirty(leaf);
2131out:
2132 btrfs_free_path(path);
2133 err = btrfs_commit_transaction(trans, root);
2134 if (err && !ret)
2135 ret = err;
2136 return ret;
2137}
2138
2139static int del_balance_item(struct btrfs_root *root)
2140{
2141 struct btrfs_trans_handle *trans;
2142 struct btrfs_path *path;
2143 struct btrfs_key key;
2144 int ret, err;
2145
2146 path = btrfs_alloc_path();
2147 if (!path)
2148 return -ENOMEM;
2149
2150 trans = btrfs_start_transaction(root, 0);
2151 if (IS_ERR(trans)) {
2152 btrfs_free_path(path);
2153 return PTR_ERR(trans);
2154 }
2155
2156 key.objectid = BTRFS_BALANCE_OBJECTID;
2157 key.type = BTRFS_BALANCE_ITEM_KEY;
2158 key.offset = 0;
2159
2160 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2161 if (ret < 0)
2162 goto out;
2163 if (ret > 0) {
2164 ret = -ENOENT;
2165 goto out;
2166 }
2167
2168 ret = btrfs_del_item(trans, root, path);
2169out:
2170 btrfs_free_path(path);
2171 err = btrfs_commit_transaction(trans, root);
2172 if (err && !ret)
2173 ret = err;
2174 return ret;
2175}
2176
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002177/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002178 * This is a heuristic used to reduce the number of chunks balanced on
2179 * resume after balance was interrupted.
2180 */
2181static void update_balance_args(struct btrfs_balance_control *bctl)
2182{
2183 /*
2184 * Turn on soft mode for chunk types that were being converted.
2185 */
2186 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2187 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2188 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2189 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2190 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2191 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2192
2193 /*
2194 * Turn on usage filter if is not already used. The idea is
2195 * that chunks that we have already balanced should be
2196 * reasonably full. Don't do it for chunks that are being
2197 * converted - that will keep us from relocating unconverted
2198 * (albeit full) chunks.
2199 */
2200 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2201 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2202 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2203 bctl->data.usage = 90;
2204 }
2205 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2206 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2207 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2208 bctl->sys.usage = 90;
2209 }
2210 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2211 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2212 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2213 bctl->meta.usage = 90;
2214 }
2215}
2216
2217/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002218 * Should be called with both balance and volume mutexes held to
2219 * serialize other volume operations (add_dev/rm_dev/resize) with
2220 * restriper. Same goes for unset_balance_control.
2221 */
2222static void set_balance_control(struct btrfs_balance_control *bctl)
2223{
2224 struct btrfs_fs_info *fs_info = bctl->fs_info;
2225
2226 BUG_ON(fs_info->balance_ctl);
2227
2228 spin_lock(&fs_info->balance_lock);
2229 fs_info->balance_ctl = bctl;
2230 spin_unlock(&fs_info->balance_lock);
2231}
2232
2233static void unset_balance_control(struct btrfs_fs_info *fs_info)
2234{
2235 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2236
2237 BUG_ON(!fs_info->balance_ctl);
2238
2239 spin_lock(&fs_info->balance_lock);
2240 fs_info->balance_ctl = NULL;
2241 spin_unlock(&fs_info->balance_lock);
2242
2243 kfree(bctl);
2244}
2245
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002246/*
2247 * Balance filters. Return 1 if chunk should be filtered out
2248 * (should not be balanced).
2249 */
2250static int chunk_profiles_filter(u64 chunk_profile,
2251 struct btrfs_balance_args *bargs)
2252{
2253 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2254
2255 if (chunk_profile == 0)
2256 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2257
2258 if (bargs->profiles & chunk_profile)
2259 return 0;
2260
2261 return 1;
2262}
2263
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002264static u64 div_factor_fine(u64 num, int factor)
2265{
2266 if (factor <= 0)
2267 return 0;
2268 if (factor >= 100)
2269 return num;
2270
2271 num *= factor;
2272 do_div(num, 100);
2273 return num;
2274}
2275
2276static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2277 struct btrfs_balance_args *bargs)
2278{
2279 struct btrfs_block_group_cache *cache;
2280 u64 chunk_used, user_thresh;
2281 int ret = 1;
2282
2283 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2284 chunk_used = btrfs_block_group_used(&cache->item);
2285
2286 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2287 if (chunk_used < user_thresh)
2288 ret = 0;
2289
2290 btrfs_put_block_group(cache);
2291 return ret;
2292}
2293
Ilya Dryomov409d4042012-01-16 22:04:47 +02002294static int chunk_devid_filter(struct extent_buffer *leaf,
2295 struct btrfs_chunk *chunk,
2296 struct btrfs_balance_args *bargs)
2297{
2298 struct btrfs_stripe *stripe;
2299 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2300 int i;
2301
2302 for (i = 0; i < num_stripes; i++) {
2303 stripe = btrfs_stripe_nr(chunk, i);
2304 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2305 return 0;
2306 }
2307
2308 return 1;
2309}
2310
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002311/* [pstart, pend) */
2312static int chunk_drange_filter(struct extent_buffer *leaf,
2313 struct btrfs_chunk *chunk,
2314 u64 chunk_offset,
2315 struct btrfs_balance_args *bargs)
2316{
2317 struct btrfs_stripe *stripe;
2318 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2319 u64 stripe_offset;
2320 u64 stripe_length;
2321 int factor;
2322 int i;
2323
2324 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2325 return 0;
2326
2327 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2328 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2329 factor = 2;
2330 else
2331 factor = 1;
2332 factor = num_stripes / factor;
2333
2334 for (i = 0; i < num_stripes; i++) {
2335 stripe = btrfs_stripe_nr(chunk, i);
2336 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2337 continue;
2338
2339 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2340 stripe_length = btrfs_chunk_length(leaf, chunk);
2341 do_div(stripe_length, factor);
2342
2343 if (stripe_offset < bargs->pend &&
2344 stripe_offset + stripe_length > bargs->pstart)
2345 return 0;
2346 }
2347
2348 return 1;
2349}
2350
Ilya Dryomovea671762012-01-16 22:04:48 +02002351/* [vstart, vend) */
2352static int chunk_vrange_filter(struct extent_buffer *leaf,
2353 struct btrfs_chunk *chunk,
2354 u64 chunk_offset,
2355 struct btrfs_balance_args *bargs)
2356{
2357 if (chunk_offset < bargs->vend &&
2358 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2359 /* at least part of the chunk is inside this vrange */
2360 return 0;
2361
2362 return 1;
2363}
2364
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002365static int chunk_soft_convert_filter(u64 chunk_profile,
2366 struct btrfs_balance_args *bargs)
2367{
2368 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2369 return 0;
2370
2371 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2372
2373 if (chunk_profile == 0)
2374 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2375
2376 if (bargs->target & chunk_profile)
2377 return 1;
2378
2379 return 0;
2380}
2381
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002382static int should_balance_chunk(struct btrfs_root *root,
2383 struct extent_buffer *leaf,
2384 struct btrfs_chunk *chunk, u64 chunk_offset)
2385{
2386 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2387 struct btrfs_balance_args *bargs = NULL;
2388 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2389
2390 /* type filter */
2391 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2392 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2393 return 0;
2394 }
2395
2396 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2397 bargs = &bctl->data;
2398 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2399 bargs = &bctl->sys;
2400 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2401 bargs = &bctl->meta;
2402
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002403 /* profiles filter */
2404 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2405 chunk_profiles_filter(chunk_type, bargs)) {
2406 return 0;
2407 }
2408
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002409 /* usage filter */
2410 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2411 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2412 return 0;
2413 }
2414
Ilya Dryomov409d4042012-01-16 22:04:47 +02002415 /* devid filter */
2416 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2417 chunk_devid_filter(leaf, chunk, bargs)) {
2418 return 0;
2419 }
2420
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002421 /* drange filter, makes sense only with devid filter */
2422 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2423 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2424 return 0;
2425 }
2426
Ilya Dryomovea671762012-01-16 22:04:48 +02002427 /* vrange filter */
2428 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2429 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2430 return 0;
2431 }
2432
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002433 /* soft profile changing mode */
2434 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2435 chunk_soft_convert_filter(chunk_type, bargs)) {
2436 return 0;
2437 }
2438
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002439 return 1;
2440}
2441
Chris Masonec44a352008-04-28 15:29:52 -04002442static u64 div_factor(u64 num, int factor)
2443{
2444 if (factor == 10)
2445 return num;
2446 num *= factor;
2447 do_div(num, 10);
2448 return num;
2449}
2450
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002451static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002452{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002453 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002454 struct btrfs_root *chunk_root = fs_info->chunk_root;
2455 struct btrfs_root *dev_root = fs_info->dev_root;
2456 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002457 struct btrfs_device *device;
2458 u64 old_size;
2459 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002460 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002461 struct btrfs_path *path;
2462 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002463 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002464 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002465 struct extent_buffer *leaf;
2466 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002467 int ret;
2468 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002469 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002470
Chris Masonec44a352008-04-28 15:29:52 -04002471 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002472 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002473 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002474 old_size = device->total_bytes;
2475 size_to_free = div_factor(old_size, 1);
2476 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002477 if (!device->writeable ||
2478 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002479 continue;
2480
2481 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002482 if (ret == -ENOSPC)
2483 break;
Chris Masonec44a352008-04-28 15:29:52 -04002484 BUG_ON(ret);
2485
Yan, Zhenga22285a2010-05-16 10:48:46 -04002486 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002487 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002488
2489 ret = btrfs_grow_device(trans, device, old_size);
2490 BUG_ON(ret);
2491
2492 btrfs_end_transaction(trans, dev_root);
2493 }
2494
2495 /* step two, relocate all the chunks */
2496 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002497 if (!path) {
2498 ret = -ENOMEM;
2499 goto error;
2500 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002501
2502 /* zero out stat counters */
2503 spin_lock(&fs_info->balance_lock);
2504 memset(&bctl->stat, 0, sizeof(bctl->stat));
2505 spin_unlock(&fs_info->balance_lock);
2506again:
Chris Masonec44a352008-04-28 15:29:52 -04002507 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2508 key.offset = (u64)-1;
2509 key.type = BTRFS_CHUNK_ITEM_KEY;
2510
Chris Masond3977122009-01-05 21:25:51 -05002511 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002512 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002513 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002514 ret = -ECANCELED;
2515 goto error;
2516 }
2517
Chris Masonec44a352008-04-28 15:29:52 -04002518 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2519 if (ret < 0)
2520 goto error;
2521
2522 /*
2523 * this shouldn't happen, it means the last relocate
2524 * failed
2525 */
2526 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002527 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002528
2529 ret = btrfs_previous_item(chunk_root, path, 0,
2530 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002531 if (ret) {
2532 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002533 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002534 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002535
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002536 leaf = path->nodes[0];
2537 slot = path->slots[0];
2538 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2539
Chris Masonec44a352008-04-28 15:29:52 -04002540 if (found_key.objectid != key.objectid)
2541 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002542
Chris Masonec44a352008-04-28 15:29:52 -04002543 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002544 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002545 break;
2546
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002547 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2548
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002549 if (!counting) {
2550 spin_lock(&fs_info->balance_lock);
2551 bctl->stat.considered++;
2552 spin_unlock(&fs_info->balance_lock);
2553 }
2554
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002555 ret = should_balance_chunk(chunk_root, leaf, chunk,
2556 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002557 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002558 if (!ret)
2559 goto loop;
2560
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002561 if (counting) {
2562 spin_lock(&fs_info->balance_lock);
2563 bctl->stat.expected++;
2564 spin_unlock(&fs_info->balance_lock);
2565 goto loop;
2566 }
2567
Chris Masonec44a352008-04-28 15:29:52 -04002568 ret = btrfs_relocate_chunk(chunk_root,
2569 chunk_root->root_key.objectid,
2570 found_key.objectid,
2571 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002572 if (ret && ret != -ENOSPC)
2573 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002574 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002575 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002576 } else {
2577 spin_lock(&fs_info->balance_lock);
2578 bctl->stat.completed++;
2579 spin_unlock(&fs_info->balance_lock);
2580 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002581loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002582 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002583 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002584
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002585 if (counting) {
2586 btrfs_release_path(path);
2587 counting = false;
2588 goto again;
2589 }
Chris Masonec44a352008-04-28 15:29:52 -04002590error:
2591 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002592 if (enospc_errors) {
2593 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2594 enospc_errors);
2595 if (!ret)
2596 ret = -ENOSPC;
2597 }
2598
Chris Masonec44a352008-04-28 15:29:52 -04002599 return ret;
2600}
2601
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002602static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2603{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002604 /* cancel requested || normal exit path */
2605 return atomic_read(&fs_info->balance_cancel_req) ||
2606 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2607 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002608}
2609
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002610static void __cancel_balance(struct btrfs_fs_info *fs_info)
2611{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002612 int ret;
2613
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002614 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002615 ret = del_balance_item(fs_info->tree_root);
2616 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002617}
2618
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002619void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002620 struct btrfs_ioctl_balance_args *bargs);
2621
2622/*
2623 * Should be called with both balance and volume mutexes held
2624 */
2625int btrfs_balance(struct btrfs_balance_control *bctl,
2626 struct btrfs_ioctl_balance_args *bargs)
2627{
2628 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002629 u64 allowed;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002630 int ret;
2631
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002632 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002633 atomic_read(&fs_info->balance_pause_req) ||
2634 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002635 ret = -EINVAL;
2636 goto out;
2637 }
2638
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002639 /*
2640 * In case of mixed groups both data and meta should be picked,
2641 * and identical options should be given for both of them.
2642 */
2643 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2644 if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
2645 (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
2646 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2647 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2648 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2649 printk(KERN_ERR "btrfs: with mixed groups data and "
2650 "metadata balance options must be the same\n");
2651 ret = -EINVAL;
2652 goto out;
2653 }
2654 }
2655
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002656 /*
2657 * Profile changing sanity checks. Skip them if a simple
2658 * balance is requested.
2659 */
2660 if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
2661 BTRFS_BALANCE_ARGS_CONVERT))
2662 goto do_balance;
2663
2664 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2665 if (fs_info->fs_devices->num_devices == 1)
2666 allowed |= BTRFS_BLOCK_GROUP_DUP;
2667 else if (fs_info->fs_devices->num_devices < 4)
2668 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2669 else
2670 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2671 BTRFS_BLOCK_GROUP_RAID10);
2672
2673 if (!profile_is_valid(bctl->data.target, 1) ||
2674 bctl->data.target & ~allowed) {
2675 printk(KERN_ERR "btrfs: unable to start balance with target "
2676 "data profile %llu\n",
2677 (unsigned long long)bctl->data.target);
2678 ret = -EINVAL;
2679 goto out;
2680 }
2681 if (!profile_is_valid(bctl->meta.target, 1) ||
2682 bctl->meta.target & ~allowed) {
2683 printk(KERN_ERR "btrfs: unable to start balance with target "
2684 "metadata profile %llu\n",
2685 (unsigned long long)bctl->meta.target);
2686 ret = -EINVAL;
2687 goto out;
2688 }
2689 if (!profile_is_valid(bctl->sys.target, 1) ||
2690 bctl->sys.target & ~allowed) {
2691 printk(KERN_ERR "btrfs: unable to start balance with target "
2692 "system profile %llu\n",
2693 (unsigned long long)bctl->sys.target);
2694 ret = -EINVAL;
2695 goto out;
2696 }
2697
2698 if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
2699 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2700 ret = -EINVAL;
2701 goto out;
2702 }
2703
2704 /* allow to reduce meta or sys integrity only if force set */
2705 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2706 BTRFS_BLOCK_GROUP_RAID10;
2707 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2708 (fs_info->avail_system_alloc_bits & allowed) &&
2709 !(bctl->sys.target & allowed)) ||
2710 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2711 (fs_info->avail_metadata_alloc_bits & allowed) &&
2712 !(bctl->meta.target & allowed))) {
2713 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2714 printk(KERN_INFO "btrfs: force reducing metadata "
2715 "integrity\n");
2716 } else {
2717 printk(KERN_ERR "btrfs: balance will reduce metadata "
2718 "integrity, use force if you want this\n");
2719 ret = -EINVAL;
2720 goto out;
2721 }
2722 }
2723
2724do_balance:
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002725 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002726 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002727 goto out;
2728
Ilya Dryomov59641012012-01-16 22:04:48 +02002729 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2730 BUG_ON(ret == -EEXIST);
2731 set_balance_control(bctl);
2732 } else {
2733 BUG_ON(ret != -EEXIST);
2734 spin_lock(&fs_info->balance_lock);
2735 update_balance_args(bctl);
2736 spin_unlock(&fs_info->balance_lock);
2737 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002738
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002739 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002740 mutex_unlock(&fs_info->balance_mutex);
2741
2742 ret = __btrfs_balance(fs_info);
2743
2744 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002745 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002746
2747 if (bargs) {
2748 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002749 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002750 }
2751
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002752 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2753 balance_need_close(fs_info)) {
2754 __cancel_balance(fs_info);
2755 }
2756
2757 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002758
2759 return ret;
2760out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002761 if (bctl->flags & BTRFS_BALANCE_RESUME)
2762 __cancel_balance(fs_info);
2763 else
2764 kfree(bctl);
2765 return ret;
2766}
2767
2768static int balance_kthread(void *data)
2769{
2770 struct btrfs_balance_control *bctl =
2771 (struct btrfs_balance_control *)data;
2772 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002773 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002774
2775 mutex_lock(&fs_info->volume_mutex);
2776 mutex_lock(&fs_info->balance_mutex);
2777
2778 set_balance_control(bctl);
2779
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002780 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2781 printk(KERN_INFO "btrfs: force skipping balance\n");
2782 } else {
2783 printk(KERN_INFO "btrfs: continuing balance\n");
2784 ret = btrfs_balance(bctl, NULL);
2785 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002786
2787 mutex_unlock(&fs_info->balance_mutex);
2788 mutex_unlock(&fs_info->volume_mutex);
2789 return ret;
2790}
2791
2792int btrfs_recover_balance(struct btrfs_root *tree_root)
2793{
2794 struct task_struct *tsk;
2795 struct btrfs_balance_control *bctl;
2796 struct btrfs_balance_item *item;
2797 struct btrfs_disk_balance_args disk_bargs;
2798 struct btrfs_path *path;
2799 struct extent_buffer *leaf;
2800 struct btrfs_key key;
2801 int ret;
2802
2803 path = btrfs_alloc_path();
2804 if (!path)
2805 return -ENOMEM;
2806
2807 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2808 if (!bctl) {
2809 ret = -ENOMEM;
2810 goto out;
2811 }
2812
2813 key.objectid = BTRFS_BALANCE_OBJECTID;
2814 key.type = BTRFS_BALANCE_ITEM_KEY;
2815 key.offset = 0;
2816
2817 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2818 if (ret < 0)
2819 goto out_bctl;
2820 if (ret > 0) { /* ret = -ENOENT; */
2821 ret = 0;
2822 goto out_bctl;
2823 }
2824
2825 leaf = path->nodes[0];
2826 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2827
2828 bctl->fs_info = tree_root->fs_info;
2829 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2830
2831 btrfs_balance_data(leaf, item, &disk_bargs);
2832 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2833 btrfs_balance_meta(leaf, item, &disk_bargs);
2834 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2835 btrfs_balance_sys(leaf, item, &disk_bargs);
2836 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2837
2838 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2839 if (IS_ERR(tsk))
2840 ret = PTR_ERR(tsk);
2841 else
2842 goto out;
2843
2844out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002845 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002846out:
2847 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002848 return ret;
2849}
2850
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002851int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2852{
2853 int ret = 0;
2854
2855 mutex_lock(&fs_info->balance_mutex);
2856 if (!fs_info->balance_ctl) {
2857 mutex_unlock(&fs_info->balance_mutex);
2858 return -ENOTCONN;
2859 }
2860
2861 if (atomic_read(&fs_info->balance_running)) {
2862 atomic_inc(&fs_info->balance_pause_req);
2863 mutex_unlock(&fs_info->balance_mutex);
2864
2865 wait_event(fs_info->balance_wait_q,
2866 atomic_read(&fs_info->balance_running) == 0);
2867
2868 mutex_lock(&fs_info->balance_mutex);
2869 /* we are good with balance_ctl ripped off from under us */
2870 BUG_ON(atomic_read(&fs_info->balance_running));
2871 atomic_dec(&fs_info->balance_pause_req);
2872 } else {
2873 ret = -ENOTCONN;
2874 }
2875
2876 mutex_unlock(&fs_info->balance_mutex);
2877 return ret;
2878}
2879
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002880int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2881{
2882 mutex_lock(&fs_info->balance_mutex);
2883 if (!fs_info->balance_ctl) {
2884 mutex_unlock(&fs_info->balance_mutex);
2885 return -ENOTCONN;
2886 }
2887
2888 atomic_inc(&fs_info->balance_cancel_req);
2889 /*
2890 * if we are running just wait and return, balance item is
2891 * deleted in btrfs_balance in this case
2892 */
2893 if (atomic_read(&fs_info->balance_running)) {
2894 mutex_unlock(&fs_info->balance_mutex);
2895 wait_event(fs_info->balance_wait_q,
2896 atomic_read(&fs_info->balance_running) == 0);
2897 mutex_lock(&fs_info->balance_mutex);
2898 } else {
2899 /* __cancel_balance needs volume_mutex */
2900 mutex_unlock(&fs_info->balance_mutex);
2901 mutex_lock(&fs_info->volume_mutex);
2902 mutex_lock(&fs_info->balance_mutex);
2903
2904 if (fs_info->balance_ctl)
2905 __cancel_balance(fs_info);
2906
2907 mutex_unlock(&fs_info->volume_mutex);
2908 }
2909
2910 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2911 atomic_dec(&fs_info->balance_cancel_req);
2912 mutex_unlock(&fs_info->balance_mutex);
2913 return 0;
2914}
2915
Chris Mason8f18cf12008-04-25 16:53:30 -04002916/*
2917 * shrinking a device means finding all of the device extents past
2918 * the new size, and then following the back refs to the chunks.
2919 * The chunk relocation code actually frees the device extent
2920 */
2921int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2922{
2923 struct btrfs_trans_handle *trans;
2924 struct btrfs_root *root = device->dev_root;
2925 struct btrfs_dev_extent *dev_extent = NULL;
2926 struct btrfs_path *path;
2927 u64 length;
2928 u64 chunk_tree;
2929 u64 chunk_objectid;
2930 u64 chunk_offset;
2931 int ret;
2932 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002933 int failed = 0;
2934 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002935 struct extent_buffer *l;
2936 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002937 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002938 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002939 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002940 u64 diff = device->total_bytes - new_size;
2941
Yan Zheng2b820322008-11-17 21:11:30 -05002942 if (new_size >= device->total_bytes)
2943 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002944
2945 path = btrfs_alloc_path();
2946 if (!path)
2947 return -ENOMEM;
2948
Chris Mason8f18cf12008-04-25 16:53:30 -04002949 path->reada = 2;
2950
Chris Mason7d9eb122008-07-08 14:19:17 -04002951 lock_chunks(root);
2952
Chris Mason8f18cf12008-04-25 16:53:30 -04002953 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002954 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002955 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002956 spin_lock(&root->fs_info->free_chunk_lock);
2957 root->fs_info->free_chunk_space -= diff;
2958 spin_unlock(&root->fs_info->free_chunk_lock);
2959 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002960 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002961
Josef Bacikba1bf482009-09-11 16:11:19 -04002962again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002963 key.objectid = device->devid;
2964 key.offset = (u64)-1;
2965 key.type = BTRFS_DEV_EXTENT_KEY;
2966
2967 while (1) {
2968 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2969 if (ret < 0)
2970 goto done;
2971
2972 ret = btrfs_previous_item(root, path, 0, key.type);
2973 if (ret < 0)
2974 goto done;
2975 if (ret) {
2976 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002977 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002978 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002979 }
2980
2981 l = path->nodes[0];
2982 slot = path->slots[0];
2983 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2984
Josef Bacikba1bf482009-09-11 16:11:19 -04002985 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002986 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002987 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002988 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002989
2990 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2991 length = btrfs_dev_extent_length(l, dev_extent);
2992
Josef Bacikba1bf482009-09-11 16:11:19 -04002993 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002994 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002995 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002996 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002997
2998 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2999 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3000 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003001 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003002
3003 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3004 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003005 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003006 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003007 if (ret == -ENOSPC)
3008 failed++;
3009 key.offset -= 1;
3010 }
3011
3012 if (failed && !retried) {
3013 failed = 0;
3014 retried = true;
3015 goto again;
3016 } else if (failed && retried) {
3017 ret = -ENOSPC;
3018 lock_chunks(root);
3019
3020 device->total_bytes = old_size;
3021 if (device->writeable)
3022 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003023 spin_lock(&root->fs_info->free_chunk_lock);
3024 root->fs_info->free_chunk_space += diff;
3025 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003026 unlock_chunks(root);
3027 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003028 }
3029
Chris Balld6397ba2009-04-27 07:29:03 -04003030 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003031 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003032 if (IS_ERR(trans)) {
3033 ret = PTR_ERR(trans);
3034 goto done;
3035 }
3036
Chris Balld6397ba2009-04-27 07:29:03 -04003037 lock_chunks(root);
3038
3039 device->disk_total_bytes = new_size;
3040 /* Now btrfs_update_device() will change the on-disk size. */
3041 ret = btrfs_update_device(trans, device);
3042 if (ret) {
3043 unlock_chunks(root);
3044 btrfs_end_transaction(trans, root);
3045 goto done;
3046 }
3047 WARN_ON(diff > old_total);
3048 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3049 unlock_chunks(root);
3050 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003051done:
3052 btrfs_free_path(path);
3053 return ret;
3054}
3055
Li Zefan125ccb02011-12-08 15:07:24 +08003056static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003057 struct btrfs_key *key,
3058 struct btrfs_chunk *chunk, int item_size)
3059{
David Sterba6c417612011-04-13 15:41:04 +02003060 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003061 struct btrfs_disk_key disk_key;
3062 u32 array_size;
3063 u8 *ptr;
3064
3065 array_size = btrfs_super_sys_array_size(super_copy);
3066 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3067 return -EFBIG;
3068
3069 ptr = super_copy->sys_chunk_array + array_size;
3070 btrfs_cpu_key_to_disk(&disk_key, key);
3071 memcpy(ptr, &disk_key, sizeof(disk_key));
3072 ptr += sizeof(disk_key);
3073 memcpy(ptr, chunk, item_size);
3074 item_size += sizeof(disk_key);
3075 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3076 return 0;
3077}
3078
Miao Xieb2117a32011-01-05 10:07:28 +00003079/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003080 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003081 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003082static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003083{
Arne Jansen73c5de02011-04-12 12:07:57 +02003084 const struct btrfs_device_info *di_a = a;
3085 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003086
Arne Jansen73c5de02011-04-12 12:07:57 +02003087 if (di_a->max_avail > di_b->max_avail)
3088 return -1;
3089 if (di_a->max_avail < di_b->max_avail)
3090 return 1;
3091 if (di_a->total_avail > di_b->total_avail)
3092 return -1;
3093 if (di_a->total_avail < di_b->total_avail)
3094 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003095 return 0;
3096}
3097
3098static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3099 struct btrfs_root *extent_root,
3100 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003101 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003102 u64 start, u64 type)
3103{
3104 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003105 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3106 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003107 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003108 struct extent_map_tree *em_tree;
3109 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003110 struct btrfs_device_info *devices_info = NULL;
3111 u64 total_avail;
3112 int num_stripes; /* total number of stripes to allocate */
3113 int sub_stripes; /* sub_stripes info for map */
3114 int dev_stripes; /* stripes per dev */
3115 int devs_max; /* max devs to use */
3116 int devs_min; /* min devs needed */
3117 int devs_increment; /* ndevs has to be a multiple of this */
3118 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003119 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003120 u64 max_stripe_size;
3121 u64 max_chunk_size;
3122 u64 stripe_size;
3123 u64 num_bytes;
3124 int ndevs;
3125 int i;
3126 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003127
3128 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
3129 (type & BTRFS_BLOCK_GROUP_DUP)) {
3130 WARN_ON(1);
3131 type &= ~BTRFS_BLOCK_GROUP_DUP;
3132 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003133
Miao Xieb2117a32011-01-05 10:07:28 +00003134 if (list_empty(&fs_devices->alloc_list))
3135 return -ENOSPC;
3136
Arne Jansen73c5de02011-04-12 12:07:57 +02003137 sub_stripes = 1;
3138 dev_stripes = 1;
3139 devs_increment = 1;
3140 ncopies = 1;
3141 devs_max = 0; /* 0 == as many as possible */
3142 devs_min = 1;
3143
3144 /*
3145 * define the properties of each RAID type.
3146 * FIXME: move this to a global table and use it in all RAID
3147 * calculation code
3148 */
3149 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3150 dev_stripes = 2;
3151 ncopies = 2;
3152 devs_max = 1;
3153 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3154 devs_min = 2;
3155 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3156 devs_increment = 2;
3157 ncopies = 2;
3158 devs_max = 2;
3159 devs_min = 2;
3160 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3161 sub_stripes = 2;
3162 devs_increment = 2;
3163 ncopies = 2;
3164 devs_min = 4;
3165 } else {
3166 devs_max = 1;
3167 }
3168
3169 if (type & BTRFS_BLOCK_GROUP_DATA) {
3170 max_stripe_size = 1024 * 1024 * 1024;
3171 max_chunk_size = 10 * max_stripe_size;
3172 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003173 /* for larger filesystems, use larger metadata chunks */
3174 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3175 max_stripe_size = 1024 * 1024 * 1024;
3176 else
3177 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003178 max_chunk_size = max_stripe_size;
3179 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003180 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003181 max_chunk_size = 2 * max_stripe_size;
3182 } else {
3183 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3184 type);
3185 BUG_ON(1);
3186 }
3187
3188 /* we don't want a chunk larger than 10% of writeable space */
3189 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3190 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003191
3192 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3193 GFP_NOFS);
3194 if (!devices_info)
3195 return -ENOMEM;
3196
Arne Jansen73c5de02011-04-12 12:07:57 +02003197 cur = fs_devices->alloc_list.next;
3198
3199 /*
3200 * in the first pass through the devices list, we gather information
3201 * about the available holes on each device.
3202 */
3203 ndevs = 0;
3204 while (cur != &fs_devices->alloc_list) {
3205 struct btrfs_device *device;
3206 u64 max_avail;
3207 u64 dev_offset;
3208
3209 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3210
3211 cur = cur->next;
3212
3213 if (!device->writeable) {
3214 printk(KERN_ERR
3215 "btrfs: read-only device in alloc_list\n");
3216 WARN_ON(1);
3217 continue;
3218 }
3219
3220 if (!device->in_fs_metadata)
3221 continue;
3222
3223 if (device->total_bytes > device->bytes_used)
3224 total_avail = device->total_bytes - device->bytes_used;
3225 else
3226 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003227
3228 /* If there is no space on this device, skip it. */
3229 if (total_avail == 0)
3230 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003231
Li Zefan125ccb02011-12-08 15:07:24 +08003232 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003233 max_stripe_size * dev_stripes,
3234 &dev_offset, &max_avail);
3235 if (ret && ret != -ENOSPC)
3236 goto error;
3237
3238 if (ret == 0)
3239 max_avail = max_stripe_size * dev_stripes;
3240
3241 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3242 continue;
3243
3244 devices_info[ndevs].dev_offset = dev_offset;
3245 devices_info[ndevs].max_avail = max_avail;
3246 devices_info[ndevs].total_avail = total_avail;
3247 devices_info[ndevs].dev = device;
3248 ++ndevs;
3249 }
3250
3251 /*
3252 * now sort the devices by hole size / available space
3253 */
3254 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3255 btrfs_cmp_device_info, NULL);
3256
3257 /* round down to number of usable stripes */
3258 ndevs -= ndevs % devs_increment;
3259
3260 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3261 ret = -ENOSPC;
3262 goto error;
3263 }
3264
3265 if (devs_max && ndevs > devs_max)
3266 ndevs = devs_max;
3267 /*
3268 * the primary goal is to maximize the number of stripes, so use as many
3269 * devices as possible, even if the stripes are not maximum sized.
3270 */
3271 stripe_size = devices_info[ndevs-1].max_avail;
3272 num_stripes = ndevs * dev_stripes;
3273
3274 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3275 stripe_size = max_chunk_size * ncopies;
3276 do_div(stripe_size, num_stripes);
3277 }
3278
3279 do_div(stripe_size, dev_stripes);
3280 do_div(stripe_size, BTRFS_STRIPE_LEN);
3281 stripe_size *= BTRFS_STRIPE_LEN;
3282
Miao Xieb2117a32011-01-05 10:07:28 +00003283 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3284 if (!map) {
3285 ret = -ENOMEM;
3286 goto error;
3287 }
3288 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003289
Arne Jansen73c5de02011-04-12 12:07:57 +02003290 for (i = 0; i < ndevs; ++i) {
3291 for (j = 0; j < dev_stripes; ++j) {
3292 int s = i * dev_stripes + j;
3293 map->stripes[s].dev = devices_info[i].dev;
3294 map->stripes[s].physical = devices_info[i].dev_offset +
3295 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003296 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003297 }
Chris Mason593060d2008-03-25 16:50:33 -04003298 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003299 map->stripe_len = BTRFS_STRIPE_LEN;
3300 map->io_align = BTRFS_STRIPE_LEN;
3301 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003302 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003303 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003304
Yan Zheng2b820322008-11-17 21:11:30 -05003305 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003306 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003307
Arne Jansen73c5de02011-04-12 12:07:57 +02003308 *stripe_size_out = stripe_size;
3309 *num_bytes_out = num_bytes;
3310
3311 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003312
David Sterba172ddd62011-04-21 00:48:27 +02003313 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003314 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003315 ret = -ENOMEM;
3316 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003317 }
Chris Mason0b86a832008-03-24 15:01:56 -04003318 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003319 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003320 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003321 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003322 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003323
Chris Mason0b86a832008-03-24 15:01:56 -04003324 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003325 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003326 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003327 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04003328 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003329 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05003330
3331 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3332 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003333 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05003334 BUG_ON(ret);
3335
Arne Jansen73c5de02011-04-12 12:07:57 +02003336 for (i = 0; i < map->num_stripes; ++i) {
3337 struct btrfs_device *device;
3338 u64 dev_offset;
3339
3340 device = map->stripes[i].dev;
3341 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003342
3343 ret = btrfs_alloc_dev_extent(trans, device,
3344 info->chunk_root->root_key.objectid,
3345 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003346 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003347 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003348 }
3349
Miao Xieb2117a32011-01-05 10:07:28 +00003350 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003351 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003352
3353error:
3354 kfree(map);
3355 kfree(devices_info);
3356 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003357}
3358
3359static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3360 struct btrfs_root *extent_root,
3361 struct map_lookup *map, u64 chunk_offset,
3362 u64 chunk_size, u64 stripe_size)
3363{
3364 u64 dev_offset;
3365 struct btrfs_key key;
3366 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3367 struct btrfs_device *device;
3368 struct btrfs_chunk *chunk;
3369 struct btrfs_stripe *stripe;
3370 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3371 int index = 0;
3372 int ret;
3373
3374 chunk = kzalloc(item_size, GFP_NOFS);
3375 if (!chunk)
3376 return -ENOMEM;
3377
3378 index = 0;
3379 while (index < map->num_stripes) {
3380 device = map->stripes[index].dev;
3381 device->bytes_used += stripe_size;
3382 ret = btrfs_update_device(trans, device);
3383 BUG_ON(ret);
3384 index++;
3385 }
3386
Josef Bacik2bf64752011-09-26 17:12:22 -04003387 spin_lock(&extent_root->fs_info->free_chunk_lock);
3388 extent_root->fs_info->free_chunk_space -= (stripe_size *
3389 map->num_stripes);
3390 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3391
Yan Zheng2b820322008-11-17 21:11:30 -05003392 index = 0;
3393 stripe = &chunk->stripe;
3394 while (index < map->num_stripes) {
3395 device = map->stripes[index].dev;
3396 dev_offset = map->stripes[index].physical;
3397
3398 btrfs_set_stack_stripe_devid(stripe, device->devid);
3399 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3400 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3401 stripe++;
3402 index++;
3403 }
3404
3405 btrfs_set_stack_chunk_length(chunk, chunk_size);
3406 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3407 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3408 btrfs_set_stack_chunk_type(chunk, map->type);
3409 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3410 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3411 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3412 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3413 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3414
3415 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3416 key.type = BTRFS_CHUNK_ITEM_KEY;
3417 key.offset = chunk_offset;
3418
3419 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003420
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003421 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3422 /*
3423 * TODO: Cleanup of inserted chunk root in case of
3424 * failure.
3425 */
Li Zefan125ccb02011-12-08 15:07:24 +08003426 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003427 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003428 }
liubo1abe9b82011-03-24 11:18:59 +00003429
Yan Zheng2b820322008-11-17 21:11:30 -05003430 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003431 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003432}
3433
3434/*
3435 * Chunk allocation falls into two parts. The first part does works
3436 * that make the new allocated chunk useable, but not do any operation
3437 * that modifies the chunk tree. The second part does the works that
3438 * require modifying the chunk tree. This division is important for the
3439 * bootstrap process of adding storage to a seed btrfs.
3440 */
3441int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3442 struct btrfs_root *extent_root, u64 type)
3443{
3444 u64 chunk_offset;
3445 u64 chunk_size;
3446 u64 stripe_size;
3447 struct map_lookup *map;
3448 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3449 int ret;
3450
3451 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3452 &chunk_offset);
3453 if (ret)
3454 return ret;
3455
3456 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3457 &stripe_size, chunk_offset, type);
3458 if (ret)
3459 return ret;
3460
3461 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3462 chunk_size, stripe_size);
3463 BUG_ON(ret);
3464 return 0;
3465}
3466
Chris Masond3977122009-01-05 21:25:51 -05003467static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003468 struct btrfs_root *root,
3469 struct btrfs_device *device)
3470{
3471 u64 chunk_offset;
3472 u64 sys_chunk_offset;
3473 u64 chunk_size;
3474 u64 sys_chunk_size;
3475 u64 stripe_size;
3476 u64 sys_stripe_size;
3477 u64 alloc_profile;
3478 struct map_lookup *map;
3479 struct map_lookup *sys_map;
3480 struct btrfs_fs_info *fs_info = root->fs_info;
3481 struct btrfs_root *extent_root = fs_info->extent_root;
3482 int ret;
3483
3484 ret = find_next_chunk(fs_info->chunk_root,
3485 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003486 if (ret)
3487 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003488
3489 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003490 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003491 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3492
3493 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3494 &stripe_size, chunk_offset, alloc_profile);
3495 BUG_ON(ret);
3496
3497 sys_chunk_offset = chunk_offset + chunk_size;
3498
3499 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003500 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003501 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3502
3503 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3504 &sys_chunk_size, &sys_stripe_size,
3505 sys_chunk_offset, alloc_profile);
3506 BUG_ON(ret);
3507
3508 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3509 BUG_ON(ret);
3510
3511 /*
3512 * Modifying chunk tree needs allocating new blocks from both
3513 * system block group and metadata block group. So we only can
3514 * do operations require modifying the chunk tree after both
3515 * block groups were created.
3516 */
3517 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3518 chunk_size, stripe_size);
3519 BUG_ON(ret);
3520
3521 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3522 sys_chunk_offset, sys_chunk_size,
3523 sys_stripe_size);
3524 BUG_ON(ret);
3525 return 0;
3526}
3527
3528int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3529{
3530 struct extent_map *em;
3531 struct map_lookup *map;
3532 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3533 int readonly = 0;
3534 int i;
3535
Chris Mason890871b2009-09-02 16:24:52 -04003536 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003537 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003538 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003539 if (!em)
3540 return 1;
3541
Josef Bacikf48b9072010-01-27 02:07:59 +00003542 if (btrfs_test_opt(root, DEGRADED)) {
3543 free_extent_map(em);
3544 return 0;
3545 }
3546
Yan Zheng2b820322008-11-17 21:11:30 -05003547 map = (struct map_lookup *)em->bdev;
3548 for (i = 0; i < map->num_stripes; i++) {
3549 if (!map->stripes[i].dev->writeable) {
3550 readonly = 1;
3551 break;
3552 }
3553 }
3554 free_extent_map(em);
3555 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003556}
3557
3558void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3559{
David Sterbaa8067e02011-04-21 00:34:43 +02003560 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003561}
3562
3563void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3564{
3565 struct extent_map *em;
3566
Chris Masond3977122009-01-05 21:25:51 -05003567 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003568 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003569 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3570 if (em)
3571 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003572 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003573 if (!em)
3574 break;
3575 kfree(em->bdev);
3576 /* once for us */
3577 free_extent_map(em);
3578 /* once for the tree */
3579 free_extent_map(em);
3580 }
3581}
3582
Chris Masonf1885912008-04-09 16:28:12 -04003583int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3584{
3585 struct extent_map *em;
3586 struct map_lookup *map;
3587 struct extent_map_tree *em_tree = &map_tree->map_tree;
3588 int ret;
3589
Chris Mason890871b2009-09-02 16:24:52 -04003590 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003591 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003592 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003593 BUG_ON(!em);
3594
3595 BUG_ON(em->start > logical || em->start + em->len < logical);
3596 map = (struct map_lookup *)em->bdev;
3597 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3598 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003599 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3600 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003601 else
3602 ret = 1;
3603 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003604 return ret;
3605}
3606
Chris Masondfe25022008-05-13 13:46:40 -04003607static int find_live_mirror(struct map_lookup *map, int first, int num,
3608 int optimal)
3609{
3610 int i;
3611 if (map->stripes[optimal].dev->bdev)
3612 return optimal;
3613 for (i = first; i < first + num; i++) {
3614 if (map->stripes[i].dev->bdev)
3615 return i;
3616 }
3617 /* we couldn't find one that doesn't fail. Just return something
3618 * and the io error handling code will clean up eventually
3619 */
3620 return optimal;
3621}
3622
Chris Masonf2d8d742008-04-21 10:03:05 -04003623static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3624 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003625 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003626 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003627{
3628 struct extent_map *em;
3629 struct map_lookup *map;
3630 struct extent_map_tree *em_tree = &map_tree->map_tree;
3631 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003632 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003633 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003634 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003635 u64 stripe_nr_orig;
3636 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003637 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003638 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003639 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003640 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003641 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003642 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003643
Chris Mason890871b2009-09-02 16:24:52 -04003644 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003645 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003646 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003647
Chris Mason3b951512008-04-17 11:29:12 -04003648 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003649 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3650 (unsigned long long)logical,
3651 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003652 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003653 }
Chris Mason0b86a832008-03-24 15:01:56 -04003654
3655 BUG_ON(em->start > logical || em->start + em->len < logical);
3656 map = (struct map_lookup *)em->bdev;
3657 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003658
Chris Masonf1885912008-04-09 16:28:12 -04003659 if (mirror_num > map->num_stripes)
3660 mirror_num = 0;
3661
Chris Mason593060d2008-03-25 16:50:33 -04003662 stripe_nr = offset;
3663 /*
3664 * stripe_nr counts the total number of stripes we have to stride
3665 * to get to this block
3666 */
3667 do_div(stripe_nr, map->stripe_len);
3668
3669 stripe_offset = stripe_nr * map->stripe_len;
3670 BUG_ON(offset < stripe_offset);
3671
3672 /* stripe_offset is the offset of this block in its stripe*/
3673 stripe_offset = offset - stripe_offset;
3674
Li Dongyangfce3bb92011-03-24 10:24:26 +00003675 if (rw & REQ_DISCARD)
3676 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003677 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003678 /* we limit the length of each bio to what fits in a stripe */
3679 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003680 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003681 } else {
3682 *length = em->len - offset;
3683 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003684
Jan Schmidta1d3c472011-08-04 17:15:33 +02003685 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003686 goto out;
3687
Chris Masonf2d8d742008-04-21 10:03:05 -04003688 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003689 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003690 stripe_nr_orig = stripe_nr;
3691 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3692 (~(map->stripe_len - 1));
3693 do_div(stripe_nr_end, map->stripe_len);
3694 stripe_end_offset = stripe_nr_end * map->stripe_len -
3695 (offset + *length);
3696 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3697 if (rw & REQ_DISCARD)
3698 num_stripes = min_t(u64, map->num_stripes,
3699 stripe_nr_end - stripe_nr_orig);
3700 stripe_index = do_div(stripe_nr, map->num_stripes);
3701 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003702 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003703 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003704 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003705 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003706 else {
3707 stripe_index = find_live_mirror(map, 0,
3708 map->num_stripes,
3709 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003710 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003711 }
Chris Mason2fff7342008-04-29 14:12:09 -04003712
Chris Mason611f0e02008-04-03 16:29:03 -04003713 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003714 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003715 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003716 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003717 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003718 } else {
3719 mirror_num = 1;
3720 }
Chris Mason2fff7342008-04-29 14:12:09 -04003721
Chris Mason321aecc2008-04-16 10:49:51 -04003722 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3723 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003724
3725 stripe_index = do_div(stripe_nr, factor);
3726 stripe_index *= map->sub_stripes;
3727
Jens Axboe7eaceac2011-03-10 08:52:07 +01003728 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003729 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003730 else if (rw & REQ_DISCARD)
3731 num_stripes = min_t(u64, map->sub_stripes *
3732 (stripe_nr_end - stripe_nr_orig),
3733 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003734 else if (mirror_num)
3735 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003736 else {
3737 stripe_index = find_live_mirror(map, stripe_index,
3738 map->sub_stripes, stripe_index +
3739 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003740 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003741 }
Chris Mason8790d502008-04-03 16:29:03 -04003742 } else {
3743 /*
3744 * after this do_div call, stripe_nr is the number of stripes
3745 * on this device we have to walk to find the data, and
3746 * stripe_index is the number of our device in the stripe array
3747 */
3748 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003749 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003750 }
Chris Mason593060d2008-03-25 16:50:33 -04003751 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003752
Li Zefande11cc12011-12-01 12:55:47 +08003753 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3754 if (!bbio) {
3755 ret = -ENOMEM;
3756 goto out;
3757 }
3758 atomic_set(&bbio->error, 0);
3759
Li Dongyangfce3bb92011-03-24 10:24:26 +00003760 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003761 int factor = 0;
3762 int sub_stripes = 0;
3763 u64 stripes_per_dev = 0;
3764 u32 remaining_stripes = 0;
3765
3766 if (map->type &
3767 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3768 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3769 sub_stripes = 1;
3770 else
3771 sub_stripes = map->sub_stripes;
3772
3773 factor = map->num_stripes / sub_stripes;
3774 stripes_per_dev = div_u64_rem(stripe_nr_end -
3775 stripe_nr_orig,
3776 factor,
3777 &remaining_stripes);
3778 }
3779
Li Dongyangfce3bb92011-03-24 10:24:26 +00003780 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003781 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003782 map->stripes[stripe_index].physical +
3783 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003784 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003785
Li Zefanec9ef7a2011-12-01 14:06:42 +08003786 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3787 BTRFS_BLOCK_GROUP_RAID10)) {
3788 bbio->stripes[i].length = stripes_per_dev *
3789 map->stripe_len;
3790 if (i / sub_stripes < remaining_stripes)
3791 bbio->stripes[i].length +=
3792 map->stripe_len;
3793 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003794 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003795 stripe_offset;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003796 if ((i / sub_stripes + 1) %
3797 sub_stripes == remaining_stripes)
3798 bbio->stripes[i].length -=
3799 stripe_end_offset;
3800 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003801 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003802 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003803 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003804
3805 stripe_index++;
3806 if (stripe_index == map->num_stripes) {
3807 /* This could only happen for RAID0/10 */
3808 stripe_index = 0;
3809 stripe_nr++;
3810 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003811 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003812 } else {
3813 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003814 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003815 map->stripes[stripe_index].physical +
3816 stripe_offset +
3817 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003818 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003819 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003820 stripe_index++;
3821 }
Chris Mason593060d2008-03-25 16:50:33 -04003822 }
Li Zefande11cc12011-12-01 12:55:47 +08003823
3824 if (rw & REQ_WRITE) {
3825 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3826 BTRFS_BLOCK_GROUP_RAID10 |
3827 BTRFS_BLOCK_GROUP_DUP)) {
3828 max_errors = 1;
3829 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003830 }
Li Zefande11cc12011-12-01 12:55:47 +08003831
3832 *bbio_ret = bbio;
3833 bbio->num_stripes = num_stripes;
3834 bbio->max_errors = max_errors;
3835 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003836out:
Chris Mason0b86a832008-03-24 15:01:56 -04003837 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003838 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003839}
3840
Chris Masonf2d8d742008-04-21 10:03:05 -04003841int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3842 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003843 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003844{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003845 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003846 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003847}
3848
Yan Zhenga512bbf2008-12-08 16:46:26 -05003849int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3850 u64 chunk_start, u64 physical, u64 devid,
3851 u64 **logical, int *naddrs, int *stripe_len)
3852{
3853 struct extent_map_tree *em_tree = &map_tree->map_tree;
3854 struct extent_map *em;
3855 struct map_lookup *map;
3856 u64 *buf;
3857 u64 bytenr;
3858 u64 length;
3859 u64 stripe_nr;
3860 int i, j, nr = 0;
3861
Chris Mason890871b2009-09-02 16:24:52 -04003862 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003863 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003864 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003865
3866 BUG_ON(!em || em->start != chunk_start);
3867 map = (struct map_lookup *)em->bdev;
3868
3869 length = em->len;
3870 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3871 do_div(length, map->num_stripes / map->sub_stripes);
3872 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3873 do_div(length, map->num_stripes);
3874
3875 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3876 BUG_ON(!buf);
3877
3878 for (i = 0; i < map->num_stripes; i++) {
3879 if (devid && map->stripes[i].dev->devid != devid)
3880 continue;
3881 if (map->stripes[i].physical > physical ||
3882 map->stripes[i].physical + length <= physical)
3883 continue;
3884
3885 stripe_nr = physical - map->stripes[i].physical;
3886 do_div(stripe_nr, map->stripe_len);
3887
3888 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3889 stripe_nr = stripe_nr * map->num_stripes + i;
3890 do_div(stripe_nr, map->sub_stripes);
3891 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3892 stripe_nr = stripe_nr * map->num_stripes + i;
3893 }
3894 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003895 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003896 for (j = 0; j < nr; j++) {
3897 if (buf[j] == bytenr)
3898 break;
3899 }
Chris Mason934d3752008-12-08 16:43:10 -05003900 if (j == nr) {
3901 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003902 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003903 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003904 }
3905
Yan Zhenga512bbf2008-12-08 16:46:26 -05003906 *logical = buf;
3907 *naddrs = nr;
3908 *stripe_len = map->stripe_len;
3909
3910 free_extent_map(em);
3911 return 0;
3912}
3913
Jan Schmidta1d3c472011-08-04 17:15:33 +02003914static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003915{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003916 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003917 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003918
Chris Mason8790d502008-04-03 16:29:03 -04003919 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003920 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003921
Jan Schmidta1d3c472011-08-04 17:15:33 +02003922 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003923 is_orig_bio = 1;
3924
Jan Schmidta1d3c472011-08-04 17:15:33 +02003925 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003926 if (!is_orig_bio) {
3927 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003928 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003929 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003930 bio->bi_private = bbio->private;
3931 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003932 bio->bi_bdev = (struct block_device *)
3933 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003934 /* only send an error to the higher layers if it is
3935 * beyond the tolerance of the multi-bio
3936 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003937 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003938 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003939 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003940 /*
3941 * this bio is actually up to date, we didn't
3942 * go over the max number of errors
3943 */
3944 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003945 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003946 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003947 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003948
3949 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003950 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003951 bio_put(bio);
3952 }
Chris Mason8790d502008-04-03 16:29:03 -04003953}
3954
Chris Mason8b712842008-06-11 16:50:36 -04003955struct async_sched {
3956 struct bio *bio;
3957 int rw;
3958 struct btrfs_fs_info *info;
3959 struct btrfs_work work;
3960};
3961
3962/*
3963 * see run_scheduled_bios for a description of why bios are collected for
3964 * async submit.
3965 *
3966 * This will add one bio to the pending list for a device and make sure
3967 * the work struct is scheduled.
3968 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003969static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003970 struct btrfs_device *device,
3971 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003972{
3973 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003974 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003975
3976 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003977 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04003978 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01003979 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04003980 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003981 return;
Chris Mason8b712842008-06-11 16:50:36 -04003982 }
3983
3984 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003985 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003986 * higher layers. Otherwise, the async bio makes it appear we have
3987 * made progress against dirty pages when we've really just put it
3988 * on a queue for later
3989 */
Chris Mason0986fe92008-08-15 15:34:15 -04003990 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04003991 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003992 bio->bi_next = NULL;
3993 bio->bi_rw |= rw;
3994
3995 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003996 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003997 pending_bios = &device->pending_sync_bios;
3998 else
3999 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004000
Chris Masonffbd5172009-04-20 15:50:09 -04004001 if (pending_bios->tail)
4002 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004003
Chris Masonffbd5172009-04-20 15:50:09 -04004004 pending_bios->tail = bio;
4005 if (!pending_bios->head)
4006 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004007 if (device->running_pending)
4008 should_queue = 0;
4009
4010 spin_unlock(&device->io_lock);
4011
4012 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004013 btrfs_queue_worker(&root->fs_info->submit_workers,
4014 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004015}
4016
Chris Masonf1885912008-04-09 16:28:12 -04004017int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004018 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004019{
4020 struct btrfs_mapping_tree *map_tree;
4021 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004022 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004023 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004024 u64 length = 0;
4025 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004026 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004027 int dev_nr = 0;
4028 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004029 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004030
Chris Masonf2d8d742008-04-21 10:03:05 -04004031 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004032 map_tree = &root->fs_info->mapping_tree;
4033 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004034
Jan Schmidta1d3c472011-08-04 17:15:33 +02004035 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004036 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04004037 BUG_ON(ret);
4038
Jan Schmidta1d3c472011-08-04 17:15:33 +02004039 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004040 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004041 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4042 "len %llu\n", (unsigned long long)logical,
4043 (unsigned long long)length,
4044 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004045 BUG();
4046 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004047
4048 bbio->orig_bio = first_bio;
4049 bbio->private = first_bio->bi_private;
4050 bbio->end_io = first_bio->bi_end_io;
4051 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004052
Chris Masond3977122009-01-05 21:25:51 -05004053 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004054 if (dev_nr < total_devs - 1) {
4055 bio = bio_clone(first_bio, GFP_NOFS);
4056 BUG_ON(!bio);
4057 } else {
4058 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004059 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004060 bio->bi_private = bbio;
4061 bio->bi_end_io = btrfs_end_bio;
4062 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4063 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004064 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004065 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4066 "(%s id %llu), size=%u\n", rw,
4067 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4068 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004069 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004070 if (async_submit)
4071 schedule_bio(root, dev, rw, bio);
4072 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004073 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004074 } else {
4075 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4076 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004077 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004078 }
Chris Mason8790d502008-04-03 16:29:03 -04004079 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004080 }
Chris Mason0b86a832008-03-24 15:01:56 -04004081 return 0;
4082}
4083
Chris Masona4437552008-04-18 10:29:38 -04004084struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004085 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004086{
Yan Zheng2b820322008-11-17 21:11:30 -05004087 struct btrfs_device *device;
4088 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004089
Yan Zheng2b820322008-11-17 21:11:30 -05004090 cur_devices = root->fs_info->fs_devices;
4091 while (cur_devices) {
4092 if (!fsid ||
4093 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4094 device = __find_device(&cur_devices->devices,
4095 devid, uuid);
4096 if (device)
4097 return device;
4098 }
4099 cur_devices = cur_devices->seed;
4100 }
4101 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004102}
4103
Chris Masondfe25022008-05-13 13:46:40 -04004104static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4105 u64 devid, u8 *dev_uuid)
4106{
4107 struct btrfs_device *device;
4108 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4109
4110 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004111 if (!device)
4112 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004113 list_add(&device->dev_list,
4114 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004115 device->dev_root = root->fs_info->dev_root;
4116 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004117 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004118 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004119 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004120 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004121 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004122 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004123 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004124 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4125 return device;
4126}
4127
Chris Mason0b86a832008-03-24 15:01:56 -04004128static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4129 struct extent_buffer *leaf,
4130 struct btrfs_chunk *chunk)
4131{
4132 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4133 struct map_lookup *map;
4134 struct extent_map *em;
4135 u64 logical;
4136 u64 length;
4137 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004138 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004139 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004140 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004141 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004142
Chris Masone17cade2008-04-15 15:41:47 -04004143 logical = key->offset;
4144 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004145
Chris Mason890871b2009-09-02 16:24:52 -04004146 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004147 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004148 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004149
4150 /* already mapped? */
4151 if (em && em->start <= logical && em->start + em->len > logical) {
4152 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004153 return 0;
4154 } else if (em) {
4155 free_extent_map(em);
4156 }
Chris Mason0b86a832008-03-24 15:01:56 -04004157
David Sterba172ddd62011-04-21 00:48:27 +02004158 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004159 if (!em)
4160 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004161 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4162 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004163 if (!map) {
4164 free_extent_map(em);
4165 return -ENOMEM;
4166 }
4167
4168 em->bdev = (struct block_device *)map;
4169 em->start = logical;
4170 em->len = length;
4171 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004172 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004173
Chris Mason593060d2008-03-25 16:50:33 -04004174 map->num_stripes = num_stripes;
4175 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4176 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4177 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4178 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4179 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004180 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004181 for (i = 0; i < num_stripes; i++) {
4182 map->stripes[i].physical =
4183 btrfs_stripe_offset_nr(leaf, chunk, i);
4184 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004185 read_extent_buffer(leaf, uuid, (unsigned long)
4186 btrfs_stripe_dev_uuid_nr(chunk, i),
4187 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004188 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4189 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004190 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004191 kfree(map);
4192 free_extent_map(em);
4193 return -EIO;
4194 }
Chris Masondfe25022008-05-13 13:46:40 -04004195 if (!map->stripes[i].dev) {
4196 map->stripes[i].dev =
4197 add_missing_dev(root, devid, uuid);
4198 if (!map->stripes[i].dev) {
4199 kfree(map);
4200 free_extent_map(em);
4201 return -EIO;
4202 }
4203 }
4204 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004205 }
4206
Chris Mason890871b2009-09-02 16:24:52 -04004207 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004208 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004209 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04004210 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04004211 free_extent_map(em);
4212
4213 return 0;
4214}
4215
Jeff Mahoney143bede2012-03-01 14:56:26 +01004216static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004217 struct btrfs_dev_item *dev_item,
4218 struct btrfs_device *device)
4219{
4220 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004221
4222 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004223 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4224 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004225 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4226 device->type = btrfs_device_type(leaf, dev_item);
4227 device->io_align = btrfs_device_io_align(leaf, dev_item);
4228 device->io_width = btrfs_device_io_width(leaf, dev_item);
4229 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004230
4231 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004232 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004233}
4234
Yan Zheng2b820322008-11-17 21:11:30 -05004235static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4236{
4237 struct btrfs_fs_devices *fs_devices;
4238 int ret;
4239
Li Zefanb367e472011-12-07 11:38:24 +08004240 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004241
4242 fs_devices = root->fs_info->fs_devices->seed;
4243 while (fs_devices) {
4244 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4245 ret = 0;
4246 goto out;
4247 }
4248 fs_devices = fs_devices->seed;
4249 }
4250
4251 fs_devices = find_fsid(fsid);
4252 if (!fs_devices) {
4253 ret = -ENOENT;
4254 goto out;
4255 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004256
4257 fs_devices = clone_fs_devices(fs_devices);
4258 if (IS_ERR(fs_devices)) {
4259 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004260 goto out;
4261 }
4262
Christoph Hellwig97288f22008-12-02 06:36:09 -05004263 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004264 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004265 if (ret)
4266 goto out;
4267
4268 if (!fs_devices->seeding) {
4269 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004270 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004271 ret = -EINVAL;
4272 goto out;
4273 }
4274
4275 fs_devices->seed = root->fs_info->fs_devices->seed;
4276 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004277out:
Yan Zheng2b820322008-11-17 21:11:30 -05004278 return ret;
4279}
4280
Chris Mason0d81ba52008-03-24 15:02:07 -04004281static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004282 struct extent_buffer *leaf,
4283 struct btrfs_dev_item *dev_item)
4284{
4285 struct btrfs_device *device;
4286 u64 devid;
4287 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004288 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004289 u8 dev_uuid[BTRFS_UUID_SIZE];
4290
Chris Mason0b86a832008-03-24 15:01:56 -04004291 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004292 read_extent_buffer(leaf, dev_uuid,
4293 (unsigned long)btrfs_device_uuid(dev_item),
4294 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004295 read_extent_buffer(leaf, fs_uuid,
4296 (unsigned long)btrfs_device_fsid(dev_item),
4297 BTRFS_UUID_SIZE);
4298
4299 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4300 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004301 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004302 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004303 }
4304
4305 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4306 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004307 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004308 return -EIO;
4309
4310 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004311 printk(KERN_WARNING "warning devid %llu missing\n",
4312 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004313 device = add_missing_dev(root, devid, dev_uuid);
4314 if (!device)
4315 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004316 } else if (!device->missing) {
4317 /*
4318 * this happens when a device that was properly setup
4319 * in the device info lists suddenly goes bad.
4320 * device->bdev is NULL, and so we have to set
4321 * device->missing to one here
4322 */
4323 root->fs_info->fs_devices->missing_devices++;
4324 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004325 }
4326 }
4327
4328 if (device->fs_devices != root->fs_info->fs_devices) {
4329 BUG_ON(device->writeable);
4330 if (device->generation !=
4331 btrfs_device_generation(leaf, dev_item))
4332 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004333 }
Chris Mason0b86a832008-03-24 15:01:56 -04004334
4335 fill_device_from_item(leaf, dev_item, device);
4336 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004337 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004338 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004339 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004340 spin_lock(&root->fs_info->free_chunk_lock);
4341 root->fs_info->free_chunk_space += device->total_bytes -
4342 device->bytes_used;
4343 spin_unlock(&root->fs_info->free_chunk_lock);
4344 }
Chris Mason0b86a832008-03-24 15:01:56 -04004345 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004346 return ret;
4347}
4348
Yan Zhenge4404d62008-12-12 10:03:26 -05004349int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004350{
David Sterba6c417612011-04-13 15:41:04 +02004351 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004352 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004353 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004354 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004355 u8 *ptr;
4356 unsigned long sb_ptr;
4357 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004358 u32 num_stripes;
4359 u32 array_size;
4360 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004361 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004362 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004363
Yan Zhenge4404d62008-12-12 10:03:26 -05004364 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004365 BTRFS_SUPER_INFO_SIZE);
4366 if (!sb)
4367 return -ENOMEM;
4368 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004369 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004370 /*
4371 * The sb extent buffer is artifical and just used to read the system array.
4372 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4373 * pages up-to-date when the page is larger: extent does not cover the
4374 * whole page and consequently check_page_uptodate does not find all
4375 * the page's extents up-to-date (the hole beyond sb),
4376 * write_extent_buffer then triggers a WARN_ON.
4377 *
4378 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4379 * but sb spans only this function. Add an explicit SetPageUptodate call
4380 * to silence the warning eg. on PowerPC 64.
4381 */
4382 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
4383 SetPageUptodate(sb->first_page);
Chris Mason4008c042009-02-12 14:09:45 -05004384
Chris Masona061fc82008-05-07 11:43:44 -04004385 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004386 array_size = btrfs_super_sys_array_size(super_copy);
4387
Chris Mason0b86a832008-03-24 15:01:56 -04004388 ptr = super_copy->sys_chunk_array;
4389 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4390 cur = 0;
4391
4392 while (cur < array_size) {
4393 disk_key = (struct btrfs_disk_key *)ptr;
4394 btrfs_disk_key_to_cpu(&key, disk_key);
4395
Chris Masona061fc82008-05-07 11:43:44 -04004396 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004397 sb_ptr += len;
4398 cur += len;
4399
Chris Mason0d81ba52008-03-24 15:02:07 -04004400 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004401 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004402 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004403 if (ret)
4404 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004405 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4406 len = btrfs_chunk_item_size(num_stripes);
4407 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004408 ret = -EIO;
4409 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004410 }
4411 ptr += len;
4412 sb_ptr += len;
4413 cur += len;
4414 }
Chris Masona061fc82008-05-07 11:43:44 -04004415 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004416 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004417}
4418
4419int btrfs_read_chunk_tree(struct btrfs_root *root)
4420{
4421 struct btrfs_path *path;
4422 struct extent_buffer *leaf;
4423 struct btrfs_key key;
4424 struct btrfs_key found_key;
4425 int ret;
4426 int slot;
4427
4428 root = root->fs_info->chunk_root;
4429
4430 path = btrfs_alloc_path();
4431 if (!path)
4432 return -ENOMEM;
4433
Li Zefanb367e472011-12-07 11:38:24 +08004434 mutex_lock(&uuid_mutex);
4435 lock_chunks(root);
4436
Chris Mason0b86a832008-03-24 15:01:56 -04004437 /* first we search for all of the device items, and then we
4438 * read in all of the chunk items. This way we can create chunk
4439 * mappings that reference all of the devices that are afound
4440 */
4441 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4442 key.offset = 0;
4443 key.type = 0;
4444again:
4445 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004446 if (ret < 0)
4447 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004448 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004449 leaf = path->nodes[0];
4450 slot = path->slots[0];
4451 if (slot >= btrfs_header_nritems(leaf)) {
4452 ret = btrfs_next_leaf(root, path);
4453 if (ret == 0)
4454 continue;
4455 if (ret < 0)
4456 goto error;
4457 break;
4458 }
4459 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4460 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4461 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4462 break;
4463 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4464 struct btrfs_dev_item *dev_item;
4465 dev_item = btrfs_item_ptr(leaf, slot,
4466 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004467 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004468 if (ret)
4469 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004470 }
4471 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4472 struct btrfs_chunk *chunk;
4473 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4474 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004475 if (ret)
4476 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004477 }
4478 path->slots[0]++;
4479 }
4480 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4481 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004482 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004483 goto again;
4484 }
Chris Mason0b86a832008-03-24 15:01:56 -04004485 ret = 0;
4486error:
Li Zefanb367e472011-12-07 11:38:24 +08004487 unlock_chunks(root);
4488 mutex_unlock(&uuid_mutex);
4489
Yan Zheng2b820322008-11-17 21:11:30 -05004490 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004491 return ret;
4492}