blob: f002973959d0053a8f1e8b441e43419b43dc558c [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 Mason492bb6de2008-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 Masond85c8a6f2011-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 Masonc289811c2009-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));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001078 if (ret)
1079 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001080
1081 leaf = path->nodes[0];
1082 extent = btrfs_item_ptr(leaf, path->slots[0],
1083 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001084 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1085 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1086 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1087
1088 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1089 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1090 BTRFS_UUID_SIZE);
1091
Chris Mason0b86a832008-03-24 15:01:56 -04001092 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1093 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001094out:
Chris Mason0b86a832008-03-24 15:01:56 -04001095 btrfs_free_path(path);
1096 return ret;
1097}
1098
Chris Masona1b32a52008-09-05 16:09:51 -04001099static noinline int find_next_chunk(struct btrfs_root *root,
1100 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001101{
1102 struct btrfs_path *path;
1103 int ret;
1104 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001105 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001106 struct btrfs_key found_key;
1107
1108 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001109 if (!path)
1110 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001111
Chris Masone17cade2008-04-15 15:41:47 -04001112 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001113 key.offset = (u64)-1;
1114 key.type = BTRFS_CHUNK_ITEM_KEY;
1115
1116 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1117 if (ret < 0)
1118 goto error;
1119
1120 BUG_ON(ret == 0);
1121
1122 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1123 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001124 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001125 } else {
1126 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1127 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001128 if (found_key.objectid != objectid)
1129 *offset = 0;
1130 else {
1131 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1132 struct btrfs_chunk);
1133 *offset = found_key.offset +
1134 btrfs_chunk_length(path->nodes[0], chunk);
1135 }
Chris Mason0b86a832008-03-24 15:01:56 -04001136 }
1137 ret = 0;
1138error:
1139 btrfs_free_path(path);
1140 return ret;
1141}
1142
Yan Zheng2b820322008-11-17 21:11:30 -05001143static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001144{
1145 int ret;
1146 struct btrfs_key key;
1147 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001148 struct btrfs_path *path;
1149
1150 root = root->fs_info->chunk_root;
1151
1152 path = btrfs_alloc_path();
1153 if (!path)
1154 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001155
1156 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1157 key.type = BTRFS_DEV_ITEM_KEY;
1158 key.offset = (u64)-1;
1159
1160 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1161 if (ret < 0)
1162 goto error;
1163
1164 BUG_ON(ret == 0);
1165
1166 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1167 BTRFS_DEV_ITEM_KEY);
1168 if (ret) {
1169 *objectid = 1;
1170 } else {
1171 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1172 path->slots[0]);
1173 *objectid = found_key.offset + 1;
1174 }
1175 ret = 0;
1176error:
Yan Zheng2b820322008-11-17 21:11:30 -05001177 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001178 return ret;
1179}
1180
1181/*
1182 * the device information is stored in the chunk root
1183 * the btrfs_device struct should be fully filled in
1184 */
1185int btrfs_add_device(struct btrfs_trans_handle *trans,
1186 struct btrfs_root *root,
1187 struct btrfs_device *device)
1188{
1189 int ret;
1190 struct btrfs_path *path;
1191 struct btrfs_dev_item *dev_item;
1192 struct extent_buffer *leaf;
1193 struct btrfs_key key;
1194 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001195
1196 root = root->fs_info->chunk_root;
1197
1198 path = btrfs_alloc_path();
1199 if (!path)
1200 return -ENOMEM;
1201
Chris Mason0b86a832008-03-24 15:01:56 -04001202 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1203 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001204 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001205
1206 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001207 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001208 if (ret)
1209 goto out;
1210
1211 leaf = path->nodes[0];
1212 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1213
1214 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001215 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001216 btrfs_set_device_type(leaf, dev_item, device->type);
1217 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1218 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1219 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001220 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1221 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001222 btrfs_set_device_group(leaf, dev_item, 0);
1223 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1224 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001225 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001226
Chris Mason0b86a832008-03-24 15:01:56 -04001227 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001228 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001229 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1230 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001231 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001232
Yan Zheng2b820322008-11-17 21:11:30 -05001233 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001234out:
1235 btrfs_free_path(path);
1236 return ret;
1237}
Chris Mason8f18cf12008-04-25 16:53:30 -04001238
Chris Masona061fc82008-05-07 11:43:44 -04001239static int btrfs_rm_dev_item(struct btrfs_root *root,
1240 struct btrfs_device *device)
1241{
1242 int ret;
1243 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001244 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001245 struct btrfs_trans_handle *trans;
1246
1247 root = root->fs_info->chunk_root;
1248
1249 path = btrfs_alloc_path();
1250 if (!path)
1251 return -ENOMEM;
1252
Yan, Zhenga22285a2010-05-16 10:48:46 -04001253 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001254 if (IS_ERR(trans)) {
1255 btrfs_free_path(path);
1256 return PTR_ERR(trans);
1257 }
Chris Masona061fc82008-05-07 11:43:44 -04001258 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1259 key.type = BTRFS_DEV_ITEM_KEY;
1260 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001261 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001262
1263 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1264 if (ret < 0)
1265 goto out;
1266
1267 if (ret > 0) {
1268 ret = -ENOENT;
1269 goto out;
1270 }
1271
1272 ret = btrfs_del_item(trans, root, path);
1273 if (ret)
1274 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001275out:
1276 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001277 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001278 btrfs_commit_transaction(trans, root);
1279 return ret;
1280}
1281
1282int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1283{
1284 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001285 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001286 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001287 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001288 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001289 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001290 u64 all_avail;
1291 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001292 u64 num_devices;
1293 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001294 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001295 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001296
Chris Masona061fc82008-05-07 11:43:44 -04001297 mutex_lock(&uuid_mutex);
1298
1299 all_avail = root->fs_info->avail_data_alloc_bits |
1300 root->fs_info->avail_system_alloc_bits |
1301 root->fs_info->avail_metadata_alloc_bits;
1302
1303 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001304 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001305 printk(KERN_ERR "btrfs: unable to go below four devices "
1306 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001307 ret = -EINVAL;
1308 goto out;
1309 }
1310
1311 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001312 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001313 printk(KERN_ERR "btrfs: unable to go below two "
1314 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001315 ret = -EINVAL;
1316 goto out;
1317 }
1318
Chris Masondfe25022008-05-13 13:46:40 -04001319 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001320 struct list_head *devices;
1321 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001322
Chris Masondfe25022008-05-13 13:46:40 -04001323 device = NULL;
1324 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001325 /*
1326 * It is safe to read the devices since the volume_mutex
1327 * is held.
1328 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001329 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001330 if (tmp->in_fs_metadata && !tmp->bdev) {
1331 device = tmp;
1332 break;
1333 }
1334 }
1335 bdev = NULL;
1336 bh = NULL;
1337 disk_super = NULL;
1338 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001339 printk(KERN_ERR "btrfs: no missing devices found to "
1340 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001341 goto out;
1342 }
Chris Masondfe25022008-05-13 13:46:40 -04001343 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001344 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1345 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001346 if (IS_ERR(bdev)) {
1347 ret = PTR_ERR(bdev);
1348 goto out;
1349 }
1350
Yan Zheng2b820322008-11-17 21:11:30 -05001351 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001352 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001353 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001354 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001355 goto error_close;
1356 }
1357 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001358 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001359 dev_uuid = disk_super->dev_item.uuid;
1360 device = btrfs_find_device(root, devid, dev_uuid,
1361 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001362 if (!device) {
1363 ret = -ENOENT;
1364 goto error_brelse;
1365 }
Chris Masondfe25022008-05-13 13:46:40 -04001366 }
Yan Zheng2b820322008-11-17 21:11:30 -05001367
1368 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001369 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1370 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001371 ret = -EINVAL;
1372 goto error_brelse;
1373 }
1374
1375 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001376 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001377 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001378 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001379 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001380 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001381 }
Chris Masona061fc82008-05-07 11:43:44 -04001382
1383 ret = btrfs_shrink_device(device, 0);
1384 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001385 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001386
Chris Masona061fc82008-05-07 11:43:44 -04001387 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1388 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001389 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001390
Josef Bacik2bf64752011-09-26 17:12:22 -04001391 spin_lock(&root->fs_info->free_chunk_lock);
1392 root->fs_info->free_chunk_space = device->total_bytes -
1393 device->bytes_used;
1394 spin_unlock(&root->fs_info->free_chunk_lock);
1395
Yan Zheng2b820322008-11-17 21:11:30 -05001396 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001397 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001398
1399 /*
1400 * the device list mutex makes sure that we don't change
1401 * the device list while someone else is writing out all
1402 * the device supers.
1403 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001404
1405 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001406 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001407 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001408
Yan Zhenge4404d62008-12-12 10:03:26 -05001409 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001410
Chris Masoncd02dca2010-12-13 14:56:23 -05001411 if (device->missing)
1412 root->fs_info->fs_devices->missing_devices--;
1413
Yan Zheng2b820322008-11-17 21:11:30 -05001414 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1415 struct btrfs_device, dev_list);
1416 if (device->bdev == root->fs_info->sb->s_bdev)
1417 root->fs_info->sb->s_bdev = next_device->bdev;
1418 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1419 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1420
Xiao Guangrong1f781602011-04-20 10:09:16 +00001421 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001422 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001423
1424 call_rcu(&device->rcu, free_device);
1425 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001426
David Sterba6c417612011-04-13 15:41:04 +02001427 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1428 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001429
Xiao Guangrong1f781602011-04-20 10:09:16 +00001430 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001431 struct btrfs_fs_devices *fs_devices;
1432 fs_devices = root->fs_info->fs_devices;
1433 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001434 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001435 break;
1436 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001437 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001438 fs_devices->seed = cur_devices->seed;
1439 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001440 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001441 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001442 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001443 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001444 }
1445
1446 /*
1447 * at this point, the device is zero sized. We want to
1448 * remove it from the devices list and zero out the old super
1449 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001450 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001451 /* make sure this device isn't detected as part of
1452 * the FS anymore
1453 */
1454 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1455 set_buffer_dirty(bh);
1456 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001457 }
Chris Masona061fc82008-05-07 11:43:44 -04001458
Chris Masona061fc82008-05-07 11:43:44 -04001459 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001460
1461error_brelse:
1462 brelse(bh);
1463error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001464 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001465 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001466out:
1467 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001468 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001469error_undo:
1470 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001471 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001472 list_add(&device->dev_alloc_list,
1473 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001474 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001475 root->fs_info->fs_devices->rw_devices++;
1476 }
1477 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001478}
1479
Yan Zheng2b820322008-11-17 21:11:30 -05001480/*
1481 * does all the dirty work required for changing file system's UUID.
1482 */
Li Zefan125ccb02011-12-08 15:07:24 +08001483static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001484{
1485 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1486 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001487 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001488 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001489 struct btrfs_device *device;
1490 u64 super_flags;
1491
1492 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001493 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001494 return -EINVAL;
1495
Yan Zhenge4404d62008-12-12 10:03:26 -05001496 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1497 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001498 return -ENOMEM;
1499
Yan Zhenge4404d62008-12-12 10:03:26 -05001500 old_devices = clone_fs_devices(fs_devices);
1501 if (IS_ERR(old_devices)) {
1502 kfree(seed_devices);
1503 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001504 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001505
Yan Zheng2b820322008-11-17 21:11:30 -05001506 list_add(&old_devices->list, &fs_uuids);
1507
Yan Zhenge4404d62008-12-12 10:03:26 -05001508 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1509 seed_devices->opened = 1;
1510 INIT_LIST_HEAD(&seed_devices->devices);
1511 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001512 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001513
1514 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001515 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1516 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001517 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1518
Yan Zhenge4404d62008-12-12 10:03:26 -05001519 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1520 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1521 device->fs_devices = seed_devices;
1522 }
1523
Yan Zheng2b820322008-11-17 21:11:30 -05001524 fs_devices->seeding = 0;
1525 fs_devices->num_devices = 0;
1526 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001527 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001528
1529 generate_random_uuid(fs_devices->fsid);
1530 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1531 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1532 super_flags = btrfs_super_flags(disk_super) &
1533 ~BTRFS_SUPER_FLAG_SEEDING;
1534 btrfs_set_super_flags(disk_super, super_flags);
1535
1536 return 0;
1537}
1538
1539/*
1540 * strore the expected generation for seed devices in device items.
1541 */
1542static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1543 struct btrfs_root *root)
1544{
1545 struct btrfs_path *path;
1546 struct extent_buffer *leaf;
1547 struct btrfs_dev_item *dev_item;
1548 struct btrfs_device *device;
1549 struct btrfs_key key;
1550 u8 fs_uuid[BTRFS_UUID_SIZE];
1551 u8 dev_uuid[BTRFS_UUID_SIZE];
1552 u64 devid;
1553 int ret;
1554
1555 path = btrfs_alloc_path();
1556 if (!path)
1557 return -ENOMEM;
1558
1559 root = root->fs_info->chunk_root;
1560 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1561 key.offset = 0;
1562 key.type = BTRFS_DEV_ITEM_KEY;
1563
1564 while (1) {
1565 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1566 if (ret < 0)
1567 goto error;
1568
1569 leaf = path->nodes[0];
1570next_slot:
1571 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1572 ret = btrfs_next_leaf(root, path);
1573 if (ret > 0)
1574 break;
1575 if (ret < 0)
1576 goto error;
1577 leaf = path->nodes[0];
1578 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001579 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001580 continue;
1581 }
1582
1583 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1584 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1585 key.type != BTRFS_DEV_ITEM_KEY)
1586 break;
1587
1588 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1589 struct btrfs_dev_item);
1590 devid = btrfs_device_id(leaf, dev_item);
1591 read_extent_buffer(leaf, dev_uuid,
1592 (unsigned long)btrfs_device_uuid(dev_item),
1593 BTRFS_UUID_SIZE);
1594 read_extent_buffer(leaf, fs_uuid,
1595 (unsigned long)btrfs_device_fsid(dev_item),
1596 BTRFS_UUID_SIZE);
1597 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1598 BUG_ON(!device);
1599
1600 if (device->fs_devices->seeding) {
1601 btrfs_set_device_generation(leaf, dev_item,
1602 device->generation);
1603 btrfs_mark_buffer_dirty(leaf);
1604 }
1605
1606 path->slots[0]++;
1607 goto next_slot;
1608 }
1609 ret = 0;
1610error:
1611 btrfs_free_path(path);
1612 return ret;
1613}
1614
Chris Mason788f20e2008-04-28 15:29:42 -04001615int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1616{
Josef Bacikd5e20032011-08-04 14:52:27 +00001617 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001618 struct btrfs_trans_handle *trans;
1619 struct btrfs_device *device;
1620 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001621 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001622 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001623 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001624 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001625 int ret = 0;
1626
Yan Zheng2b820322008-11-17 21:11:30 -05001627 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1628 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001629
Li Zefana5d16332011-12-07 20:08:40 -05001630 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001631 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001632 if (IS_ERR(bdev))
1633 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001634
Yan Zheng2b820322008-11-17 21:11:30 -05001635 if (root->fs_info->fs_devices->seeding) {
1636 seeding_dev = 1;
1637 down_write(&sb->s_umount);
1638 mutex_lock(&uuid_mutex);
1639 }
1640
Chris Mason8c8bee12008-09-29 11:19:10 -04001641 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001642
Chris Mason788f20e2008-04-28 15:29:42 -04001643 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001644 /*
1645 * we have the volume lock, so we don't need the extra
1646 * device list mutex while reading the list here.
1647 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001648 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001649 if (device->bdev == bdev) {
1650 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001651 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001652 }
1653 }
1654
1655 device = kzalloc(sizeof(*device), GFP_NOFS);
1656 if (!device) {
1657 /* we can safely leave the fs_devices entry around */
1658 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001659 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001660 }
1661
Chris Mason788f20e2008-04-28 15:29:42 -04001662 device->name = kstrdup(device_path, GFP_NOFS);
1663 if (!device->name) {
1664 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001665 ret = -ENOMEM;
1666 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001667 }
Yan Zheng2b820322008-11-17 21:11:30 -05001668
1669 ret = find_next_devid(root, &device->devid);
1670 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001671 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001672 kfree(device);
1673 goto error;
1674 }
1675
Yan, Zhenga22285a2010-05-16 10:48:46 -04001676 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001677 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001678 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001679 kfree(device);
1680 ret = PTR_ERR(trans);
1681 goto error;
1682 }
1683
Yan Zheng2b820322008-11-17 21:11:30 -05001684 lock_chunks(root);
1685
Josef Bacikd5e20032011-08-04 14:52:27 +00001686 q = bdev_get_queue(bdev);
1687 if (blk_queue_discard(q))
1688 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001689 device->writeable = 1;
1690 device->work.func = pending_bios_fn;
1691 generate_random_uuid(device->uuid);
1692 spin_lock_init(&device->io_lock);
1693 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001694 device->io_width = root->sectorsize;
1695 device->io_align = root->sectorsize;
1696 device->sector_size = root->sectorsize;
1697 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001698 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001699 device->dev_root = root->fs_info->dev_root;
1700 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001701 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001702 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001703 set_blocksize(device->bdev, 4096);
1704
Yan Zheng2b820322008-11-17 21:11:30 -05001705 if (seeding_dev) {
1706 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001707 ret = btrfs_prepare_sprout(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001708 BUG_ON(ret);
1709 }
1710
1711 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001712
1713 /*
1714 * we don't want write_supers to jump in here with our device
1715 * half setup
1716 */
1717 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001718 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001719 list_add(&device->dev_alloc_list,
1720 &root->fs_info->fs_devices->alloc_list);
1721 root->fs_info->fs_devices->num_devices++;
1722 root->fs_info->fs_devices->open_devices++;
1723 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001724 if (device->can_discard)
1725 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001726 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1727
Josef Bacik2bf64752011-09-26 17:12:22 -04001728 spin_lock(&root->fs_info->free_chunk_lock);
1729 root->fs_info->free_chunk_space += device->total_bytes;
1730 spin_unlock(&root->fs_info->free_chunk_lock);
1731
Chris Masonc289811c2009-06-10 09:51:32 -04001732 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1733 root->fs_info->fs_devices->rotating = 1;
1734
David Sterba6c417612011-04-13 15:41:04 +02001735 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1736 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001737 total_bytes + device->total_bytes);
1738
David Sterba6c417612011-04-13 15:41:04 +02001739 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1740 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001741 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001742 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001743
Yan Zheng2b820322008-11-17 21:11:30 -05001744 if (seeding_dev) {
1745 ret = init_first_rw_device(trans, root, device);
1746 BUG_ON(ret);
1747 ret = btrfs_finish_sprout(trans, root);
1748 BUG_ON(ret);
1749 } else {
1750 ret = btrfs_add_device(trans, root, device);
1751 }
1752
Chris Mason913d9522009-03-10 13:17:18 -04001753 /*
1754 * we've got more storage, clear any full flags on the space
1755 * infos
1756 */
1757 btrfs_clear_space_info_full(root->fs_info);
1758
Chris Mason7d9eb122008-07-08 14:19:17 -04001759 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001760 btrfs_commit_transaction(trans, root);
1761
1762 if (seeding_dev) {
1763 mutex_unlock(&uuid_mutex);
1764 up_write(&sb->s_umount);
1765
1766 ret = btrfs_relocate_sys_chunks(root);
1767 BUG_ON(ret);
1768 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001769
Chris Mason788f20e2008-04-28 15:29:42 -04001770 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001771error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001772 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001773 if (seeding_dev) {
1774 mutex_unlock(&uuid_mutex);
1775 up_write(&sb->s_umount);
1776 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001777 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001778}
1779
Chris Masond3977122009-01-05 21:25:51 -05001780static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1781 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001782{
1783 int ret;
1784 struct btrfs_path *path;
1785 struct btrfs_root *root;
1786 struct btrfs_dev_item *dev_item;
1787 struct extent_buffer *leaf;
1788 struct btrfs_key key;
1789
1790 root = device->dev_root->fs_info->chunk_root;
1791
1792 path = btrfs_alloc_path();
1793 if (!path)
1794 return -ENOMEM;
1795
1796 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1797 key.type = BTRFS_DEV_ITEM_KEY;
1798 key.offset = device->devid;
1799
1800 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1801 if (ret < 0)
1802 goto out;
1803
1804 if (ret > 0) {
1805 ret = -ENOENT;
1806 goto out;
1807 }
1808
1809 leaf = path->nodes[0];
1810 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1811
1812 btrfs_set_device_id(leaf, dev_item, device->devid);
1813 btrfs_set_device_type(leaf, dev_item, device->type);
1814 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1815 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1816 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001817 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001818 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1819 btrfs_mark_buffer_dirty(leaf);
1820
1821out:
1822 btrfs_free_path(path);
1823 return ret;
1824}
1825
Chris Mason7d9eb122008-07-08 14:19:17 -04001826static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001827 struct btrfs_device *device, u64 new_size)
1828{
1829 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001830 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001831 u64 old_total = btrfs_super_total_bytes(super_copy);
1832 u64 diff = new_size - device->total_bytes;
1833
Yan Zheng2b820322008-11-17 21:11:30 -05001834 if (!device->writeable)
1835 return -EACCES;
1836 if (new_size <= device->total_bytes)
1837 return -EINVAL;
1838
Chris Mason8f18cf12008-04-25 16:53:30 -04001839 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001840 device->fs_devices->total_rw_bytes += diff;
1841
1842 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001843 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001844 btrfs_clear_space_info_full(device->dev_root->fs_info);
1845
Chris Mason8f18cf12008-04-25 16:53:30 -04001846 return btrfs_update_device(trans, device);
1847}
1848
Chris Mason7d9eb122008-07-08 14:19:17 -04001849int btrfs_grow_device(struct btrfs_trans_handle *trans,
1850 struct btrfs_device *device, u64 new_size)
1851{
1852 int ret;
1853 lock_chunks(device->dev_root);
1854 ret = __btrfs_grow_device(trans, device, new_size);
1855 unlock_chunks(device->dev_root);
1856 return ret;
1857}
1858
Chris Mason8f18cf12008-04-25 16:53:30 -04001859static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1860 struct btrfs_root *root,
1861 u64 chunk_tree, u64 chunk_objectid,
1862 u64 chunk_offset)
1863{
1864 int ret;
1865 struct btrfs_path *path;
1866 struct btrfs_key key;
1867
1868 root = root->fs_info->chunk_root;
1869 path = btrfs_alloc_path();
1870 if (!path)
1871 return -ENOMEM;
1872
1873 key.objectid = chunk_objectid;
1874 key.offset = chunk_offset;
1875 key.type = BTRFS_CHUNK_ITEM_KEY;
1876
1877 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1878 BUG_ON(ret);
1879
1880 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001881
1882 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001883 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001884}
1885
Christoph Hellwigb2950862008-12-02 09:54:17 -05001886static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001887 chunk_offset)
1888{
David Sterba6c417612011-04-13 15:41:04 +02001889 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001890 struct btrfs_disk_key *disk_key;
1891 struct btrfs_chunk *chunk;
1892 u8 *ptr;
1893 int ret = 0;
1894 u32 num_stripes;
1895 u32 array_size;
1896 u32 len = 0;
1897 u32 cur;
1898 struct btrfs_key key;
1899
1900 array_size = btrfs_super_sys_array_size(super_copy);
1901
1902 ptr = super_copy->sys_chunk_array;
1903 cur = 0;
1904
1905 while (cur < array_size) {
1906 disk_key = (struct btrfs_disk_key *)ptr;
1907 btrfs_disk_key_to_cpu(&key, disk_key);
1908
1909 len = sizeof(*disk_key);
1910
1911 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1912 chunk = (struct btrfs_chunk *)(ptr + len);
1913 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1914 len += btrfs_chunk_item_size(num_stripes);
1915 } else {
1916 ret = -EIO;
1917 break;
1918 }
1919 if (key.objectid == chunk_objectid &&
1920 key.offset == chunk_offset) {
1921 memmove(ptr, ptr + len, array_size - (cur + len));
1922 array_size -= len;
1923 btrfs_set_super_sys_array_size(super_copy, array_size);
1924 } else {
1925 ptr += len;
1926 cur += len;
1927 }
1928 }
1929 return ret;
1930}
1931
Christoph Hellwigb2950862008-12-02 09:54:17 -05001932static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001933 u64 chunk_tree, u64 chunk_objectid,
1934 u64 chunk_offset)
1935{
1936 struct extent_map_tree *em_tree;
1937 struct btrfs_root *extent_root;
1938 struct btrfs_trans_handle *trans;
1939 struct extent_map *em;
1940 struct map_lookup *map;
1941 int ret;
1942 int i;
1943
1944 root = root->fs_info->chunk_root;
1945 extent_root = root->fs_info->extent_root;
1946 em_tree = &root->fs_info->mapping_tree.map_tree;
1947
Josef Bacikba1bf482009-09-11 16:11:19 -04001948 ret = btrfs_can_relocate(extent_root, chunk_offset);
1949 if (ret)
1950 return -ENOSPC;
1951
Chris Mason8f18cf12008-04-25 16:53:30 -04001952 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001953 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001954 if (ret)
1955 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001956
Yan, Zhenga22285a2010-05-16 10:48:46 -04001957 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001958 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001959
Chris Mason7d9eb122008-07-08 14:19:17 -04001960 lock_chunks(root);
1961
Chris Mason8f18cf12008-04-25 16:53:30 -04001962 /*
1963 * step two, delete the device extents and the
1964 * chunk tree entries
1965 */
Chris Mason890871b2009-09-02 16:24:52 -04001966 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001967 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001968 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001969
Tsutomu Itoh285190d2012-02-16 16:23:58 +09001970 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04001971 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001972 map = (struct map_lookup *)em->bdev;
1973
1974 for (i = 0; i < map->num_stripes; i++) {
1975 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1976 map->stripes[i].physical);
1977 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001978
Chris Masondfe25022008-05-13 13:46:40 -04001979 if (map->stripes[i].dev) {
1980 ret = btrfs_update_device(trans, map->stripes[i].dev);
1981 BUG_ON(ret);
1982 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001983 }
1984 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1985 chunk_offset);
1986
1987 BUG_ON(ret);
1988
liubo1abe9b82011-03-24 11:18:59 +00001989 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1990
Chris Mason8f18cf12008-04-25 16:53:30 -04001991 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1992 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1993 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001994 }
1995
Zheng Yan1a40e232008-09-26 10:09:34 -04001996 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1997 BUG_ON(ret);
1998
Chris Mason890871b2009-09-02 16:24:52 -04001999 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002000 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002001 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002002
Chris Mason8f18cf12008-04-25 16:53:30 -04002003 kfree(map);
2004 em->bdev = NULL;
2005
2006 /* once for the tree */
2007 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002008 /* once for us */
2009 free_extent_map(em);
2010
Chris Mason7d9eb122008-07-08 14:19:17 -04002011 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002012 btrfs_end_transaction(trans, root);
2013 return 0;
2014}
2015
Yan Zheng2b820322008-11-17 21:11:30 -05002016static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2017{
2018 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2019 struct btrfs_path *path;
2020 struct extent_buffer *leaf;
2021 struct btrfs_chunk *chunk;
2022 struct btrfs_key key;
2023 struct btrfs_key found_key;
2024 u64 chunk_tree = chunk_root->root_key.objectid;
2025 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002026 bool retried = false;
2027 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002028 int ret;
2029
2030 path = btrfs_alloc_path();
2031 if (!path)
2032 return -ENOMEM;
2033
Josef Bacikba1bf482009-09-11 16:11:19 -04002034again:
Yan Zheng2b820322008-11-17 21:11:30 -05002035 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2036 key.offset = (u64)-1;
2037 key.type = BTRFS_CHUNK_ITEM_KEY;
2038
2039 while (1) {
2040 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2041 if (ret < 0)
2042 goto error;
2043 BUG_ON(ret == 0);
2044
2045 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2046 key.type);
2047 if (ret < 0)
2048 goto error;
2049 if (ret > 0)
2050 break;
2051
2052 leaf = path->nodes[0];
2053 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2054
2055 chunk = btrfs_item_ptr(leaf, path->slots[0],
2056 struct btrfs_chunk);
2057 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002058 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002059
2060 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2061 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2062 found_key.objectid,
2063 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002064 if (ret == -ENOSPC)
2065 failed++;
2066 else if (ret)
2067 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002068 }
2069
2070 if (found_key.offset == 0)
2071 break;
2072 key.offset = found_key.offset - 1;
2073 }
2074 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002075 if (failed && !retried) {
2076 failed = 0;
2077 retried = true;
2078 goto again;
2079 } else if (failed && retried) {
2080 WARN_ON(1);
2081 ret = -ENOSPC;
2082 }
Yan Zheng2b820322008-11-17 21:11:30 -05002083error:
2084 btrfs_free_path(path);
2085 return ret;
2086}
2087
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002088static int insert_balance_item(struct btrfs_root *root,
2089 struct btrfs_balance_control *bctl)
2090{
2091 struct btrfs_trans_handle *trans;
2092 struct btrfs_balance_item *item;
2093 struct btrfs_disk_balance_args disk_bargs;
2094 struct btrfs_path *path;
2095 struct extent_buffer *leaf;
2096 struct btrfs_key key;
2097 int ret, err;
2098
2099 path = btrfs_alloc_path();
2100 if (!path)
2101 return -ENOMEM;
2102
2103 trans = btrfs_start_transaction(root, 0);
2104 if (IS_ERR(trans)) {
2105 btrfs_free_path(path);
2106 return PTR_ERR(trans);
2107 }
2108
2109 key.objectid = BTRFS_BALANCE_OBJECTID;
2110 key.type = BTRFS_BALANCE_ITEM_KEY;
2111 key.offset = 0;
2112
2113 ret = btrfs_insert_empty_item(trans, root, path, &key,
2114 sizeof(*item));
2115 if (ret)
2116 goto out;
2117
2118 leaf = path->nodes[0];
2119 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2120
2121 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2122
2123 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2124 btrfs_set_balance_data(leaf, item, &disk_bargs);
2125 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2126 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2127 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2128 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2129
2130 btrfs_set_balance_flags(leaf, item, bctl->flags);
2131
2132 btrfs_mark_buffer_dirty(leaf);
2133out:
2134 btrfs_free_path(path);
2135 err = btrfs_commit_transaction(trans, root);
2136 if (err && !ret)
2137 ret = err;
2138 return ret;
2139}
2140
2141static int del_balance_item(struct btrfs_root *root)
2142{
2143 struct btrfs_trans_handle *trans;
2144 struct btrfs_path *path;
2145 struct btrfs_key key;
2146 int ret, err;
2147
2148 path = btrfs_alloc_path();
2149 if (!path)
2150 return -ENOMEM;
2151
2152 trans = btrfs_start_transaction(root, 0);
2153 if (IS_ERR(trans)) {
2154 btrfs_free_path(path);
2155 return PTR_ERR(trans);
2156 }
2157
2158 key.objectid = BTRFS_BALANCE_OBJECTID;
2159 key.type = BTRFS_BALANCE_ITEM_KEY;
2160 key.offset = 0;
2161
2162 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2163 if (ret < 0)
2164 goto out;
2165 if (ret > 0) {
2166 ret = -ENOENT;
2167 goto out;
2168 }
2169
2170 ret = btrfs_del_item(trans, root, path);
2171out:
2172 btrfs_free_path(path);
2173 err = btrfs_commit_transaction(trans, root);
2174 if (err && !ret)
2175 ret = err;
2176 return ret;
2177}
2178
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002179/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002180 * This is a heuristic used to reduce the number of chunks balanced on
2181 * resume after balance was interrupted.
2182 */
2183static void update_balance_args(struct btrfs_balance_control *bctl)
2184{
2185 /*
2186 * Turn on soft mode for chunk types that were being converted.
2187 */
2188 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2189 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2190 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2191 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2192 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2193 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2194
2195 /*
2196 * Turn on usage filter if is not already used. The idea is
2197 * that chunks that we have already balanced should be
2198 * reasonably full. Don't do it for chunks that are being
2199 * converted - that will keep us from relocating unconverted
2200 * (albeit full) chunks.
2201 */
2202 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2203 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2204 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2205 bctl->data.usage = 90;
2206 }
2207 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2208 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2209 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2210 bctl->sys.usage = 90;
2211 }
2212 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2213 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2214 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2215 bctl->meta.usage = 90;
2216 }
2217}
2218
2219/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002220 * Should be called with both balance and volume mutexes held to
2221 * serialize other volume operations (add_dev/rm_dev/resize) with
2222 * restriper. Same goes for unset_balance_control.
2223 */
2224static void set_balance_control(struct btrfs_balance_control *bctl)
2225{
2226 struct btrfs_fs_info *fs_info = bctl->fs_info;
2227
2228 BUG_ON(fs_info->balance_ctl);
2229
2230 spin_lock(&fs_info->balance_lock);
2231 fs_info->balance_ctl = bctl;
2232 spin_unlock(&fs_info->balance_lock);
2233}
2234
2235static void unset_balance_control(struct btrfs_fs_info *fs_info)
2236{
2237 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2238
2239 BUG_ON(!fs_info->balance_ctl);
2240
2241 spin_lock(&fs_info->balance_lock);
2242 fs_info->balance_ctl = NULL;
2243 spin_unlock(&fs_info->balance_lock);
2244
2245 kfree(bctl);
2246}
2247
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002248/*
2249 * Balance filters. Return 1 if chunk should be filtered out
2250 * (should not be balanced).
2251 */
2252static int chunk_profiles_filter(u64 chunk_profile,
2253 struct btrfs_balance_args *bargs)
2254{
2255 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2256
2257 if (chunk_profile == 0)
2258 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2259
2260 if (bargs->profiles & chunk_profile)
2261 return 0;
2262
2263 return 1;
2264}
2265
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002266static u64 div_factor_fine(u64 num, int factor)
2267{
2268 if (factor <= 0)
2269 return 0;
2270 if (factor >= 100)
2271 return num;
2272
2273 num *= factor;
2274 do_div(num, 100);
2275 return num;
2276}
2277
2278static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2279 struct btrfs_balance_args *bargs)
2280{
2281 struct btrfs_block_group_cache *cache;
2282 u64 chunk_used, user_thresh;
2283 int ret = 1;
2284
2285 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2286 chunk_used = btrfs_block_group_used(&cache->item);
2287
2288 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2289 if (chunk_used < user_thresh)
2290 ret = 0;
2291
2292 btrfs_put_block_group(cache);
2293 return ret;
2294}
2295
Ilya Dryomov409d4042012-01-16 22:04:47 +02002296static int chunk_devid_filter(struct extent_buffer *leaf,
2297 struct btrfs_chunk *chunk,
2298 struct btrfs_balance_args *bargs)
2299{
2300 struct btrfs_stripe *stripe;
2301 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2302 int i;
2303
2304 for (i = 0; i < num_stripes; i++) {
2305 stripe = btrfs_stripe_nr(chunk, i);
2306 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2307 return 0;
2308 }
2309
2310 return 1;
2311}
2312
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002313/* [pstart, pend) */
2314static int chunk_drange_filter(struct extent_buffer *leaf,
2315 struct btrfs_chunk *chunk,
2316 u64 chunk_offset,
2317 struct btrfs_balance_args *bargs)
2318{
2319 struct btrfs_stripe *stripe;
2320 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2321 u64 stripe_offset;
2322 u64 stripe_length;
2323 int factor;
2324 int i;
2325
2326 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2327 return 0;
2328
2329 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2330 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2331 factor = 2;
2332 else
2333 factor = 1;
2334 factor = num_stripes / factor;
2335
2336 for (i = 0; i < num_stripes; i++) {
2337 stripe = btrfs_stripe_nr(chunk, i);
2338 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2339 continue;
2340
2341 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2342 stripe_length = btrfs_chunk_length(leaf, chunk);
2343 do_div(stripe_length, factor);
2344
2345 if (stripe_offset < bargs->pend &&
2346 stripe_offset + stripe_length > bargs->pstart)
2347 return 0;
2348 }
2349
2350 return 1;
2351}
2352
Ilya Dryomovea671762012-01-16 22:04:48 +02002353/* [vstart, vend) */
2354static int chunk_vrange_filter(struct extent_buffer *leaf,
2355 struct btrfs_chunk *chunk,
2356 u64 chunk_offset,
2357 struct btrfs_balance_args *bargs)
2358{
2359 if (chunk_offset < bargs->vend &&
2360 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2361 /* at least part of the chunk is inside this vrange */
2362 return 0;
2363
2364 return 1;
2365}
2366
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002367static int chunk_soft_convert_filter(u64 chunk_profile,
2368 struct btrfs_balance_args *bargs)
2369{
2370 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2371 return 0;
2372
2373 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2374
2375 if (chunk_profile == 0)
2376 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2377
2378 if (bargs->target & chunk_profile)
2379 return 1;
2380
2381 return 0;
2382}
2383
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002384static int should_balance_chunk(struct btrfs_root *root,
2385 struct extent_buffer *leaf,
2386 struct btrfs_chunk *chunk, u64 chunk_offset)
2387{
2388 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2389 struct btrfs_balance_args *bargs = NULL;
2390 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2391
2392 /* type filter */
2393 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2394 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2395 return 0;
2396 }
2397
2398 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2399 bargs = &bctl->data;
2400 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2401 bargs = &bctl->sys;
2402 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2403 bargs = &bctl->meta;
2404
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002405 /* profiles filter */
2406 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2407 chunk_profiles_filter(chunk_type, bargs)) {
2408 return 0;
2409 }
2410
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002411 /* usage filter */
2412 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2413 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2414 return 0;
2415 }
2416
Ilya Dryomov409d4042012-01-16 22:04:47 +02002417 /* devid filter */
2418 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2419 chunk_devid_filter(leaf, chunk, bargs)) {
2420 return 0;
2421 }
2422
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002423 /* drange filter, makes sense only with devid filter */
2424 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2425 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2426 return 0;
2427 }
2428
Ilya Dryomovea671762012-01-16 22:04:48 +02002429 /* vrange filter */
2430 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2431 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2432 return 0;
2433 }
2434
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002435 /* soft profile changing mode */
2436 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2437 chunk_soft_convert_filter(chunk_type, bargs)) {
2438 return 0;
2439 }
2440
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002441 return 1;
2442}
2443
Chris Masonec44a352008-04-28 15:29:52 -04002444static u64 div_factor(u64 num, int factor)
2445{
2446 if (factor == 10)
2447 return num;
2448 num *= factor;
2449 do_div(num, 10);
2450 return num;
2451}
2452
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002453static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002454{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002455 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002456 struct btrfs_root *chunk_root = fs_info->chunk_root;
2457 struct btrfs_root *dev_root = fs_info->dev_root;
2458 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002459 struct btrfs_device *device;
2460 u64 old_size;
2461 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002462 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002463 struct btrfs_path *path;
2464 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002465 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002466 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002467 struct extent_buffer *leaf;
2468 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002469 int ret;
2470 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002471 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002472
Chris Masonec44a352008-04-28 15:29:52 -04002473 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002474 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002475 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002476 old_size = device->total_bytes;
2477 size_to_free = div_factor(old_size, 1);
2478 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002479 if (!device->writeable ||
2480 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002481 continue;
2482
2483 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002484 if (ret == -ENOSPC)
2485 break;
Chris Masonec44a352008-04-28 15:29:52 -04002486 BUG_ON(ret);
2487
Yan, Zhenga22285a2010-05-16 10:48:46 -04002488 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002489 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002490
2491 ret = btrfs_grow_device(trans, device, old_size);
2492 BUG_ON(ret);
2493
2494 btrfs_end_transaction(trans, dev_root);
2495 }
2496
2497 /* step two, relocate all the chunks */
2498 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002499 if (!path) {
2500 ret = -ENOMEM;
2501 goto error;
2502 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002503
2504 /* zero out stat counters */
2505 spin_lock(&fs_info->balance_lock);
2506 memset(&bctl->stat, 0, sizeof(bctl->stat));
2507 spin_unlock(&fs_info->balance_lock);
2508again:
Chris Masonec44a352008-04-28 15:29:52 -04002509 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2510 key.offset = (u64)-1;
2511 key.type = BTRFS_CHUNK_ITEM_KEY;
2512
Chris Masond3977122009-01-05 21:25:51 -05002513 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002514 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002515 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002516 ret = -ECANCELED;
2517 goto error;
2518 }
2519
Chris Masonec44a352008-04-28 15:29:52 -04002520 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2521 if (ret < 0)
2522 goto error;
2523
2524 /*
2525 * this shouldn't happen, it means the last relocate
2526 * failed
2527 */
2528 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002529 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002530
2531 ret = btrfs_previous_item(chunk_root, path, 0,
2532 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002533 if (ret) {
2534 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002535 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002536 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002537
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002538 leaf = path->nodes[0];
2539 slot = path->slots[0];
2540 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2541
Chris Masonec44a352008-04-28 15:29:52 -04002542 if (found_key.objectid != key.objectid)
2543 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002544
Chris Masonec44a352008-04-28 15:29:52 -04002545 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002546 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002547 break;
2548
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002549 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2550
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002551 if (!counting) {
2552 spin_lock(&fs_info->balance_lock);
2553 bctl->stat.considered++;
2554 spin_unlock(&fs_info->balance_lock);
2555 }
2556
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002557 ret = should_balance_chunk(chunk_root, leaf, chunk,
2558 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002559 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002560 if (!ret)
2561 goto loop;
2562
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002563 if (counting) {
2564 spin_lock(&fs_info->balance_lock);
2565 bctl->stat.expected++;
2566 spin_unlock(&fs_info->balance_lock);
2567 goto loop;
2568 }
2569
Chris Masonec44a352008-04-28 15:29:52 -04002570 ret = btrfs_relocate_chunk(chunk_root,
2571 chunk_root->root_key.objectid,
2572 found_key.objectid,
2573 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002574 if (ret && ret != -ENOSPC)
2575 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002576 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002577 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002578 } else {
2579 spin_lock(&fs_info->balance_lock);
2580 bctl->stat.completed++;
2581 spin_unlock(&fs_info->balance_lock);
2582 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002583loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002584 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002585 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002586
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002587 if (counting) {
2588 btrfs_release_path(path);
2589 counting = false;
2590 goto again;
2591 }
Chris Masonec44a352008-04-28 15:29:52 -04002592error:
2593 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002594 if (enospc_errors) {
2595 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2596 enospc_errors);
2597 if (!ret)
2598 ret = -ENOSPC;
2599 }
2600
Chris Masonec44a352008-04-28 15:29:52 -04002601 return ret;
2602}
2603
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002604static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2605{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002606 /* cancel requested || normal exit path */
2607 return atomic_read(&fs_info->balance_cancel_req) ||
2608 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2609 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002610}
2611
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002612static void __cancel_balance(struct btrfs_fs_info *fs_info)
2613{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002614 int ret;
2615
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002616 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002617 ret = del_balance_item(fs_info->tree_root);
2618 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002619}
2620
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002621void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002622 struct btrfs_ioctl_balance_args *bargs);
2623
2624/*
2625 * Should be called with both balance and volume mutexes held
2626 */
2627int btrfs_balance(struct btrfs_balance_control *bctl,
2628 struct btrfs_ioctl_balance_args *bargs)
2629{
2630 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002631 u64 allowed;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002632 int ret;
2633
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002634 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002635 atomic_read(&fs_info->balance_pause_req) ||
2636 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002637 ret = -EINVAL;
2638 goto out;
2639 }
2640
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002641 /*
2642 * In case of mixed groups both data and meta should be picked,
2643 * and identical options should be given for both of them.
2644 */
2645 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2646 if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
2647 (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
2648 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2649 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2650 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2651 printk(KERN_ERR "btrfs: with mixed groups data and "
2652 "metadata balance options must be the same\n");
2653 ret = -EINVAL;
2654 goto out;
2655 }
2656 }
2657
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002658 /*
2659 * Profile changing sanity checks. Skip them if a simple
2660 * balance is requested.
2661 */
2662 if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
2663 BTRFS_BALANCE_ARGS_CONVERT))
2664 goto do_balance;
2665
2666 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2667 if (fs_info->fs_devices->num_devices == 1)
2668 allowed |= BTRFS_BLOCK_GROUP_DUP;
2669 else if (fs_info->fs_devices->num_devices < 4)
2670 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2671 else
2672 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2673 BTRFS_BLOCK_GROUP_RAID10);
2674
2675 if (!profile_is_valid(bctl->data.target, 1) ||
2676 bctl->data.target & ~allowed) {
2677 printk(KERN_ERR "btrfs: unable to start balance with target "
2678 "data profile %llu\n",
2679 (unsigned long long)bctl->data.target);
2680 ret = -EINVAL;
2681 goto out;
2682 }
2683 if (!profile_is_valid(bctl->meta.target, 1) ||
2684 bctl->meta.target & ~allowed) {
2685 printk(KERN_ERR "btrfs: unable to start balance with target "
2686 "metadata profile %llu\n",
2687 (unsigned long long)bctl->meta.target);
2688 ret = -EINVAL;
2689 goto out;
2690 }
2691 if (!profile_is_valid(bctl->sys.target, 1) ||
2692 bctl->sys.target & ~allowed) {
2693 printk(KERN_ERR "btrfs: unable to start balance with target "
2694 "system profile %llu\n",
2695 (unsigned long long)bctl->sys.target);
2696 ret = -EINVAL;
2697 goto out;
2698 }
2699
2700 if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
2701 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2702 ret = -EINVAL;
2703 goto out;
2704 }
2705
2706 /* allow to reduce meta or sys integrity only if force set */
2707 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2708 BTRFS_BLOCK_GROUP_RAID10;
2709 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2710 (fs_info->avail_system_alloc_bits & allowed) &&
2711 !(bctl->sys.target & allowed)) ||
2712 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2713 (fs_info->avail_metadata_alloc_bits & allowed) &&
2714 !(bctl->meta.target & allowed))) {
2715 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2716 printk(KERN_INFO "btrfs: force reducing metadata "
2717 "integrity\n");
2718 } else {
2719 printk(KERN_ERR "btrfs: balance will reduce metadata "
2720 "integrity, use force if you want this\n");
2721 ret = -EINVAL;
2722 goto out;
2723 }
2724 }
2725
2726do_balance:
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002727 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002728 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002729 goto out;
2730
Ilya Dryomov59641012012-01-16 22:04:48 +02002731 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2732 BUG_ON(ret == -EEXIST);
2733 set_balance_control(bctl);
2734 } else {
2735 BUG_ON(ret != -EEXIST);
2736 spin_lock(&fs_info->balance_lock);
2737 update_balance_args(bctl);
2738 spin_unlock(&fs_info->balance_lock);
2739 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002740
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002741 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002742 mutex_unlock(&fs_info->balance_mutex);
2743
2744 ret = __btrfs_balance(fs_info);
2745
2746 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002747 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002748
2749 if (bargs) {
2750 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002751 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002752 }
2753
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002754 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2755 balance_need_close(fs_info)) {
2756 __cancel_balance(fs_info);
2757 }
2758
2759 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002760
2761 return ret;
2762out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002763 if (bctl->flags & BTRFS_BALANCE_RESUME)
2764 __cancel_balance(fs_info);
2765 else
2766 kfree(bctl);
2767 return ret;
2768}
2769
2770static int balance_kthread(void *data)
2771{
2772 struct btrfs_balance_control *bctl =
2773 (struct btrfs_balance_control *)data;
2774 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002775 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002776
2777 mutex_lock(&fs_info->volume_mutex);
2778 mutex_lock(&fs_info->balance_mutex);
2779
2780 set_balance_control(bctl);
2781
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002782 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2783 printk(KERN_INFO "btrfs: force skipping balance\n");
2784 } else {
2785 printk(KERN_INFO "btrfs: continuing balance\n");
2786 ret = btrfs_balance(bctl, NULL);
2787 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002788
2789 mutex_unlock(&fs_info->balance_mutex);
2790 mutex_unlock(&fs_info->volume_mutex);
2791 return ret;
2792}
2793
2794int btrfs_recover_balance(struct btrfs_root *tree_root)
2795{
2796 struct task_struct *tsk;
2797 struct btrfs_balance_control *bctl;
2798 struct btrfs_balance_item *item;
2799 struct btrfs_disk_balance_args disk_bargs;
2800 struct btrfs_path *path;
2801 struct extent_buffer *leaf;
2802 struct btrfs_key key;
2803 int ret;
2804
2805 path = btrfs_alloc_path();
2806 if (!path)
2807 return -ENOMEM;
2808
2809 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2810 if (!bctl) {
2811 ret = -ENOMEM;
2812 goto out;
2813 }
2814
2815 key.objectid = BTRFS_BALANCE_OBJECTID;
2816 key.type = BTRFS_BALANCE_ITEM_KEY;
2817 key.offset = 0;
2818
2819 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2820 if (ret < 0)
2821 goto out_bctl;
2822 if (ret > 0) { /* ret = -ENOENT; */
2823 ret = 0;
2824 goto out_bctl;
2825 }
2826
2827 leaf = path->nodes[0];
2828 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2829
2830 bctl->fs_info = tree_root->fs_info;
2831 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2832
2833 btrfs_balance_data(leaf, item, &disk_bargs);
2834 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2835 btrfs_balance_meta(leaf, item, &disk_bargs);
2836 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2837 btrfs_balance_sys(leaf, item, &disk_bargs);
2838 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2839
2840 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2841 if (IS_ERR(tsk))
2842 ret = PTR_ERR(tsk);
2843 else
2844 goto out;
2845
2846out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002847 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002848out:
2849 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002850 return ret;
2851}
2852
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002853int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2854{
2855 int ret = 0;
2856
2857 mutex_lock(&fs_info->balance_mutex);
2858 if (!fs_info->balance_ctl) {
2859 mutex_unlock(&fs_info->balance_mutex);
2860 return -ENOTCONN;
2861 }
2862
2863 if (atomic_read(&fs_info->balance_running)) {
2864 atomic_inc(&fs_info->balance_pause_req);
2865 mutex_unlock(&fs_info->balance_mutex);
2866
2867 wait_event(fs_info->balance_wait_q,
2868 atomic_read(&fs_info->balance_running) == 0);
2869
2870 mutex_lock(&fs_info->balance_mutex);
2871 /* we are good with balance_ctl ripped off from under us */
2872 BUG_ON(atomic_read(&fs_info->balance_running));
2873 atomic_dec(&fs_info->balance_pause_req);
2874 } else {
2875 ret = -ENOTCONN;
2876 }
2877
2878 mutex_unlock(&fs_info->balance_mutex);
2879 return ret;
2880}
2881
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002882int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2883{
2884 mutex_lock(&fs_info->balance_mutex);
2885 if (!fs_info->balance_ctl) {
2886 mutex_unlock(&fs_info->balance_mutex);
2887 return -ENOTCONN;
2888 }
2889
2890 atomic_inc(&fs_info->balance_cancel_req);
2891 /*
2892 * if we are running just wait and return, balance item is
2893 * deleted in btrfs_balance in this case
2894 */
2895 if (atomic_read(&fs_info->balance_running)) {
2896 mutex_unlock(&fs_info->balance_mutex);
2897 wait_event(fs_info->balance_wait_q,
2898 atomic_read(&fs_info->balance_running) == 0);
2899 mutex_lock(&fs_info->balance_mutex);
2900 } else {
2901 /* __cancel_balance needs volume_mutex */
2902 mutex_unlock(&fs_info->balance_mutex);
2903 mutex_lock(&fs_info->volume_mutex);
2904 mutex_lock(&fs_info->balance_mutex);
2905
2906 if (fs_info->balance_ctl)
2907 __cancel_balance(fs_info);
2908
2909 mutex_unlock(&fs_info->volume_mutex);
2910 }
2911
2912 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2913 atomic_dec(&fs_info->balance_cancel_req);
2914 mutex_unlock(&fs_info->balance_mutex);
2915 return 0;
2916}
2917
Chris Mason8f18cf12008-04-25 16:53:30 -04002918/*
2919 * shrinking a device means finding all of the device extents past
2920 * the new size, and then following the back refs to the chunks.
2921 * The chunk relocation code actually frees the device extent
2922 */
2923int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2924{
2925 struct btrfs_trans_handle *trans;
2926 struct btrfs_root *root = device->dev_root;
2927 struct btrfs_dev_extent *dev_extent = NULL;
2928 struct btrfs_path *path;
2929 u64 length;
2930 u64 chunk_tree;
2931 u64 chunk_objectid;
2932 u64 chunk_offset;
2933 int ret;
2934 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002935 int failed = 0;
2936 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002937 struct extent_buffer *l;
2938 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002939 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002940 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002941 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002942 u64 diff = device->total_bytes - new_size;
2943
Yan Zheng2b820322008-11-17 21:11:30 -05002944 if (new_size >= device->total_bytes)
2945 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002946
2947 path = btrfs_alloc_path();
2948 if (!path)
2949 return -ENOMEM;
2950
Chris Mason8f18cf12008-04-25 16:53:30 -04002951 path->reada = 2;
2952
Chris Mason7d9eb122008-07-08 14:19:17 -04002953 lock_chunks(root);
2954
Chris Mason8f18cf12008-04-25 16:53:30 -04002955 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002956 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002957 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002958 spin_lock(&root->fs_info->free_chunk_lock);
2959 root->fs_info->free_chunk_space -= diff;
2960 spin_unlock(&root->fs_info->free_chunk_lock);
2961 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002962 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002963
Josef Bacikba1bf482009-09-11 16:11:19 -04002964again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002965 key.objectid = device->devid;
2966 key.offset = (u64)-1;
2967 key.type = BTRFS_DEV_EXTENT_KEY;
2968
2969 while (1) {
2970 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2971 if (ret < 0)
2972 goto done;
2973
2974 ret = btrfs_previous_item(root, path, 0, key.type);
2975 if (ret < 0)
2976 goto done;
2977 if (ret) {
2978 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002979 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002980 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002981 }
2982
2983 l = path->nodes[0];
2984 slot = path->slots[0];
2985 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2986
Josef Bacikba1bf482009-09-11 16:11:19 -04002987 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002988 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002989 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002990 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002991
2992 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2993 length = btrfs_dev_extent_length(l, dev_extent);
2994
Josef Bacikba1bf482009-09-11 16:11:19 -04002995 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002996 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002997 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002998 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002999
3000 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3001 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3002 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003003 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003004
3005 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3006 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003007 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003008 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003009 if (ret == -ENOSPC)
3010 failed++;
3011 key.offset -= 1;
3012 }
3013
3014 if (failed && !retried) {
3015 failed = 0;
3016 retried = true;
3017 goto again;
3018 } else if (failed && retried) {
3019 ret = -ENOSPC;
3020 lock_chunks(root);
3021
3022 device->total_bytes = old_size;
3023 if (device->writeable)
3024 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003025 spin_lock(&root->fs_info->free_chunk_lock);
3026 root->fs_info->free_chunk_space += diff;
3027 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003028 unlock_chunks(root);
3029 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003030 }
3031
Chris Balld6397ba2009-04-27 07:29:03 -04003032 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003033 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003034 if (IS_ERR(trans)) {
3035 ret = PTR_ERR(trans);
3036 goto done;
3037 }
3038
Chris Balld6397ba2009-04-27 07:29:03 -04003039 lock_chunks(root);
3040
3041 device->disk_total_bytes = new_size;
3042 /* Now btrfs_update_device() will change the on-disk size. */
3043 ret = btrfs_update_device(trans, device);
3044 if (ret) {
3045 unlock_chunks(root);
3046 btrfs_end_transaction(trans, root);
3047 goto done;
3048 }
3049 WARN_ON(diff > old_total);
3050 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3051 unlock_chunks(root);
3052 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003053done:
3054 btrfs_free_path(path);
3055 return ret;
3056}
3057
Li Zefan125ccb02011-12-08 15:07:24 +08003058static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003059 struct btrfs_key *key,
3060 struct btrfs_chunk *chunk, int item_size)
3061{
David Sterba6c417612011-04-13 15:41:04 +02003062 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003063 struct btrfs_disk_key disk_key;
3064 u32 array_size;
3065 u8 *ptr;
3066
3067 array_size = btrfs_super_sys_array_size(super_copy);
3068 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3069 return -EFBIG;
3070
3071 ptr = super_copy->sys_chunk_array + array_size;
3072 btrfs_cpu_key_to_disk(&disk_key, key);
3073 memcpy(ptr, &disk_key, sizeof(disk_key));
3074 ptr += sizeof(disk_key);
3075 memcpy(ptr, chunk, item_size);
3076 item_size += sizeof(disk_key);
3077 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3078 return 0;
3079}
3080
Miao Xieb2117a32011-01-05 10:07:28 +00003081/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003082 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003083 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003084static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003085{
Arne Jansen73c5de02011-04-12 12:07:57 +02003086 const struct btrfs_device_info *di_a = a;
3087 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003088
Arne Jansen73c5de02011-04-12 12:07:57 +02003089 if (di_a->max_avail > di_b->max_avail)
3090 return -1;
3091 if (di_a->max_avail < di_b->max_avail)
3092 return 1;
3093 if (di_a->total_avail > di_b->total_avail)
3094 return -1;
3095 if (di_a->total_avail < di_b->total_avail)
3096 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003097 return 0;
3098}
3099
3100static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3101 struct btrfs_root *extent_root,
3102 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003103 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003104 u64 start, u64 type)
3105{
3106 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003107 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3108 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003109 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003110 struct extent_map_tree *em_tree;
3111 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003112 struct btrfs_device_info *devices_info = NULL;
3113 u64 total_avail;
3114 int num_stripes; /* total number of stripes to allocate */
3115 int sub_stripes; /* sub_stripes info for map */
3116 int dev_stripes; /* stripes per dev */
3117 int devs_max; /* max devs to use */
3118 int devs_min; /* min devs needed */
3119 int devs_increment; /* ndevs has to be a multiple of this */
3120 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003121 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003122 u64 max_stripe_size;
3123 u64 max_chunk_size;
3124 u64 stripe_size;
3125 u64 num_bytes;
3126 int ndevs;
3127 int i;
3128 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003129
3130 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
3131 (type & BTRFS_BLOCK_GROUP_DUP)) {
3132 WARN_ON(1);
3133 type &= ~BTRFS_BLOCK_GROUP_DUP;
3134 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003135
Miao Xieb2117a32011-01-05 10:07:28 +00003136 if (list_empty(&fs_devices->alloc_list))
3137 return -ENOSPC;
3138
Arne Jansen73c5de02011-04-12 12:07:57 +02003139 sub_stripes = 1;
3140 dev_stripes = 1;
3141 devs_increment = 1;
3142 ncopies = 1;
3143 devs_max = 0; /* 0 == as many as possible */
3144 devs_min = 1;
3145
3146 /*
3147 * define the properties of each RAID type.
3148 * FIXME: move this to a global table and use it in all RAID
3149 * calculation code
3150 */
3151 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3152 dev_stripes = 2;
3153 ncopies = 2;
3154 devs_max = 1;
3155 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3156 devs_min = 2;
3157 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3158 devs_increment = 2;
3159 ncopies = 2;
3160 devs_max = 2;
3161 devs_min = 2;
3162 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3163 sub_stripes = 2;
3164 devs_increment = 2;
3165 ncopies = 2;
3166 devs_min = 4;
3167 } else {
3168 devs_max = 1;
3169 }
3170
3171 if (type & BTRFS_BLOCK_GROUP_DATA) {
3172 max_stripe_size = 1024 * 1024 * 1024;
3173 max_chunk_size = 10 * max_stripe_size;
3174 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003175 /* for larger filesystems, use larger metadata chunks */
3176 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3177 max_stripe_size = 1024 * 1024 * 1024;
3178 else
3179 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003180 max_chunk_size = max_stripe_size;
3181 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003182 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003183 max_chunk_size = 2 * max_stripe_size;
3184 } else {
3185 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3186 type);
3187 BUG_ON(1);
3188 }
3189
3190 /* we don't want a chunk larger than 10% of writeable space */
3191 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3192 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003193
3194 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3195 GFP_NOFS);
3196 if (!devices_info)
3197 return -ENOMEM;
3198
Arne Jansen73c5de02011-04-12 12:07:57 +02003199 cur = fs_devices->alloc_list.next;
3200
3201 /*
3202 * in the first pass through the devices list, we gather information
3203 * about the available holes on each device.
3204 */
3205 ndevs = 0;
3206 while (cur != &fs_devices->alloc_list) {
3207 struct btrfs_device *device;
3208 u64 max_avail;
3209 u64 dev_offset;
3210
3211 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3212
3213 cur = cur->next;
3214
3215 if (!device->writeable) {
3216 printk(KERN_ERR
3217 "btrfs: read-only device in alloc_list\n");
3218 WARN_ON(1);
3219 continue;
3220 }
3221
3222 if (!device->in_fs_metadata)
3223 continue;
3224
3225 if (device->total_bytes > device->bytes_used)
3226 total_avail = device->total_bytes - device->bytes_used;
3227 else
3228 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003229
3230 /* If there is no space on this device, skip it. */
3231 if (total_avail == 0)
3232 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003233
Li Zefan125ccb02011-12-08 15:07:24 +08003234 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003235 max_stripe_size * dev_stripes,
3236 &dev_offset, &max_avail);
3237 if (ret && ret != -ENOSPC)
3238 goto error;
3239
3240 if (ret == 0)
3241 max_avail = max_stripe_size * dev_stripes;
3242
3243 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3244 continue;
3245
3246 devices_info[ndevs].dev_offset = dev_offset;
3247 devices_info[ndevs].max_avail = max_avail;
3248 devices_info[ndevs].total_avail = total_avail;
3249 devices_info[ndevs].dev = device;
3250 ++ndevs;
3251 }
3252
3253 /*
3254 * now sort the devices by hole size / available space
3255 */
3256 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3257 btrfs_cmp_device_info, NULL);
3258
3259 /* round down to number of usable stripes */
3260 ndevs -= ndevs % devs_increment;
3261
3262 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3263 ret = -ENOSPC;
3264 goto error;
3265 }
3266
3267 if (devs_max && ndevs > devs_max)
3268 ndevs = devs_max;
3269 /*
3270 * the primary goal is to maximize the number of stripes, so use as many
3271 * devices as possible, even if the stripes are not maximum sized.
3272 */
3273 stripe_size = devices_info[ndevs-1].max_avail;
3274 num_stripes = ndevs * dev_stripes;
3275
3276 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3277 stripe_size = max_chunk_size * ncopies;
3278 do_div(stripe_size, num_stripes);
3279 }
3280
3281 do_div(stripe_size, dev_stripes);
3282 do_div(stripe_size, BTRFS_STRIPE_LEN);
3283 stripe_size *= BTRFS_STRIPE_LEN;
3284
Miao Xieb2117a32011-01-05 10:07:28 +00003285 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3286 if (!map) {
3287 ret = -ENOMEM;
3288 goto error;
3289 }
3290 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003291
Arne Jansen73c5de02011-04-12 12:07:57 +02003292 for (i = 0; i < ndevs; ++i) {
3293 for (j = 0; j < dev_stripes; ++j) {
3294 int s = i * dev_stripes + j;
3295 map->stripes[s].dev = devices_info[i].dev;
3296 map->stripes[s].physical = devices_info[i].dev_offset +
3297 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003298 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003299 }
Chris Mason593060d2008-03-25 16:50:33 -04003300 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003301 map->stripe_len = BTRFS_STRIPE_LEN;
3302 map->io_align = BTRFS_STRIPE_LEN;
3303 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003304 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003305 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003306
Yan Zheng2b820322008-11-17 21:11:30 -05003307 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003308 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003309
Arne Jansen73c5de02011-04-12 12:07:57 +02003310 *stripe_size_out = stripe_size;
3311 *num_bytes_out = num_bytes;
3312
3313 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003314
David Sterba172ddd62011-04-21 00:48:27 +02003315 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003316 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003317 ret = -ENOMEM;
3318 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003319 }
Chris Mason0b86a832008-03-24 15:01:56 -04003320 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003321 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003322 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003323 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003324 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003325
Chris Mason0b86a832008-03-24 15:01:56 -04003326 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003327 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003328 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003329 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003330 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003331 if (ret)
3332 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003333
3334 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3335 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003336 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05003337 BUG_ON(ret);
3338
Arne Jansen73c5de02011-04-12 12:07:57 +02003339 for (i = 0; i < map->num_stripes; ++i) {
3340 struct btrfs_device *device;
3341 u64 dev_offset;
3342
3343 device = map->stripes[i].dev;
3344 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003345
3346 ret = btrfs_alloc_dev_extent(trans, device,
3347 info->chunk_root->root_key.objectid,
3348 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003349 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003350 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003351 }
3352
Miao Xieb2117a32011-01-05 10:07:28 +00003353 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003354 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003355
3356error:
3357 kfree(map);
3358 kfree(devices_info);
3359 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003360}
3361
3362static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3363 struct btrfs_root *extent_root,
3364 struct map_lookup *map, u64 chunk_offset,
3365 u64 chunk_size, u64 stripe_size)
3366{
3367 u64 dev_offset;
3368 struct btrfs_key key;
3369 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3370 struct btrfs_device *device;
3371 struct btrfs_chunk *chunk;
3372 struct btrfs_stripe *stripe;
3373 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3374 int index = 0;
3375 int ret;
3376
3377 chunk = kzalloc(item_size, GFP_NOFS);
3378 if (!chunk)
3379 return -ENOMEM;
3380
3381 index = 0;
3382 while (index < map->num_stripes) {
3383 device = map->stripes[index].dev;
3384 device->bytes_used += stripe_size;
3385 ret = btrfs_update_device(trans, device);
3386 BUG_ON(ret);
3387 index++;
3388 }
3389
Josef Bacik2bf64752011-09-26 17:12:22 -04003390 spin_lock(&extent_root->fs_info->free_chunk_lock);
3391 extent_root->fs_info->free_chunk_space -= (stripe_size *
3392 map->num_stripes);
3393 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3394
Yan Zheng2b820322008-11-17 21:11:30 -05003395 index = 0;
3396 stripe = &chunk->stripe;
3397 while (index < map->num_stripes) {
3398 device = map->stripes[index].dev;
3399 dev_offset = map->stripes[index].physical;
3400
3401 btrfs_set_stack_stripe_devid(stripe, device->devid);
3402 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3403 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3404 stripe++;
3405 index++;
3406 }
3407
3408 btrfs_set_stack_chunk_length(chunk, chunk_size);
3409 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3410 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3411 btrfs_set_stack_chunk_type(chunk, map->type);
3412 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3413 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3414 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3415 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3416 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3417
3418 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3419 key.type = BTRFS_CHUNK_ITEM_KEY;
3420 key.offset = chunk_offset;
3421
3422 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003423
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003424 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3425 /*
3426 * TODO: Cleanup of inserted chunk root in case of
3427 * failure.
3428 */
Li Zefan125ccb02011-12-08 15:07:24 +08003429 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003430 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003431 }
liubo1abe9b82011-03-24 11:18:59 +00003432
Yan Zheng2b820322008-11-17 21:11:30 -05003433 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003434 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003435}
3436
3437/*
3438 * Chunk allocation falls into two parts. The first part does works
3439 * that make the new allocated chunk useable, but not do any operation
3440 * that modifies the chunk tree. The second part does the works that
3441 * require modifying the chunk tree. This division is important for the
3442 * bootstrap process of adding storage to a seed btrfs.
3443 */
3444int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3445 struct btrfs_root *extent_root, u64 type)
3446{
3447 u64 chunk_offset;
3448 u64 chunk_size;
3449 u64 stripe_size;
3450 struct map_lookup *map;
3451 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3452 int ret;
3453
3454 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3455 &chunk_offset);
3456 if (ret)
3457 return ret;
3458
3459 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3460 &stripe_size, chunk_offset, type);
3461 if (ret)
3462 return ret;
3463
3464 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3465 chunk_size, stripe_size);
3466 BUG_ON(ret);
3467 return 0;
3468}
3469
Chris Masond3977122009-01-05 21:25:51 -05003470static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003471 struct btrfs_root *root,
3472 struct btrfs_device *device)
3473{
3474 u64 chunk_offset;
3475 u64 sys_chunk_offset;
3476 u64 chunk_size;
3477 u64 sys_chunk_size;
3478 u64 stripe_size;
3479 u64 sys_stripe_size;
3480 u64 alloc_profile;
3481 struct map_lookup *map;
3482 struct map_lookup *sys_map;
3483 struct btrfs_fs_info *fs_info = root->fs_info;
3484 struct btrfs_root *extent_root = fs_info->extent_root;
3485 int ret;
3486
3487 ret = find_next_chunk(fs_info->chunk_root,
3488 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003489 if (ret)
3490 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003491
3492 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003493 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003494 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3495
3496 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3497 &stripe_size, chunk_offset, alloc_profile);
3498 BUG_ON(ret);
3499
3500 sys_chunk_offset = chunk_offset + chunk_size;
3501
3502 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003503 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003504 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3505
3506 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3507 &sys_chunk_size, &sys_stripe_size,
3508 sys_chunk_offset, alloc_profile);
3509 BUG_ON(ret);
3510
3511 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3512 BUG_ON(ret);
3513
3514 /*
3515 * Modifying chunk tree needs allocating new blocks from both
3516 * system block group and metadata block group. So we only can
3517 * do operations require modifying the chunk tree after both
3518 * block groups were created.
3519 */
3520 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3521 chunk_size, stripe_size);
3522 BUG_ON(ret);
3523
3524 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3525 sys_chunk_offset, sys_chunk_size,
3526 sys_stripe_size);
3527 BUG_ON(ret);
3528 return 0;
3529}
3530
3531int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3532{
3533 struct extent_map *em;
3534 struct map_lookup *map;
3535 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3536 int readonly = 0;
3537 int i;
3538
Chris Mason890871b2009-09-02 16:24:52 -04003539 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003540 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003541 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003542 if (!em)
3543 return 1;
3544
Josef Bacikf48b9072010-01-27 02:07:59 +00003545 if (btrfs_test_opt(root, DEGRADED)) {
3546 free_extent_map(em);
3547 return 0;
3548 }
3549
Yan Zheng2b820322008-11-17 21:11:30 -05003550 map = (struct map_lookup *)em->bdev;
3551 for (i = 0; i < map->num_stripes; i++) {
3552 if (!map->stripes[i].dev->writeable) {
3553 readonly = 1;
3554 break;
3555 }
3556 }
3557 free_extent_map(em);
3558 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003559}
3560
3561void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3562{
David Sterbaa8067e02011-04-21 00:34:43 +02003563 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003564}
3565
3566void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3567{
3568 struct extent_map *em;
3569
Chris Masond3977122009-01-05 21:25:51 -05003570 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003571 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003572 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3573 if (em)
3574 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003575 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003576 if (!em)
3577 break;
3578 kfree(em->bdev);
3579 /* once for us */
3580 free_extent_map(em);
3581 /* once for the tree */
3582 free_extent_map(em);
3583 }
3584}
3585
Chris Masonf1885912008-04-09 16:28:12 -04003586int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3587{
3588 struct extent_map *em;
3589 struct map_lookup *map;
3590 struct extent_map_tree *em_tree = &map_tree->map_tree;
3591 int ret;
3592
Chris Mason890871b2009-09-02 16:24:52 -04003593 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003594 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003595 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003596 BUG_ON(!em);
3597
3598 BUG_ON(em->start > logical || em->start + em->len < logical);
3599 map = (struct map_lookup *)em->bdev;
3600 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3601 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003602 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3603 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003604 else
3605 ret = 1;
3606 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003607 return ret;
3608}
3609
Chris Masondfe25022008-05-13 13:46:40 -04003610static int find_live_mirror(struct map_lookup *map, int first, int num,
3611 int optimal)
3612{
3613 int i;
3614 if (map->stripes[optimal].dev->bdev)
3615 return optimal;
3616 for (i = first; i < first + num; i++) {
3617 if (map->stripes[i].dev->bdev)
3618 return i;
3619 }
3620 /* we couldn't find one that doesn't fail. Just return something
3621 * and the io error handling code will clean up eventually
3622 */
3623 return optimal;
3624}
3625
Chris Masonf2d8d742008-04-21 10:03:05 -04003626static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3627 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003628 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003629 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003630{
3631 struct extent_map *em;
3632 struct map_lookup *map;
3633 struct extent_map_tree *em_tree = &map_tree->map_tree;
3634 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003635 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003636 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003637 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003638 u64 stripe_nr_orig;
3639 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003640 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003641 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003642 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003643 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003644 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003645 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003646
Chris Mason890871b2009-09-02 16:24:52 -04003647 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003648 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003649 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003650
Chris Mason3b951512008-04-17 11:29:12 -04003651 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003652 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3653 (unsigned long long)logical,
3654 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003655 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003656 }
Chris Mason0b86a832008-03-24 15:01:56 -04003657
3658 BUG_ON(em->start > logical || em->start + em->len < logical);
3659 map = (struct map_lookup *)em->bdev;
3660 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003661
Chris Masonf1885912008-04-09 16:28:12 -04003662 if (mirror_num > map->num_stripes)
3663 mirror_num = 0;
3664
Chris Mason593060d2008-03-25 16:50:33 -04003665 stripe_nr = offset;
3666 /*
3667 * stripe_nr counts the total number of stripes we have to stride
3668 * to get to this block
3669 */
3670 do_div(stripe_nr, map->stripe_len);
3671
3672 stripe_offset = stripe_nr * map->stripe_len;
3673 BUG_ON(offset < stripe_offset);
3674
3675 /* stripe_offset is the offset of this block in its stripe*/
3676 stripe_offset = offset - stripe_offset;
3677
Li Dongyangfce3bb92011-03-24 10:24:26 +00003678 if (rw & REQ_DISCARD)
3679 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003680 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003681 /* we limit the length of each bio to what fits in a stripe */
3682 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003683 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003684 } else {
3685 *length = em->len - offset;
3686 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003687
Jan Schmidta1d3c472011-08-04 17:15:33 +02003688 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003689 goto out;
3690
Chris Masonf2d8d742008-04-21 10:03:05 -04003691 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003692 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003693 stripe_nr_orig = stripe_nr;
3694 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3695 (~(map->stripe_len - 1));
3696 do_div(stripe_nr_end, map->stripe_len);
3697 stripe_end_offset = stripe_nr_end * map->stripe_len -
3698 (offset + *length);
3699 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3700 if (rw & REQ_DISCARD)
3701 num_stripes = min_t(u64, map->num_stripes,
3702 stripe_nr_end - stripe_nr_orig);
3703 stripe_index = do_div(stripe_nr, map->num_stripes);
3704 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003705 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003706 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003707 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003708 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003709 else {
3710 stripe_index = find_live_mirror(map, 0,
3711 map->num_stripes,
3712 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003713 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003714 }
Chris Mason2fff7342008-04-29 14:12:09 -04003715
Chris Mason611f0e02008-04-03 16:29:03 -04003716 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003717 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003718 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003719 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003720 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003721 } else {
3722 mirror_num = 1;
3723 }
Chris Mason2fff7342008-04-29 14:12:09 -04003724
Chris Mason321aecc2008-04-16 10:49:51 -04003725 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3726 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003727
3728 stripe_index = do_div(stripe_nr, factor);
3729 stripe_index *= map->sub_stripes;
3730
Jens Axboe7eaceac2011-03-10 08:52:07 +01003731 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003732 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003733 else if (rw & REQ_DISCARD)
3734 num_stripes = min_t(u64, map->sub_stripes *
3735 (stripe_nr_end - stripe_nr_orig),
3736 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003737 else if (mirror_num)
3738 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003739 else {
3740 stripe_index = find_live_mirror(map, stripe_index,
3741 map->sub_stripes, stripe_index +
3742 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003743 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003744 }
Chris Mason8790d502008-04-03 16:29:03 -04003745 } else {
3746 /*
3747 * after this do_div call, stripe_nr is the number of stripes
3748 * on this device we have to walk to find the data, and
3749 * stripe_index is the number of our device in the stripe array
3750 */
3751 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003752 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003753 }
Chris Mason593060d2008-03-25 16:50:33 -04003754 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003755
Li Zefande11cc12011-12-01 12:55:47 +08003756 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3757 if (!bbio) {
3758 ret = -ENOMEM;
3759 goto out;
3760 }
3761 atomic_set(&bbio->error, 0);
3762
Li Dongyangfce3bb92011-03-24 10:24:26 +00003763 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003764 int factor = 0;
3765 int sub_stripes = 0;
3766 u64 stripes_per_dev = 0;
3767 u32 remaining_stripes = 0;
3768
3769 if (map->type &
3770 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3771 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3772 sub_stripes = 1;
3773 else
3774 sub_stripes = map->sub_stripes;
3775
3776 factor = map->num_stripes / sub_stripes;
3777 stripes_per_dev = div_u64_rem(stripe_nr_end -
3778 stripe_nr_orig,
3779 factor,
3780 &remaining_stripes);
3781 }
3782
Li Dongyangfce3bb92011-03-24 10:24:26 +00003783 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003784 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003785 map->stripes[stripe_index].physical +
3786 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003787 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003788
Li Zefanec9ef7a2011-12-01 14:06:42 +08003789 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3790 BTRFS_BLOCK_GROUP_RAID10)) {
3791 bbio->stripes[i].length = stripes_per_dev *
3792 map->stripe_len;
3793 if (i / sub_stripes < remaining_stripes)
3794 bbio->stripes[i].length +=
3795 map->stripe_len;
3796 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003797 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003798 stripe_offset;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003799 if ((i / sub_stripes + 1) %
3800 sub_stripes == remaining_stripes)
3801 bbio->stripes[i].length -=
3802 stripe_end_offset;
3803 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003804 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003805 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003806 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003807
3808 stripe_index++;
3809 if (stripe_index == map->num_stripes) {
3810 /* This could only happen for RAID0/10 */
3811 stripe_index = 0;
3812 stripe_nr++;
3813 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003814 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003815 } else {
3816 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003817 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003818 map->stripes[stripe_index].physical +
3819 stripe_offset +
3820 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003821 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003822 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003823 stripe_index++;
3824 }
Chris Mason593060d2008-03-25 16:50:33 -04003825 }
Li Zefande11cc12011-12-01 12:55:47 +08003826
3827 if (rw & REQ_WRITE) {
3828 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3829 BTRFS_BLOCK_GROUP_RAID10 |
3830 BTRFS_BLOCK_GROUP_DUP)) {
3831 max_errors = 1;
3832 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003833 }
Li Zefande11cc12011-12-01 12:55:47 +08003834
3835 *bbio_ret = bbio;
3836 bbio->num_stripes = num_stripes;
3837 bbio->max_errors = max_errors;
3838 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003839out:
Chris Mason0b86a832008-03-24 15:01:56 -04003840 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003841 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003842}
3843
Chris Masonf2d8d742008-04-21 10:03:05 -04003844int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3845 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003846 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003847{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003848 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003849 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003850}
3851
Yan Zhenga512bbf2008-12-08 16:46:26 -05003852int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3853 u64 chunk_start, u64 physical, u64 devid,
3854 u64 **logical, int *naddrs, int *stripe_len)
3855{
3856 struct extent_map_tree *em_tree = &map_tree->map_tree;
3857 struct extent_map *em;
3858 struct map_lookup *map;
3859 u64 *buf;
3860 u64 bytenr;
3861 u64 length;
3862 u64 stripe_nr;
3863 int i, j, nr = 0;
3864
Chris Mason890871b2009-09-02 16:24:52 -04003865 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003866 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003867 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003868
3869 BUG_ON(!em || em->start != chunk_start);
3870 map = (struct map_lookup *)em->bdev;
3871
3872 length = em->len;
3873 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3874 do_div(length, map->num_stripes / map->sub_stripes);
3875 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3876 do_div(length, map->num_stripes);
3877
3878 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3879 BUG_ON(!buf);
3880
3881 for (i = 0; i < map->num_stripes; i++) {
3882 if (devid && map->stripes[i].dev->devid != devid)
3883 continue;
3884 if (map->stripes[i].physical > physical ||
3885 map->stripes[i].physical + length <= physical)
3886 continue;
3887
3888 stripe_nr = physical - map->stripes[i].physical;
3889 do_div(stripe_nr, map->stripe_len);
3890
3891 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3892 stripe_nr = stripe_nr * map->num_stripes + i;
3893 do_div(stripe_nr, map->sub_stripes);
3894 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3895 stripe_nr = stripe_nr * map->num_stripes + i;
3896 }
3897 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003898 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003899 for (j = 0; j < nr; j++) {
3900 if (buf[j] == bytenr)
3901 break;
3902 }
Chris Mason934d3752008-12-08 16:43:10 -05003903 if (j == nr) {
3904 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003905 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003906 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003907 }
3908
Yan Zhenga512bbf2008-12-08 16:46:26 -05003909 *logical = buf;
3910 *naddrs = nr;
3911 *stripe_len = map->stripe_len;
3912
3913 free_extent_map(em);
3914 return 0;
3915}
3916
Jan Schmidta1d3c472011-08-04 17:15:33 +02003917static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003918{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003919 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003920 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003921
Chris Mason8790d502008-04-03 16:29:03 -04003922 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003923 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003924
Jan Schmidta1d3c472011-08-04 17:15:33 +02003925 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003926 is_orig_bio = 1;
3927
Jan Schmidta1d3c472011-08-04 17:15:33 +02003928 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003929 if (!is_orig_bio) {
3930 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003931 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003932 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003933 bio->bi_private = bbio->private;
3934 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003935 bio->bi_bdev = (struct block_device *)
3936 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003937 /* only send an error to the higher layers if it is
3938 * beyond the tolerance of the multi-bio
3939 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003940 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003941 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003942 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003943 /*
3944 * this bio is actually up to date, we didn't
3945 * go over the max number of errors
3946 */
3947 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003948 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003949 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003950 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003951
3952 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003953 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003954 bio_put(bio);
3955 }
Chris Mason8790d502008-04-03 16:29:03 -04003956}
3957
Chris Mason8b712842008-06-11 16:50:36 -04003958struct async_sched {
3959 struct bio *bio;
3960 int rw;
3961 struct btrfs_fs_info *info;
3962 struct btrfs_work work;
3963};
3964
3965/*
3966 * see run_scheduled_bios for a description of why bios are collected for
3967 * async submit.
3968 *
3969 * This will add one bio to the pending list for a device and make sure
3970 * the work struct is scheduled.
3971 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003972static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003973 struct btrfs_device *device,
3974 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003975{
3976 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003977 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003978
3979 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003980 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003981 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01003982 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003983 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003984 return;
Chris Mason8b712842008-06-11 16:50:36 -04003985 }
3986
3987 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003988 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003989 * higher layers. Otherwise, the async bio makes it appear we have
3990 * made progress against dirty pages when we've really just put it
3991 * on a queue for later
3992 */
Chris Mason0986fe92008-08-15 15:34:15 -04003993 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003994 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003995 bio->bi_next = NULL;
3996 bio->bi_rw |= rw;
3997
3998 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003999 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004000 pending_bios = &device->pending_sync_bios;
4001 else
4002 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004003
Chris Masonffbd5172009-04-20 15:50:09 -04004004 if (pending_bios->tail)
4005 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004006
Chris Masonffbd5172009-04-20 15:50:09 -04004007 pending_bios->tail = bio;
4008 if (!pending_bios->head)
4009 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004010 if (device->running_pending)
4011 should_queue = 0;
4012
4013 spin_unlock(&device->io_lock);
4014
4015 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004016 btrfs_queue_worker(&root->fs_info->submit_workers,
4017 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004018}
4019
Chris Masonf1885912008-04-09 16:28:12 -04004020int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004021 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004022{
4023 struct btrfs_mapping_tree *map_tree;
4024 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004025 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004026 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004027 u64 length = 0;
4028 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004029 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004030 int dev_nr = 0;
4031 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004032 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004033
Chris Masonf2d8d742008-04-21 10:03:05 -04004034 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004035 map_tree = &root->fs_info->mapping_tree;
4036 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004037
Jan Schmidta1d3c472011-08-04 17:15:33 +02004038 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004039 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04004040 BUG_ON(ret);
4041
Jan Schmidta1d3c472011-08-04 17:15:33 +02004042 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004043 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004044 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4045 "len %llu\n", (unsigned long long)logical,
4046 (unsigned long long)length,
4047 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004048 BUG();
4049 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004050
4051 bbio->orig_bio = first_bio;
4052 bbio->private = first_bio->bi_private;
4053 bbio->end_io = first_bio->bi_end_io;
4054 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004055
Chris Masond3977122009-01-05 21:25:51 -05004056 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004057 if (dev_nr < total_devs - 1) {
4058 bio = bio_clone(first_bio, GFP_NOFS);
4059 BUG_ON(!bio);
4060 } else {
4061 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004062 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004063 bio->bi_private = bbio;
4064 bio->bi_end_io = btrfs_end_bio;
4065 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4066 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004067 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004068 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4069 "(%s id %llu), size=%u\n", rw,
4070 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4071 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004072 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004073 if (async_submit)
4074 schedule_bio(root, dev, rw, bio);
4075 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004076 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004077 } else {
4078 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4079 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004080 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004081 }
Chris Mason8790d502008-04-03 16:29:03 -04004082 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004083 }
Chris Mason0b86a832008-03-24 15:01:56 -04004084 return 0;
4085}
4086
Chris Masona4437552008-04-18 10:29:38 -04004087struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004088 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004089{
Yan Zheng2b820322008-11-17 21:11:30 -05004090 struct btrfs_device *device;
4091 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004092
Yan Zheng2b820322008-11-17 21:11:30 -05004093 cur_devices = root->fs_info->fs_devices;
4094 while (cur_devices) {
4095 if (!fsid ||
4096 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4097 device = __find_device(&cur_devices->devices,
4098 devid, uuid);
4099 if (device)
4100 return device;
4101 }
4102 cur_devices = cur_devices->seed;
4103 }
4104 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004105}
4106
Chris Masondfe25022008-05-13 13:46:40 -04004107static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4108 u64 devid, u8 *dev_uuid)
4109{
4110 struct btrfs_device *device;
4111 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4112
4113 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004114 if (!device)
4115 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004116 list_add(&device->dev_list,
4117 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004118 device->dev_root = root->fs_info->dev_root;
4119 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004120 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004121 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004122 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004123 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004124 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004125 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004126 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004127 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4128 return device;
4129}
4130
Chris Mason0b86a832008-03-24 15:01:56 -04004131static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4132 struct extent_buffer *leaf,
4133 struct btrfs_chunk *chunk)
4134{
4135 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4136 struct map_lookup *map;
4137 struct extent_map *em;
4138 u64 logical;
4139 u64 length;
4140 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004141 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004142 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004143 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004144 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004145
Chris Masone17cade2008-04-15 15:41:47 -04004146 logical = key->offset;
4147 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004148
Chris Mason890871b2009-09-02 16:24:52 -04004149 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004150 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004151 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004152
4153 /* already mapped? */
4154 if (em && em->start <= logical && em->start + em->len > logical) {
4155 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004156 return 0;
4157 } else if (em) {
4158 free_extent_map(em);
4159 }
Chris Mason0b86a832008-03-24 15:01:56 -04004160
David Sterba172ddd62011-04-21 00:48:27 +02004161 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004162 if (!em)
4163 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004164 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4165 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004166 if (!map) {
4167 free_extent_map(em);
4168 return -ENOMEM;
4169 }
4170
4171 em->bdev = (struct block_device *)map;
4172 em->start = logical;
4173 em->len = length;
4174 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004175 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004176
Chris Mason593060d2008-03-25 16:50:33 -04004177 map->num_stripes = num_stripes;
4178 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4179 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4180 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4181 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4182 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004183 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004184 for (i = 0; i < num_stripes; i++) {
4185 map->stripes[i].physical =
4186 btrfs_stripe_offset_nr(leaf, chunk, i);
4187 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004188 read_extent_buffer(leaf, uuid, (unsigned long)
4189 btrfs_stripe_dev_uuid_nr(chunk, i),
4190 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004191 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4192 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004193 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004194 kfree(map);
4195 free_extent_map(em);
4196 return -EIO;
4197 }
Chris Masondfe25022008-05-13 13:46:40 -04004198 if (!map->stripes[i].dev) {
4199 map->stripes[i].dev =
4200 add_missing_dev(root, devid, uuid);
4201 if (!map->stripes[i].dev) {
4202 kfree(map);
4203 free_extent_map(em);
4204 return -EIO;
4205 }
4206 }
4207 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004208 }
4209
Chris Mason890871b2009-09-02 16:24:52 -04004210 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004211 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004212 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04004213 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04004214 free_extent_map(em);
4215
4216 return 0;
4217}
4218
Jeff Mahoney143bede2012-03-01 14:56:26 +01004219static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004220 struct btrfs_dev_item *dev_item,
4221 struct btrfs_device *device)
4222{
4223 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004224
4225 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004226 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4227 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004228 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4229 device->type = btrfs_device_type(leaf, dev_item);
4230 device->io_align = btrfs_device_io_align(leaf, dev_item);
4231 device->io_width = btrfs_device_io_width(leaf, dev_item);
4232 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004233
4234 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004235 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004236}
4237
Yan Zheng2b820322008-11-17 21:11:30 -05004238static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4239{
4240 struct btrfs_fs_devices *fs_devices;
4241 int ret;
4242
Li Zefanb367e472011-12-07 11:38:24 +08004243 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004244
4245 fs_devices = root->fs_info->fs_devices->seed;
4246 while (fs_devices) {
4247 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4248 ret = 0;
4249 goto out;
4250 }
4251 fs_devices = fs_devices->seed;
4252 }
4253
4254 fs_devices = find_fsid(fsid);
4255 if (!fs_devices) {
4256 ret = -ENOENT;
4257 goto out;
4258 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004259
4260 fs_devices = clone_fs_devices(fs_devices);
4261 if (IS_ERR(fs_devices)) {
4262 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004263 goto out;
4264 }
4265
Christoph Hellwig97288f22008-12-02 06:36:09 -05004266 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004267 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004268 if (ret)
4269 goto out;
4270
4271 if (!fs_devices->seeding) {
4272 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004273 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004274 ret = -EINVAL;
4275 goto out;
4276 }
4277
4278 fs_devices->seed = root->fs_info->fs_devices->seed;
4279 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004280out:
Yan Zheng2b820322008-11-17 21:11:30 -05004281 return ret;
4282}
4283
Chris Mason0d81ba52008-03-24 15:02:07 -04004284static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004285 struct extent_buffer *leaf,
4286 struct btrfs_dev_item *dev_item)
4287{
4288 struct btrfs_device *device;
4289 u64 devid;
4290 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004291 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004292 u8 dev_uuid[BTRFS_UUID_SIZE];
4293
Chris Mason0b86a832008-03-24 15:01:56 -04004294 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004295 read_extent_buffer(leaf, dev_uuid,
4296 (unsigned long)btrfs_device_uuid(dev_item),
4297 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004298 read_extent_buffer(leaf, fs_uuid,
4299 (unsigned long)btrfs_device_fsid(dev_item),
4300 BTRFS_UUID_SIZE);
4301
4302 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4303 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004304 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004305 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004306 }
4307
4308 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4309 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004310 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004311 return -EIO;
4312
4313 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004314 printk(KERN_WARNING "warning devid %llu missing\n",
4315 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004316 device = add_missing_dev(root, devid, dev_uuid);
4317 if (!device)
4318 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004319 } else if (!device->missing) {
4320 /*
4321 * this happens when a device that was properly setup
4322 * in the device info lists suddenly goes bad.
4323 * device->bdev is NULL, and so we have to set
4324 * device->missing to one here
4325 */
4326 root->fs_info->fs_devices->missing_devices++;
4327 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004328 }
4329 }
4330
4331 if (device->fs_devices != root->fs_info->fs_devices) {
4332 BUG_ON(device->writeable);
4333 if (device->generation !=
4334 btrfs_device_generation(leaf, dev_item))
4335 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004336 }
Chris Mason0b86a832008-03-24 15:01:56 -04004337
4338 fill_device_from_item(leaf, dev_item, device);
4339 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004340 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004341 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004342 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004343 spin_lock(&root->fs_info->free_chunk_lock);
4344 root->fs_info->free_chunk_space += device->total_bytes -
4345 device->bytes_used;
4346 spin_unlock(&root->fs_info->free_chunk_lock);
4347 }
Chris Mason0b86a832008-03-24 15:01:56 -04004348 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004349 return ret;
4350}
4351
Yan Zhenge4404d62008-12-12 10:03:26 -05004352int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004353{
David Sterba6c417612011-04-13 15:41:04 +02004354 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004355 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004356 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004357 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004358 u8 *ptr;
4359 unsigned long sb_ptr;
4360 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004361 u32 num_stripes;
4362 u32 array_size;
4363 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004364 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004365 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004366
Yan Zhenge4404d62008-12-12 10:03:26 -05004367 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004368 BTRFS_SUPER_INFO_SIZE);
4369 if (!sb)
4370 return -ENOMEM;
4371 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004372 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004373 /*
4374 * The sb extent buffer is artifical and just used to read the system array.
4375 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4376 * pages up-to-date when the page is larger: extent does not cover the
4377 * whole page and consequently check_page_uptodate does not find all
4378 * the page's extents up-to-date (the hole beyond sb),
4379 * write_extent_buffer then triggers a WARN_ON.
4380 *
4381 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4382 * but sb spans only this function. Add an explicit SetPageUptodate call
4383 * to silence the warning eg. on PowerPC 64.
4384 */
4385 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
4386 SetPageUptodate(sb->first_page);
Chris Mason4008c042009-02-12 14:09:45 -05004387
Chris Masona061fc82008-05-07 11:43:44 -04004388 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004389 array_size = btrfs_super_sys_array_size(super_copy);
4390
Chris Mason0b86a832008-03-24 15:01:56 -04004391 ptr = super_copy->sys_chunk_array;
4392 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4393 cur = 0;
4394
4395 while (cur < array_size) {
4396 disk_key = (struct btrfs_disk_key *)ptr;
4397 btrfs_disk_key_to_cpu(&key, disk_key);
4398
Chris Masona061fc82008-05-07 11:43:44 -04004399 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004400 sb_ptr += len;
4401 cur += len;
4402
Chris Mason0d81ba52008-03-24 15:02:07 -04004403 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004404 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004405 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004406 if (ret)
4407 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004408 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4409 len = btrfs_chunk_item_size(num_stripes);
4410 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004411 ret = -EIO;
4412 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004413 }
4414 ptr += len;
4415 sb_ptr += len;
4416 cur += len;
4417 }
Chris Masona061fc82008-05-07 11:43:44 -04004418 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004419 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004420}
4421
4422int btrfs_read_chunk_tree(struct btrfs_root *root)
4423{
4424 struct btrfs_path *path;
4425 struct extent_buffer *leaf;
4426 struct btrfs_key key;
4427 struct btrfs_key found_key;
4428 int ret;
4429 int slot;
4430
4431 root = root->fs_info->chunk_root;
4432
4433 path = btrfs_alloc_path();
4434 if (!path)
4435 return -ENOMEM;
4436
Li Zefanb367e472011-12-07 11:38:24 +08004437 mutex_lock(&uuid_mutex);
4438 lock_chunks(root);
4439
Chris Mason0b86a832008-03-24 15:01:56 -04004440 /* first we search for all of the device items, and then we
4441 * read in all of the chunk items. This way we can create chunk
4442 * mappings that reference all of the devices that are afound
4443 */
4444 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4445 key.offset = 0;
4446 key.type = 0;
4447again:
4448 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004449 if (ret < 0)
4450 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004451 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004452 leaf = path->nodes[0];
4453 slot = path->slots[0];
4454 if (slot >= btrfs_header_nritems(leaf)) {
4455 ret = btrfs_next_leaf(root, path);
4456 if (ret == 0)
4457 continue;
4458 if (ret < 0)
4459 goto error;
4460 break;
4461 }
4462 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4463 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4464 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4465 break;
4466 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4467 struct btrfs_dev_item *dev_item;
4468 dev_item = btrfs_item_ptr(leaf, slot,
4469 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004470 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004471 if (ret)
4472 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004473 }
4474 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4475 struct btrfs_chunk *chunk;
4476 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4477 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004478 if (ret)
4479 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004480 }
4481 path->slots[0]++;
4482 }
4483 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4484 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004485 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004486 goto again;
4487 }
Chris Mason0b86a832008-03-24 15:01:56 -04004488 ret = 0;
4489error:
Li Zefanb367e472011-12-07 11:38:24 +08004490 unlock_chunks(root);
4491 mutex_unlock(&uuid_mutex);
4492
Yan Zheng2b820322008-11-17 21:11:30 -05004493 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004494 return ret;
4495}