blob: 57305e88ea827b4bc65e47bc13787bfa70b40054 [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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100552 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000553 memcpy(new_device, device, sizeof(*new_device));
554 new_device->name = kstrdup(device->name, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100555 BUG_ON(device->name && !new_device->name); /* -ENOMEM */
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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039 } else {
1040 btrfs_error(root->fs_info, ret, "Slot search failed");
1041 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001042 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001043
Josef Bacik2bf64752011-09-26 17:12:22 -04001044 if (device->bytes_used > 0) {
1045 u64 len = btrfs_dev_extent_length(leaf, extent);
1046 device->bytes_used -= len;
1047 spin_lock(&root->fs_info->free_chunk_lock);
1048 root->fs_info->free_chunk_space += len;
1049 spin_unlock(&root->fs_info->free_chunk_lock);
1050 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001051 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001052 if (ret) {
1053 btrfs_error(root->fs_info, ret,
1054 "Failed to remove dev extent item");
1055 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001056out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001057 btrfs_free_path(path);
1058 return ret;
1059}
1060
Yan Zheng2b820322008-11-17 21:11:30 -05001061int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001062 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001063 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001064 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001065{
1066 int ret;
1067 struct btrfs_path *path;
1068 struct btrfs_root *root = device->dev_root;
1069 struct btrfs_dev_extent *extent;
1070 struct extent_buffer *leaf;
1071 struct btrfs_key key;
1072
Chris Masondfe25022008-05-13 13:46:40 -04001073 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001074 path = btrfs_alloc_path();
1075 if (!path)
1076 return -ENOMEM;
1077
Chris Mason0b86a832008-03-24 15:01:56 -04001078 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001079 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001080 key.type = BTRFS_DEV_EXTENT_KEY;
1081 ret = btrfs_insert_empty_item(trans, root, path, &key,
1082 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001083 if (ret)
1084 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001085
1086 leaf = path->nodes[0];
1087 extent = btrfs_item_ptr(leaf, path->slots[0],
1088 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001089 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1090 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1091 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1092
1093 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1094 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1095 BTRFS_UUID_SIZE);
1096
Chris Mason0b86a832008-03-24 15:01:56 -04001097 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1098 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001099out:
Chris Mason0b86a832008-03-24 15:01:56 -04001100 btrfs_free_path(path);
1101 return ret;
1102}
1103
Chris Masona1b32a52008-09-05 16:09:51 -04001104static noinline int find_next_chunk(struct btrfs_root *root,
1105 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001106{
1107 struct btrfs_path *path;
1108 int ret;
1109 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001110 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001111 struct btrfs_key found_key;
1112
1113 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001114 if (!path)
1115 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001116
Chris Masone17cade2008-04-15 15:41:47 -04001117 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001118 key.offset = (u64)-1;
1119 key.type = BTRFS_CHUNK_ITEM_KEY;
1120
1121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1122 if (ret < 0)
1123 goto error;
1124
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001125 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001126
1127 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1128 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001129 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001130 } else {
1131 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1132 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001133 if (found_key.objectid != objectid)
1134 *offset = 0;
1135 else {
1136 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1137 struct btrfs_chunk);
1138 *offset = found_key.offset +
1139 btrfs_chunk_length(path->nodes[0], chunk);
1140 }
Chris Mason0b86a832008-03-24 15:01:56 -04001141 }
1142 ret = 0;
1143error:
1144 btrfs_free_path(path);
1145 return ret;
1146}
1147
Yan Zheng2b820322008-11-17 21:11:30 -05001148static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001149{
1150 int ret;
1151 struct btrfs_key key;
1152 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001153 struct btrfs_path *path;
1154
1155 root = root->fs_info->chunk_root;
1156
1157 path = btrfs_alloc_path();
1158 if (!path)
1159 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001160
1161 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1162 key.type = BTRFS_DEV_ITEM_KEY;
1163 key.offset = (u64)-1;
1164
1165 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1166 if (ret < 0)
1167 goto error;
1168
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001169 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001170
1171 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1172 BTRFS_DEV_ITEM_KEY);
1173 if (ret) {
1174 *objectid = 1;
1175 } else {
1176 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1177 path->slots[0]);
1178 *objectid = found_key.offset + 1;
1179 }
1180 ret = 0;
1181error:
Yan Zheng2b820322008-11-17 21:11:30 -05001182 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001183 return ret;
1184}
1185
1186/*
1187 * the device information is stored in the chunk root
1188 * the btrfs_device struct should be fully filled in
1189 */
1190int btrfs_add_device(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root,
1192 struct btrfs_device *device)
1193{
1194 int ret;
1195 struct btrfs_path *path;
1196 struct btrfs_dev_item *dev_item;
1197 struct extent_buffer *leaf;
1198 struct btrfs_key key;
1199 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001200
1201 root = root->fs_info->chunk_root;
1202
1203 path = btrfs_alloc_path();
1204 if (!path)
1205 return -ENOMEM;
1206
Chris Mason0b86a832008-03-24 15:01:56 -04001207 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1208 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001209 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001210
1211 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001212 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001213 if (ret)
1214 goto out;
1215
1216 leaf = path->nodes[0];
1217 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1218
1219 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001220 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001221 btrfs_set_device_type(leaf, dev_item, device->type);
1222 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1223 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1224 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001225 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1226 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001227 btrfs_set_device_group(leaf, dev_item, 0);
1228 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1229 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001230 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001231
Chris Mason0b86a832008-03-24 15:01:56 -04001232 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001233 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001234 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1235 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001236 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001237
Yan Zheng2b820322008-11-17 21:11:30 -05001238 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001239out:
1240 btrfs_free_path(path);
1241 return ret;
1242}
Chris Mason8f18cf12008-04-25 16:53:30 -04001243
Chris Masona061fc82008-05-07 11:43:44 -04001244static int btrfs_rm_dev_item(struct btrfs_root *root,
1245 struct btrfs_device *device)
1246{
1247 int ret;
1248 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001249 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001250 struct btrfs_trans_handle *trans;
1251
1252 root = root->fs_info->chunk_root;
1253
1254 path = btrfs_alloc_path();
1255 if (!path)
1256 return -ENOMEM;
1257
Yan, Zhenga22285a2010-05-16 10:48:46 -04001258 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001259 if (IS_ERR(trans)) {
1260 btrfs_free_path(path);
1261 return PTR_ERR(trans);
1262 }
Chris Masona061fc82008-05-07 11:43:44 -04001263 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1264 key.type = BTRFS_DEV_ITEM_KEY;
1265 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001266 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001267
1268 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1269 if (ret < 0)
1270 goto out;
1271
1272 if (ret > 0) {
1273 ret = -ENOENT;
1274 goto out;
1275 }
1276
1277 ret = btrfs_del_item(trans, root, path);
1278 if (ret)
1279 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001280out:
1281 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001282 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001283 btrfs_commit_transaction(trans, root);
1284 return ret;
1285}
1286
1287int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1288{
1289 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001290 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001291 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001292 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001293 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001294 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001295 u64 all_avail;
1296 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001297 u64 num_devices;
1298 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001299 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001300 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001301
Chris Masona061fc82008-05-07 11:43:44 -04001302 mutex_lock(&uuid_mutex);
1303
1304 all_avail = root->fs_info->avail_data_alloc_bits |
1305 root->fs_info->avail_system_alloc_bits |
1306 root->fs_info->avail_metadata_alloc_bits;
1307
1308 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001309 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001310 printk(KERN_ERR "btrfs: unable to go below four devices "
1311 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001312 ret = -EINVAL;
1313 goto out;
1314 }
1315
1316 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001317 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001318 printk(KERN_ERR "btrfs: unable to go below two "
1319 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001320 ret = -EINVAL;
1321 goto out;
1322 }
1323
Chris Masondfe25022008-05-13 13:46:40 -04001324 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001325 struct list_head *devices;
1326 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001327
Chris Masondfe25022008-05-13 13:46:40 -04001328 device = NULL;
1329 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001330 /*
1331 * It is safe to read the devices since the volume_mutex
1332 * is held.
1333 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001334 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001335 if (tmp->in_fs_metadata && !tmp->bdev) {
1336 device = tmp;
1337 break;
1338 }
1339 }
1340 bdev = NULL;
1341 bh = NULL;
1342 disk_super = NULL;
1343 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001344 printk(KERN_ERR "btrfs: no missing devices found to "
1345 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001346 goto out;
1347 }
Chris Masondfe25022008-05-13 13:46:40 -04001348 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001349 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1350 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001351 if (IS_ERR(bdev)) {
1352 ret = PTR_ERR(bdev);
1353 goto out;
1354 }
1355
Yan Zheng2b820322008-11-17 21:11:30 -05001356 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001357 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001358 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001359 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001360 goto error_close;
1361 }
1362 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001363 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001364 dev_uuid = disk_super->dev_item.uuid;
1365 device = btrfs_find_device(root, devid, dev_uuid,
1366 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001367 if (!device) {
1368 ret = -ENOENT;
1369 goto error_brelse;
1370 }
Chris Masondfe25022008-05-13 13:46:40 -04001371 }
Yan Zheng2b820322008-11-17 21:11:30 -05001372
1373 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001374 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1375 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001376 ret = -EINVAL;
1377 goto error_brelse;
1378 }
1379
1380 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001381 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001382 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001383 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001384 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001385 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001386 }
Chris Masona061fc82008-05-07 11:43:44 -04001387
1388 ret = btrfs_shrink_device(device, 0);
1389 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001390 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001391
Chris Masona061fc82008-05-07 11:43:44 -04001392 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1393 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001394 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001395
Josef Bacik2bf64752011-09-26 17:12:22 -04001396 spin_lock(&root->fs_info->free_chunk_lock);
1397 root->fs_info->free_chunk_space = device->total_bytes -
1398 device->bytes_used;
1399 spin_unlock(&root->fs_info->free_chunk_lock);
1400
Yan Zheng2b820322008-11-17 21:11:30 -05001401 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001402 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001403
1404 /*
1405 * the device list mutex makes sure that we don't change
1406 * the device list while someone else is writing out all
1407 * the device supers.
1408 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001409
1410 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001411 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001412 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001413
Yan Zhenge4404d62008-12-12 10:03:26 -05001414 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001415
Chris Masoncd02dca2010-12-13 14:56:23 -05001416 if (device->missing)
1417 root->fs_info->fs_devices->missing_devices--;
1418
Yan Zheng2b820322008-11-17 21:11:30 -05001419 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1420 struct btrfs_device, dev_list);
1421 if (device->bdev == root->fs_info->sb->s_bdev)
1422 root->fs_info->sb->s_bdev = next_device->bdev;
1423 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1424 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1425
Xiao Guangrong1f781602011-04-20 10:09:16 +00001426 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001427 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001428
1429 call_rcu(&device->rcu, free_device);
1430 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001431
David Sterba6c417612011-04-13 15:41:04 +02001432 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1433 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001434
Xiao Guangrong1f781602011-04-20 10:09:16 +00001435 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001436 struct btrfs_fs_devices *fs_devices;
1437 fs_devices = root->fs_info->fs_devices;
1438 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001439 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001440 break;
1441 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001442 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001443 fs_devices->seed = cur_devices->seed;
1444 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001445 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001446 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001447 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001448 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001449 }
1450
1451 /*
1452 * at this point, the device is zero sized. We want to
1453 * remove it from the devices list and zero out the old super
1454 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001455 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001456 /* make sure this device isn't detected as part of
1457 * the FS anymore
1458 */
1459 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1460 set_buffer_dirty(bh);
1461 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001462 }
Chris Masona061fc82008-05-07 11:43:44 -04001463
Chris Masona061fc82008-05-07 11:43:44 -04001464 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001465
1466error_brelse:
1467 brelse(bh);
1468error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001469 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001470 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001471out:
1472 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001473 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001474error_undo:
1475 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001476 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001477 list_add(&device->dev_alloc_list,
1478 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001479 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001480 root->fs_info->fs_devices->rw_devices++;
1481 }
1482 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001483}
1484
Yan Zheng2b820322008-11-17 21:11:30 -05001485/*
1486 * does all the dirty work required for changing file system's UUID.
1487 */
Li Zefan125ccb02011-12-08 15:07:24 +08001488static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001489{
1490 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1491 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001492 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001493 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001494 struct btrfs_device *device;
1495 u64 super_flags;
1496
1497 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001498 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001499 return -EINVAL;
1500
Yan Zhenge4404d62008-12-12 10:03:26 -05001501 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1502 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001503 return -ENOMEM;
1504
Yan Zhenge4404d62008-12-12 10:03:26 -05001505 old_devices = clone_fs_devices(fs_devices);
1506 if (IS_ERR(old_devices)) {
1507 kfree(seed_devices);
1508 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001509 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001510
Yan Zheng2b820322008-11-17 21:11:30 -05001511 list_add(&old_devices->list, &fs_uuids);
1512
Yan Zhenge4404d62008-12-12 10:03:26 -05001513 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1514 seed_devices->opened = 1;
1515 INIT_LIST_HEAD(&seed_devices->devices);
1516 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001517 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001518
1519 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001520 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1521 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001522 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1523
Yan Zhenge4404d62008-12-12 10:03:26 -05001524 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1525 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1526 device->fs_devices = seed_devices;
1527 }
1528
Yan Zheng2b820322008-11-17 21:11:30 -05001529 fs_devices->seeding = 0;
1530 fs_devices->num_devices = 0;
1531 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001532 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001533
1534 generate_random_uuid(fs_devices->fsid);
1535 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1536 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1537 super_flags = btrfs_super_flags(disk_super) &
1538 ~BTRFS_SUPER_FLAG_SEEDING;
1539 btrfs_set_super_flags(disk_super, super_flags);
1540
1541 return 0;
1542}
1543
1544/*
1545 * strore the expected generation for seed devices in device items.
1546 */
1547static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1548 struct btrfs_root *root)
1549{
1550 struct btrfs_path *path;
1551 struct extent_buffer *leaf;
1552 struct btrfs_dev_item *dev_item;
1553 struct btrfs_device *device;
1554 struct btrfs_key key;
1555 u8 fs_uuid[BTRFS_UUID_SIZE];
1556 u8 dev_uuid[BTRFS_UUID_SIZE];
1557 u64 devid;
1558 int ret;
1559
1560 path = btrfs_alloc_path();
1561 if (!path)
1562 return -ENOMEM;
1563
1564 root = root->fs_info->chunk_root;
1565 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1566 key.offset = 0;
1567 key.type = BTRFS_DEV_ITEM_KEY;
1568
1569 while (1) {
1570 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1571 if (ret < 0)
1572 goto error;
1573
1574 leaf = path->nodes[0];
1575next_slot:
1576 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1577 ret = btrfs_next_leaf(root, path);
1578 if (ret > 0)
1579 break;
1580 if (ret < 0)
1581 goto error;
1582 leaf = path->nodes[0];
1583 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001584 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001585 continue;
1586 }
1587
1588 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1589 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1590 key.type != BTRFS_DEV_ITEM_KEY)
1591 break;
1592
1593 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1594 struct btrfs_dev_item);
1595 devid = btrfs_device_id(leaf, dev_item);
1596 read_extent_buffer(leaf, dev_uuid,
1597 (unsigned long)btrfs_device_uuid(dev_item),
1598 BTRFS_UUID_SIZE);
1599 read_extent_buffer(leaf, fs_uuid,
1600 (unsigned long)btrfs_device_fsid(dev_item),
1601 BTRFS_UUID_SIZE);
1602 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001603 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001604
1605 if (device->fs_devices->seeding) {
1606 btrfs_set_device_generation(leaf, dev_item,
1607 device->generation);
1608 btrfs_mark_buffer_dirty(leaf);
1609 }
1610
1611 path->slots[0]++;
1612 goto next_slot;
1613 }
1614 ret = 0;
1615error:
1616 btrfs_free_path(path);
1617 return ret;
1618}
1619
Chris Mason788f20e2008-04-28 15:29:42 -04001620int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1621{
Josef Bacikd5e20032011-08-04 14:52:27 +00001622 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001623 struct btrfs_trans_handle *trans;
1624 struct btrfs_device *device;
1625 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001626 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001627 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001628 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001629 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001630 int ret = 0;
1631
Yan Zheng2b820322008-11-17 21:11:30 -05001632 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1633 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001634
Li Zefana5d16332011-12-07 20:08:40 -05001635 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001636 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001637 if (IS_ERR(bdev))
1638 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001639
Yan Zheng2b820322008-11-17 21:11:30 -05001640 if (root->fs_info->fs_devices->seeding) {
1641 seeding_dev = 1;
1642 down_write(&sb->s_umount);
1643 mutex_lock(&uuid_mutex);
1644 }
1645
Chris Mason8c8bee12008-09-29 11:19:10 -04001646 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001647
Chris Mason788f20e2008-04-28 15:29:42 -04001648 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001649 /*
1650 * we have the volume lock, so we don't need the extra
1651 * device list mutex while reading the list here.
1652 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001653 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001654 if (device->bdev == bdev) {
1655 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001656 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001657 }
1658 }
1659
1660 device = kzalloc(sizeof(*device), GFP_NOFS);
1661 if (!device) {
1662 /* we can safely leave the fs_devices entry around */
1663 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001664 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001665 }
1666
Chris Mason788f20e2008-04-28 15:29:42 -04001667 device->name = kstrdup(device_path, GFP_NOFS);
1668 if (!device->name) {
1669 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001670 ret = -ENOMEM;
1671 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001672 }
Yan Zheng2b820322008-11-17 21:11:30 -05001673
1674 ret = find_next_devid(root, &device->devid);
1675 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001676 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001677 kfree(device);
1678 goto error;
1679 }
1680
Yan, Zhenga22285a2010-05-16 10:48:46 -04001681 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001682 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001683 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001684 kfree(device);
1685 ret = PTR_ERR(trans);
1686 goto error;
1687 }
1688
Yan Zheng2b820322008-11-17 21:11:30 -05001689 lock_chunks(root);
1690
Josef Bacikd5e20032011-08-04 14:52:27 +00001691 q = bdev_get_queue(bdev);
1692 if (blk_queue_discard(q))
1693 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001694 device->writeable = 1;
1695 device->work.func = pending_bios_fn;
1696 generate_random_uuid(device->uuid);
1697 spin_lock_init(&device->io_lock);
1698 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001699 device->io_width = root->sectorsize;
1700 device->io_align = root->sectorsize;
1701 device->sector_size = root->sectorsize;
1702 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001703 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001704 device->dev_root = root->fs_info->dev_root;
1705 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001706 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001707 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001708 set_blocksize(device->bdev, 4096);
1709
Yan Zheng2b820322008-11-17 21:11:30 -05001710 if (seeding_dev) {
1711 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001712 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001713 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001714 }
1715
1716 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001717
1718 /*
1719 * we don't want write_supers to jump in here with our device
1720 * half setup
1721 */
1722 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001723 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001724 list_add(&device->dev_alloc_list,
1725 &root->fs_info->fs_devices->alloc_list);
1726 root->fs_info->fs_devices->num_devices++;
1727 root->fs_info->fs_devices->open_devices++;
1728 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001729 if (device->can_discard)
1730 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001731 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1732
Josef Bacik2bf64752011-09-26 17:12:22 -04001733 spin_lock(&root->fs_info->free_chunk_lock);
1734 root->fs_info->free_chunk_space += device->total_bytes;
1735 spin_unlock(&root->fs_info->free_chunk_lock);
1736
Chris Masonc289811c2009-06-10 09:51:32 -04001737 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1738 root->fs_info->fs_devices->rotating = 1;
1739
David Sterba6c417612011-04-13 15:41:04 +02001740 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1741 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001742 total_bytes + device->total_bytes);
1743
David Sterba6c417612011-04-13 15:41:04 +02001744 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1745 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001746 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001747 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001748
Yan Zheng2b820322008-11-17 21:11:30 -05001749 if (seeding_dev) {
1750 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001751 if (ret)
1752 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001753 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001754 if (ret)
1755 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001756 } else {
1757 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001758 if (ret)
1759 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001760 }
1761
Chris Mason913d9522009-03-10 13:17:18 -04001762 /*
1763 * we've got more storage, clear any full flags on the space
1764 * infos
1765 */
1766 btrfs_clear_space_info_full(root->fs_info);
1767
Chris Mason7d9eb122008-07-08 14:19:17 -04001768 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001769 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001770
1771 if (seeding_dev) {
1772 mutex_unlock(&uuid_mutex);
1773 up_write(&sb->s_umount);
1774
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001775 if (ret) /* transaction commit */
1776 return ret;
1777
Yan Zheng2b820322008-11-17 21:11:30 -05001778 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001779 if (ret < 0)
1780 btrfs_error(root->fs_info, ret,
1781 "Failed to relocate sys chunks after "
1782 "device initialization. This can be fixed "
1783 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001784 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001785
Chris Mason788f20e2008-04-28 15:29:42 -04001786 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001787
1788error_trans:
1789 unlock_chunks(root);
1790 btrfs_abort_transaction(trans, root, ret);
1791 btrfs_end_transaction(trans, root);
1792 kfree(device->name);
1793 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001794error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001795 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001796 if (seeding_dev) {
1797 mutex_unlock(&uuid_mutex);
1798 up_write(&sb->s_umount);
1799 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001800 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001801}
1802
Chris Masond3977122009-01-05 21:25:51 -05001803static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1804 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001805{
1806 int ret;
1807 struct btrfs_path *path;
1808 struct btrfs_root *root;
1809 struct btrfs_dev_item *dev_item;
1810 struct extent_buffer *leaf;
1811 struct btrfs_key key;
1812
1813 root = device->dev_root->fs_info->chunk_root;
1814
1815 path = btrfs_alloc_path();
1816 if (!path)
1817 return -ENOMEM;
1818
1819 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1820 key.type = BTRFS_DEV_ITEM_KEY;
1821 key.offset = device->devid;
1822
1823 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1824 if (ret < 0)
1825 goto out;
1826
1827 if (ret > 0) {
1828 ret = -ENOENT;
1829 goto out;
1830 }
1831
1832 leaf = path->nodes[0];
1833 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1834
1835 btrfs_set_device_id(leaf, dev_item, device->devid);
1836 btrfs_set_device_type(leaf, dev_item, device->type);
1837 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1838 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1839 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001840 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001841 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1842 btrfs_mark_buffer_dirty(leaf);
1843
1844out:
1845 btrfs_free_path(path);
1846 return ret;
1847}
1848
Chris Mason7d9eb122008-07-08 14:19:17 -04001849static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001850 struct btrfs_device *device, u64 new_size)
1851{
1852 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001853 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001854 u64 old_total = btrfs_super_total_bytes(super_copy);
1855 u64 diff = new_size - device->total_bytes;
1856
Yan Zheng2b820322008-11-17 21:11:30 -05001857 if (!device->writeable)
1858 return -EACCES;
1859 if (new_size <= device->total_bytes)
1860 return -EINVAL;
1861
Chris Mason8f18cf12008-04-25 16:53:30 -04001862 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001863 device->fs_devices->total_rw_bytes += diff;
1864
1865 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001866 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001867 btrfs_clear_space_info_full(device->dev_root->fs_info);
1868
Chris Mason8f18cf12008-04-25 16:53:30 -04001869 return btrfs_update_device(trans, device);
1870}
1871
Chris Mason7d9eb122008-07-08 14:19:17 -04001872int btrfs_grow_device(struct btrfs_trans_handle *trans,
1873 struct btrfs_device *device, u64 new_size)
1874{
1875 int ret;
1876 lock_chunks(device->dev_root);
1877 ret = __btrfs_grow_device(trans, device, new_size);
1878 unlock_chunks(device->dev_root);
1879 return ret;
1880}
1881
Chris Mason8f18cf12008-04-25 16:53:30 -04001882static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1883 struct btrfs_root *root,
1884 u64 chunk_tree, u64 chunk_objectid,
1885 u64 chunk_offset)
1886{
1887 int ret;
1888 struct btrfs_path *path;
1889 struct btrfs_key key;
1890
1891 root = root->fs_info->chunk_root;
1892 path = btrfs_alloc_path();
1893 if (!path)
1894 return -ENOMEM;
1895
1896 key.objectid = chunk_objectid;
1897 key.offset = chunk_offset;
1898 key.type = BTRFS_CHUNK_ITEM_KEY;
1899
1900 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001901 if (ret < 0)
1902 goto out;
1903 else if (ret > 0) { /* Logic error or corruption */
1904 btrfs_error(root->fs_info, -ENOENT,
1905 "Failed lookup while freeing chunk.");
1906 ret = -ENOENT;
1907 goto out;
1908 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001909
1910 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001911 if (ret < 0)
1912 btrfs_error(root->fs_info, ret,
1913 "Failed to delete chunk item.");
1914out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001915 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001916 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001917}
1918
Christoph Hellwigb2950862008-12-02 09:54:17 -05001919static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001920 chunk_offset)
1921{
David Sterba6c417612011-04-13 15:41:04 +02001922 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001923 struct btrfs_disk_key *disk_key;
1924 struct btrfs_chunk *chunk;
1925 u8 *ptr;
1926 int ret = 0;
1927 u32 num_stripes;
1928 u32 array_size;
1929 u32 len = 0;
1930 u32 cur;
1931 struct btrfs_key key;
1932
1933 array_size = btrfs_super_sys_array_size(super_copy);
1934
1935 ptr = super_copy->sys_chunk_array;
1936 cur = 0;
1937
1938 while (cur < array_size) {
1939 disk_key = (struct btrfs_disk_key *)ptr;
1940 btrfs_disk_key_to_cpu(&key, disk_key);
1941
1942 len = sizeof(*disk_key);
1943
1944 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1945 chunk = (struct btrfs_chunk *)(ptr + len);
1946 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1947 len += btrfs_chunk_item_size(num_stripes);
1948 } else {
1949 ret = -EIO;
1950 break;
1951 }
1952 if (key.objectid == chunk_objectid &&
1953 key.offset == chunk_offset) {
1954 memmove(ptr, ptr + len, array_size - (cur + len));
1955 array_size -= len;
1956 btrfs_set_super_sys_array_size(super_copy, array_size);
1957 } else {
1958 ptr += len;
1959 cur += len;
1960 }
1961 }
1962 return ret;
1963}
1964
Christoph Hellwigb2950862008-12-02 09:54:17 -05001965static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001966 u64 chunk_tree, u64 chunk_objectid,
1967 u64 chunk_offset)
1968{
1969 struct extent_map_tree *em_tree;
1970 struct btrfs_root *extent_root;
1971 struct btrfs_trans_handle *trans;
1972 struct extent_map *em;
1973 struct map_lookup *map;
1974 int ret;
1975 int i;
1976
1977 root = root->fs_info->chunk_root;
1978 extent_root = root->fs_info->extent_root;
1979 em_tree = &root->fs_info->mapping_tree.map_tree;
1980
Josef Bacikba1bf482009-09-11 16:11:19 -04001981 ret = btrfs_can_relocate(extent_root, chunk_offset);
1982 if (ret)
1983 return -ENOSPC;
1984
Chris Mason8f18cf12008-04-25 16:53:30 -04001985 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001986 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001987 if (ret)
1988 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001989
Yan, Zhenga22285a2010-05-16 10:48:46 -04001990 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001991 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001992
Chris Mason7d9eb122008-07-08 14:19:17 -04001993 lock_chunks(root);
1994
Chris Mason8f18cf12008-04-25 16:53:30 -04001995 /*
1996 * step two, delete the device extents and the
1997 * chunk tree entries
1998 */
Chris Mason890871b2009-09-02 16:24:52 -04001999 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002000 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002001 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002002
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002003 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002004 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002005 map = (struct map_lookup *)em->bdev;
2006
2007 for (i = 0; i < map->num_stripes; i++) {
2008 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2009 map->stripes[i].physical);
2010 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002011
Chris Masondfe25022008-05-13 13:46:40 -04002012 if (map->stripes[i].dev) {
2013 ret = btrfs_update_device(trans, map->stripes[i].dev);
2014 BUG_ON(ret);
2015 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002016 }
2017 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2018 chunk_offset);
2019
2020 BUG_ON(ret);
2021
liubo1abe9b82011-03-24 11:18:59 +00002022 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2023
Chris Mason8f18cf12008-04-25 16:53:30 -04002024 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2025 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2026 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002027 }
2028
Zheng Yan1a40e232008-09-26 10:09:34 -04002029 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2030 BUG_ON(ret);
2031
Chris Mason890871b2009-09-02 16:24:52 -04002032 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002033 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002034 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002035
Chris Mason8f18cf12008-04-25 16:53:30 -04002036 kfree(map);
2037 em->bdev = NULL;
2038
2039 /* once for the tree */
2040 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002041 /* once for us */
2042 free_extent_map(em);
2043
Chris Mason7d9eb122008-07-08 14:19:17 -04002044 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002045 btrfs_end_transaction(trans, root);
2046 return 0;
2047}
2048
Yan Zheng2b820322008-11-17 21:11:30 -05002049static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2050{
2051 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2052 struct btrfs_path *path;
2053 struct extent_buffer *leaf;
2054 struct btrfs_chunk *chunk;
2055 struct btrfs_key key;
2056 struct btrfs_key found_key;
2057 u64 chunk_tree = chunk_root->root_key.objectid;
2058 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002059 bool retried = false;
2060 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002061 int ret;
2062
2063 path = btrfs_alloc_path();
2064 if (!path)
2065 return -ENOMEM;
2066
Josef Bacikba1bf482009-09-11 16:11:19 -04002067again:
Yan Zheng2b820322008-11-17 21:11:30 -05002068 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2069 key.offset = (u64)-1;
2070 key.type = BTRFS_CHUNK_ITEM_KEY;
2071
2072 while (1) {
2073 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2074 if (ret < 0)
2075 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002076 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002077
2078 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2079 key.type);
2080 if (ret < 0)
2081 goto error;
2082 if (ret > 0)
2083 break;
2084
2085 leaf = path->nodes[0];
2086 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2087
2088 chunk = btrfs_item_ptr(leaf, path->slots[0],
2089 struct btrfs_chunk);
2090 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002091 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002092
2093 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2094 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2095 found_key.objectid,
2096 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002097 if (ret == -ENOSPC)
2098 failed++;
2099 else if (ret)
2100 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002101 }
2102
2103 if (found_key.offset == 0)
2104 break;
2105 key.offset = found_key.offset - 1;
2106 }
2107 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002108 if (failed && !retried) {
2109 failed = 0;
2110 retried = true;
2111 goto again;
2112 } else if (failed && retried) {
2113 WARN_ON(1);
2114 ret = -ENOSPC;
2115 }
Yan Zheng2b820322008-11-17 21:11:30 -05002116error:
2117 btrfs_free_path(path);
2118 return ret;
2119}
2120
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002121static int insert_balance_item(struct btrfs_root *root,
2122 struct btrfs_balance_control *bctl)
2123{
2124 struct btrfs_trans_handle *trans;
2125 struct btrfs_balance_item *item;
2126 struct btrfs_disk_balance_args disk_bargs;
2127 struct btrfs_path *path;
2128 struct extent_buffer *leaf;
2129 struct btrfs_key key;
2130 int ret, err;
2131
2132 path = btrfs_alloc_path();
2133 if (!path)
2134 return -ENOMEM;
2135
2136 trans = btrfs_start_transaction(root, 0);
2137 if (IS_ERR(trans)) {
2138 btrfs_free_path(path);
2139 return PTR_ERR(trans);
2140 }
2141
2142 key.objectid = BTRFS_BALANCE_OBJECTID;
2143 key.type = BTRFS_BALANCE_ITEM_KEY;
2144 key.offset = 0;
2145
2146 ret = btrfs_insert_empty_item(trans, root, path, &key,
2147 sizeof(*item));
2148 if (ret)
2149 goto out;
2150
2151 leaf = path->nodes[0];
2152 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2153
2154 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2155
2156 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2157 btrfs_set_balance_data(leaf, item, &disk_bargs);
2158 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2159 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2160 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2161 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2162
2163 btrfs_set_balance_flags(leaf, item, bctl->flags);
2164
2165 btrfs_mark_buffer_dirty(leaf);
2166out:
2167 btrfs_free_path(path);
2168 err = btrfs_commit_transaction(trans, root);
2169 if (err && !ret)
2170 ret = err;
2171 return ret;
2172}
2173
2174static int del_balance_item(struct btrfs_root *root)
2175{
2176 struct btrfs_trans_handle *trans;
2177 struct btrfs_path *path;
2178 struct btrfs_key key;
2179 int ret, err;
2180
2181 path = btrfs_alloc_path();
2182 if (!path)
2183 return -ENOMEM;
2184
2185 trans = btrfs_start_transaction(root, 0);
2186 if (IS_ERR(trans)) {
2187 btrfs_free_path(path);
2188 return PTR_ERR(trans);
2189 }
2190
2191 key.objectid = BTRFS_BALANCE_OBJECTID;
2192 key.type = BTRFS_BALANCE_ITEM_KEY;
2193 key.offset = 0;
2194
2195 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2196 if (ret < 0)
2197 goto out;
2198 if (ret > 0) {
2199 ret = -ENOENT;
2200 goto out;
2201 }
2202
2203 ret = btrfs_del_item(trans, root, path);
2204out:
2205 btrfs_free_path(path);
2206 err = btrfs_commit_transaction(trans, root);
2207 if (err && !ret)
2208 ret = err;
2209 return ret;
2210}
2211
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002212/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002213 * This is a heuristic used to reduce the number of chunks balanced on
2214 * resume after balance was interrupted.
2215 */
2216static void update_balance_args(struct btrfs_balance_control *bctl)
2217{
2218 /*
2219 * Turn on soft mode for chunk types that were being converted.
2220 */
2221 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2222 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2223 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2224 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2225 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2226 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2227
2228 /*
2229 * Turn on usage filter if is not already used. The idea is
2230 * that chunks that we have already balanced should be
2231 * reasonably full. Don't do it for chunks that are being
2232 * converted - that will keep us from relocating unconverted
2233 * (albeit full) chunks.
2234 */
2235 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2236 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2237 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2238 bctl->data.usage = 90;
2239 }
2240 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2241 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2242 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2243 bctl->sys.usage = 90;
2244 }
2245 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2246 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2247 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2248 bctl->meta.usage = 90;
2249 }
2250}
2251
2252/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002253 * Should be called with both balance and volume mutexes held to
2254 * serialize other volume operations (add_dev/rm_dev/resize) with
2255 * restriper. Same goes for unset_balance_control.
2256 */
2257static void set_balance_control(struct btrfs_balance_control *bctl)
2258{
2259 struct btrfs_fs_info *fs_info = bctl->fs_info;
2260
2261 BUG_ON(fs_info->balance_ctl);
2262
2263 spin_lock(&fs_info->balance_lock);
2264 fs_info->balance_ctl = bctl;
2265 spin_unlock(&fs_info->balance_lock);
2266}
2267
2268static void unset_balance_control(struct btrfs_fs_info *fs_info)
2269{
2270 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2271
2272 BUG_ON(!fs_info->balance_ctl);
2273
2274 spin_lock(&fs_info->balance_lock);
2275 fs_info->balance_ctl = NULL;
2276 spin_unlock(&fs_info->balance_lock);
2277
2278 kfree(bctl);
2279}
2280
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002281/*
2282 * Balance filters. Return 1 if chunk should be filtered out
2283 * (should not be balanced).
2284 */
2285static int chunk_profiles_filter(u64 chunk_profile,
2286 struct btrfs_balance_args *bargs)
2287{
2288 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2289
2290 if (chunk_profile == 0)
2291 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2292
2293 if (bargs->profiles & chunk_profile)
2294 return 0;
2295
2296 return 1;
2297}
2298
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002299static u64 div_factor_fine(u64 num, int factor)
2300{
2301 if (factor <= 0)
2302 return 0;
2303 if (factor >= 100)
2304 return num;
2305
2306 num *= factor;
2307 do_div(num, 100);
2308 return num;
2309}
2310
2311static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2312 struct btrfs_balance_args *bargs)
2313{
2314 struct btrfs_block_group_cache *cache;
2315 u64 chunk_used, user_thresh;
2316 int ret = 1;
2317
2318 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2319 chunk_used = btrfs_block_group_used(&cache->item);
2320
2321 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2322 if (chunk_used < user_thresh)
2323 ret = 0;
2324
2325 btrfs_put_block_group(cache);
2326 return ret;
2327}
2328
Ilya Dryomov409d4042012-01-16 22:04:47 +02002329static int chunk_devid_filter(struct extent_buffer *leaf,
2330 struct btrfs_chunk *chunk,
2331 struct btrfs_balance_args *bargs)
2332{
2333 struct btrfs_stripe *stripe;
2334 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2335 int i;
2336
2337 for (i = 0; i < num_stripes; i++) {
2338 stripe = btrfs_stripe_nr(chunk, i);
2339 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2340 return 0;
2341 }
2342
2343 return 1;
2344}
2345
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002346/* [pstart, pend) */
2347static int chunk_drange_filter(struct extent_buffer *leaf,
2348 struct btrfs_chunk *chunk,
2349 u64 chunk_offset,
2350 struct btrfs_balance_args *bargs)
2351{
2352 struct btrfs_stripe *stripe;
2353 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2354 u64 stripe_offset;
2355 u64 stripe_length;
2356 int factor;
2357 int i;
2358
2359 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2360 return 0;
2361
2362 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2363 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2364 factor = 2;
2365 else
2366 factor = 1;
2367 factor = num_stripes / factor;
2368
2369 for (i = 0; i < num_stripes; i++) {
2370 stripe = btrfs_stripe_nr(chunk, i);
2371 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2372 continue;
2373
2374 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2375 stripe_length = btrfs_chunk_length(leaf, chunk);
2376 do_div(stripe_length, factor);
2377
2378 if (stripe_offset < bargs->pend &&
2379 stripe_offset + stripe_length > bargs->pstart)
2380 return 0;
2381 }
2382
2383 return 1;
2384}
2385
Ilya Dryomovea671762012-01-16 22:04:48 +02002386/* [vstart, vend) */
2387static int chunk_vrange_filter(struct extent_buffer *leaf,
2388 struct btrfs_chunk *chunk,
2389 u64 chunk_offset,
2390 struct btrfs_balance_args *bargs)
2391{
2392 if (chunk_offset < bargs->vend &&
2393 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2394 /* at least part of the chunk is inside this vrange */
2395 return 0;
2396
2397 return 1;
2398}
2399
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002400static int chunk_soft_convert_filter(u64 chunk_profile,
2401 struct btrfs_balance_args *bargs)
2402{
2403 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2404 return 0;
2405
2406 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2407
2408 if (chunk_profile == 0)
2409 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2410
2411 if (bargs->target & chunk_profile)
2412 return 1;
2413
2414 return 0;
2415}
2416
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002417static int should_balance_chunk(struct btrfs_root *root,
2418 struct extent_buffer *leaf,
2419 struct btrfs_chunk *chunk, u64 chunk_offset)
2420{
2421 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2422 struct btrfs_balance_args *bargs = NULL;
2423 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2424
2425 /* type filter */
2426 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2427 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2428 return 0;
2429 }
2430
2431 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2432 bargs = &bctl->data;
2433 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2434 bargs = &bctl->sys;
2435 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2436 bargs = &bctl->meta;
2437
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002438 /* profiles filter */
2439 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2440 chunk_profiles_filter(chunk_type, bargs)) {
2441 return 0;
2442 }
2443
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002444 /* usage filter */
2445 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2446 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2447 return 0;
2448 }
2449
Ilya Dryomov409d4042012-01-16 22:04:47 +02002450 /* devid filter */
2451 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2452 chunk_devid_filter(leaf, chunk, bargs)) {
2453 return 0;
2454 }
2455
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002456 /* drange filter, makes sense only with devid filter */
2457 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2458 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2459 return 0;
2460 }
2461
Ilya Dryomovea671762012-01-16 22:04:48 +02002462 /* vrange filter */
2463 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2464 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2465 return 0;
2466 }
2467
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002468 /* soft profile changing mode */
2469 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2470 chunk_soft_convert_filter(chunk_type, bargs)) {
2471 return 0;
2472 }
2473
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002474 return 1;
2475}
2476
Chris Masonec44a352008-04-28 15:29:52 -04002477static u64 div_factor(u64 num, int factor)
2478{
2479 if (factor == 10)
2480 return num;
2481 num *= factor;
2482 do_div(num, 10);
2483 return num;
2484}
2485
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002486static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002487{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002488 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002489 struct btrfs_root *chunk_root = fs_info->chunk_root;
2490 struct btrfs_root *dev_root = fs_info->dev_root;
2491 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002492 struct btrfs_device *device;
2493 u64 old_size;
2494 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002495 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002496 struct btrfs_path *path;
2497 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002498 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002499 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002500 struct extent_buffer *leaf;
2501 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002502 int ret;
2503 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002504 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002505
Chris Masonec44a352008-04-28 15:29:52 -04002506 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002507 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002508 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002509 old_size = device->total_bytes;
2510 size_to_free = div_factor(old_size, 1);
2511 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002512 if (!device->writeable ||
2513 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002514 continue;
2515
2516 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002517 if (ret == -ENOSPC)
2518 break;
Chris Masonec44a352008-04-28 15:29:52 -04002519 BUG_ON(ret);
2520
Yan, Zhenga22285a2010-05-16 10:48:46 -04002521 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002522 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002523
2524 ret = btrfs_grow_device(trans, device, old_size);
2525 BUG_ON(ret);
2526
2527 btrfs_end_transaction(trans, dev_root);
2528 }
2529
2530 /* step two, relocate all the chunks */
2531 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002532 if (!path) {
2533 ret = -ENOMEM;
2534 goto error;
2535 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002536
2537 /* zero out stat counters */
2538 spin_lock(&fs_info->balance_lock);
2539 memset(&bctl->stat, 0, sizeof(bctl->stat));
2540 spin_unlock(&fs_info->balance_lock);
2541again:
Chris Masonec44a352008-04-28 15:29:52 -04002542 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2543 key.offset = (u64)-1;
2544 key.type = BTRFS_CHUNK_ITEM_KEY;
2545
Chris Masond3977122009-01-05 21:25:51 -05002546 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002547 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002548 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002549 ret = -ECANCELED;
2550 goto error;
2551 }
2552
Chris Masonec44a352008-04-28 15:29:52 -04002553 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2554 if (ret < 0)
2555 goto error;
2556
2557 /*
2558 * this shouldn't happen, it means the last relocate
2559 * failed
2560 */
2561 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002562 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002563
2564 ret = btrfs_previous_item(chunk_root, path, 0,
2565 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002566 if (ret) {
2567 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002568 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002569 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002570
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002571 leaf = path->nodes[0];
2572 slot = path->slots[0];
2573 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2574
Chris Masonec44a352008-04-28 15:29:52 -04002575 if (found_key.objectid != key.objectid)
2576 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002577
Chris Masonec44a352008-04-28 15:29:52 -04002578 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002579 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002580 break;
2581
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002582 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2583
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002584 if (!counting) {
2585 spin_lock(&fs_info->balance_lock);
2586 bctl->stat.considered++;
2587 spin_unlock(&fs_info->balance_lock);
2588 }
2589
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002590 ret = should_balance_chunk(chunk_root, leaf, chunk,
2591 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002592 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002593 if (!ret)
2594 goto loop;
2595
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002596 if (counting) {
2597 spin_lock(&fs_info->balance_lock);
2598 bctl->stat.expected++;
2599 spin_unlock(&fs_info->balance_lock);
2600 goto loop;
2601 }
2602
Chris Masonec44a352008-04-28 15:29:52 -04002603 ret = btrfs_relocate_chunk(chunk_root,
2604 chunk_root->root_key.objectid,
2605 found_key.objectid,
2606 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002607 if (ret && ret != -ENOSPC)
2608 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002609 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002610 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002611 } else {
2612 spin_lock(&fs_info->balance_lock);
2613 bctl->stat.completed++;
2614 spin_unlock(&fs_info->balance_lock);
2615 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002616loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002617 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002618 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002619
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002620 if (counting) {
2621 btrfs_release_path(path);
2622 counting = false;
2623 goto again;
2624 }
Chris Masonec44a352008-04-28 15:29:52 -04002625error:
2626 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002627 if (enospc_errors) {
2628 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2629 enospc_errors);
2630 if (!ret)
2631 ret = -ENOSPC;
2632 }
2633
Chris Masonec44a352008-04-28 15:29:52 -04002634 return ret;
2635}
2636
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002637static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2638{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002639 /* cancel requested || normal exit path */
2640 return atomic_read(&fs_info->balance_cancel_req) ||
2641 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2642 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002643}
2644
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002645static void __cancel_balance(struct btrfs_fs_info *fs_info)
2646{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002647 int ret;
2648
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002649 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002650 ret = del_balance_item(fs_info->tree_root);
2651 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002652}
2653
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002654void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002655 struct btrfs_ioctl_balance_args *bargs);
2656
2657/*
2658 * Should be called with both balance and volume mutexes held
2659 */
2660int btrfs_balance(struct btrfs_balance_control *bctl,
2661 struct btrfs_ioctl_balance_args *bargs)
2662{
2663 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002664 u64 allowed;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002665 int ret;
2666
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002667 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002668 atomic_read(&fs_info->balance_pause_req) ||
2669 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002670 ret = -EINVAL;
2671 goto out;
2672 }
2673
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002674 /*
2675 * In case of mixed groups both data and meta should be picked,
2676 * and identical options should be given for both of them.
2677 */
2678 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2679 if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
2680 (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
2681 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2682 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2683 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2684 printk(KERN_ERR "btrfs: with mixed groups data and "
2685 "metadata balance options must be the same\n");
2686 ret = -EINVAL;
2687 goto out;
2688 }
2689 }
2690
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002691 /*
2692 * Profile changing sanity checks. Skip them if a simple
2693 * balance is requested.
2694 */
2695 if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
2696 BTRFS_BALANCE_ARGS_CONVERT))
2697 goto do_balance;
2698
2699 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2700 if (fs_info->fs_devices->num_devices == 1)
2701 allowed |= BTRFS_BLOCK_GROUP_DUP;
2702 else if (fs_info->fs_devices->num_devices < 4)
2703 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2704 else
2705 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2706 BTRFS_BLOCK_GROUP_RAID10);
2707
2708 if (!profile_is_valid(bctl->data.target, 1) ||
2709 bctl->data.target & ~allowed) {
2710 printk(KERN_ERR "btrfs: unable to start balance with target "
2711 "data profile %llu\n",
2712 (unsigned long long)bctl->data.target);
2713 ret = -EINVAL;
2714 goto out;
2715 }
2716 if (!profile_is_valid(bctl->meta.target, 1) ||
2717 bctl->meta.target & ~allowed) {
2718 printk(KERN_ERR "btrfs: unable to start balance with target "
2719 "metadata profile %llu\n",
2720 (unsigned long long)bctl->meta.target);
2721 ret = -EINVAL;
2722 goto out;
2723 }
2724 if (!profile_is_valid(bctl->sys.target, 1) ||
2725 bctl->sys.target & ~allowed) {
2726 printk(KERN_ERR "btrfs: unable to start balance with target "
2727 "system profile %llu\n",
2728 (unsigned long long)bctl->sys.target);
2729 ret = -EINVAL;
2730 goto out;
2731 }
2732
2733 if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
2734 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2735 ret = -EINVAL;
2736 goto out;
2737 }
2738
2739 /* allow to reduce meta or sys integrity only if force set */
2740 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2741 BTRFS_BLOCK_GROUP_RAID10;
2742 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2743 (fs_info->avail_system_alloc_bits & allowed) &&
2744 !(bctl->sys.target & allowed)) ||
2745 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2746 (fs_info->avail_metadata_alloc_bits & allowed) &&
2747 !(bctl->meta.target & allowed))) {
2748 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2749 printk(KERN_INFO "btrfs: force reducing metadata "
2750 "integrity\n");
2751 } else {
2752 printk(KERN_ERR "btrfs: balance will reduce metadata "
2753 "integrity, use force if you want this\n");
2754 ret = -EINVAL;
2755 goto out;
2756 }
2757 }
2758
2759do_balance:
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002760 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002761 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002762 goto out;
2763
Ilya Dryomov59641012012-01-16 22:04:48 +02002764 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2765 BUG_ON(ret == -EEXIST);
2766 set_balance_control(bctl);
2767 } else {
2768 BUG_ON(ret != -EEXIST);
2769 spin_lock(&fs_info->balance_lock);
2770 update_balance_args(bctl);
2771 spin_unlock(&fs_info->balance_lock);
2772 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002773
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002774 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002775 mutex_unlock(&fs_info->balance_mutex);
2776
2777 ret = __btrfs_balance(fs_info);
2778
2779 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002780 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002781
2782 if (bargs) {
2783 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002784 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002785 }
2786
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002787 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2788 balance_need_close(fs_info)) {
2789 __cancel_balance(fs_info);
2790 }
2791
2792 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002793
2794 return ret;
2795out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002796 if (bctl->flags & BTRFS_BALANCE_RESUME)
2797 __cancel_balance(fs_info);
2798 else
2799 kfree(bctl);
2800 return ret;
2801}
2802
2803static int balance_kthread(void *data)
2804{
2805 struct btrfs_balance_control *bctl =
2806 (struct btrfs_balance_control *)data;
2807 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002808 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002809
2810 mutex_lock(&fs_info->volume_mutex);
2811 mutex_lock(&fs_info->balance_mutex);
2812
2813 set_balance_control(bctl);
2814
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002815 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2816 printk(KERN_INFO "btrfs: force skipping balance\n");
2817 } else {
2818 printk(KERN_INFO "btrfs: continuing balance\n");
2819 ret = btrfs_balance(bctl, NULL);
2820 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002821
2822 mutex_unlock(&fs_info->balance_mutex);
2823 mutex_unlock(&fs_info->volume_mutex);
2824 return ret;
2825}
2826
2827int btrfs_recover_balance(struct btrfs_root *tree_root)
2828{
2829 struct task_struct *tsk;
2830 struct btrfs_balance_control *bctl;
2831 struct btrfs_balance_item *item;
2832 struct btrfs_disk_balance_args disk_bargs;
2833 struct btrfs_path *path;
2834 struct extent_buffer *leaf;
2835 struct btrfs_key key;
2836 int ret;
2837
2838 path = btrfs_alloc_path();
2839 if (!path)
2840 return -ENOMEM;
2841
2842 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2843 if (!bctl) {
2844 ret = -ENOMEM;
2845 goto out;
2846 }
2847
2848 key.objectid = BTRFS_BALANCE_OBJECTID;
2849 key.type = BTRFS_BALANCE_ITEM_KEY;
2850 key.offset = 0;
2851
2852 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2853 if (ret < 0)
2854 goto out_bctl;
2855 if (ret > 0) { /* ret = -ENOENT; */
2856 ret = 0;
2857 goto out_bctl;
2858 }
2859
2860 leaf = path->nodes[0];
2861 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2862
2863 bctl->fs_info = tree_root->fs_info;
2864 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2865
2866 btrfs_balance_data(leaf, item, &disk_bargs);
2867 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2868 btrfs_balance_meta(leaf, item, &disk_bargs);
2869 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2870 btrfs_balance_sys(leaf, item, &disk_bargs);
2871 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2872
2873 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2874 if (IS_ERR(tsk))
2875 ret = PTR_ERR(tsk);
2876 else
2877 goto out;
2878
2879out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002880 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002881out:
2882 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002883 return ret;
2884}
2885
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002886int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2887{
2888 int ret = 0;
2889
2890 mutex_lock(&fs_info->balance_mutex);
2891 if (!fs_info->balance_ctl) {
2892 mutex_unlock(&fs_info->balance_mutex);
2893 return -ENOTCONN;
2894 }
2895
2896 if (atomic_read(&fs_info->balance_running)) {
2897 atomic_inc(&fs_info->balance_pause_req);
2898 mutex_unlock(&fs_info->balance_mutex);
2899
2900 wait_event(fs_info->balance_wait_q,
2901 atomic_read(&fs_info->balance_running) == 0);
2902
2903 mutex_lock(&fs_info->balance_mutex);
2904 /* we are good with balance_ctl ripped off from under us */
2905 BUG_ON(atomic_read(&fs_info->balance_running));
2906 atomic_dec(&fs_info->balance_pause_req);
2907 } else {
2908 ret = -ENOTCONN;
2909 }
2910
2911 mutex_unlock(&fs_info->balance_mutex);
2912 return ret;
2913}
2914
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002915int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2916{
2917 mutex_lock(&fs_info->balance_mutex);
2918 if (!fs_info->balance_ctl) {
2919 mutex_unlock(&fs_info->balance_mutex);
2920 return -ENOTCONN;
2921 }
2922
2923 atomic_inc(&fs_info->balance_cancel_req);
2924 /*
2925 * if we are running just wait and return, balance item is
2926 * deleted in btrfs_balance in this case
2927 */
2928 if (atomic_read(&fs_info->balance_running)) {
2929 mutex_unlock(&fs_info->balance_mutex);
2930 wait_event(fs_info->balance_wait_q,
2931 atomic_read(&fs_info->balance_running) == 0);
2932 mutex_lock(&fs_info->balance_mutex);
2933 } else {
2934 /* __cancel_balance needs volume_mutex */
2935 mutex_unlock(&fs_info->balance_mutex);
2936 mutex_lock(&fs_info->volume_mutex);
2937 mutex_lock(&fs_info->balance_mutex);
2938
2939 if (fs_info->balance_ctl)
2940 __cancel_balance(fs_info);
2941
2942 mutex_unlock(&fs_info->volume_mutex);
2943 }
2944
2945 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2946 atomic_dec(&fs_info->balance_cancel_req);
2947 mutex_unlock(&fs_info->balance_mutex);
2948 return 0;
2949}
2950
Chris Mason8f18cf12008-04-25 16:53:30 -04002951/*
2952 * shrinking a device means finding all of the device extents past
2953 * the new size, and then following the back refs to the chunks.
2954 * The chunk relocation code actually frees the device extent
2955 */
2956int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2957{
2958 struct btrfs_trans_handle *trans;
2959 struct btrfs_root *root = device->dev_root;
2960 struct btrfs_dev_extent *dev_extent = NULL;
2961 struct btrfs_path *path;
2962 u64 length;
2963 u64 chunk_tree;
2964 u64 chunk_objectid;
2965 u64 chunk_offset;
2966 int ret;
2967 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002968 int failed = 0;
2969 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002970 struct extent_buffer *l;
2971 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002972 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002973 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002974 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002975 u64 diff = device->total_bytes - new_size;
2976
Yan Zheng2b820322008-11-17 21:11:30 -05002977 if (new_size >= device->total_bytes)
2978 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002979
2980 path = btrfs_alloc_path();
2981 if (!path)
2982 return -ENOMEM;
2983
Chris Mason8f18cf12008-04-25 16:53:30 -04002984 path->reada = 2;
2985
Chris Mason7d9eb122008-07-08 14:19:17 -04002986 lock_chunks(root);
2987
Chris Mason8f18cf12008-04-25 16:53:30 -04002988 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002989 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002990 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002991 spin_lock(&root->fs_info->free_chunk_lock);
2992 root->fs_info->free_chunk_space -= diff;
2993 spin_unlock(&root->fs_info->free_chunk_lock);
2994 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002995 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002996
Josef Bacikba1bf482009-09-11 16:11:19 -04002997again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002998 key.objectid = device->devid;
2999 key.offset = (u64)-1;
3000 key.type = BTRFS_DEV_EXTENT_KEY;
3001
3002 while (1) {
3003 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3004 if (ret < 0)
3005 goto done;
3006
3007 ret = btrfs_previous_item(root, path, 0, key.type);
3008 if (ret < 0)
3009 goto done;
3010 if (ret) {
3011 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003012 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003013 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003014 }
3015
3016 l = path->nodes[0];
3017 slot = path->slots[0];
3018 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3019
Josef Bacikba1bf482009-09-11 16:11:19 -04003020 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003021 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003022 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003023 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003024
3025 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3026 length = btrfs_dev_extent_length(l, dev_extent);
3027
Josef Bacikba1bf482009-09-11 16:11:19 -04003028 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003029 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003030 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003031 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003032
3033 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3034 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3035 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003036 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003037
3038 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3039 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003040 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003041 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003042 if (ret == -ENOSPC)
3043 failed++;
3044 key.offset -= 1;
3045 }
3046
3047 if (failed && !retried) {
3048 failed = 0;
3049 retried = true;
3050 goto again;
3051 } else if (failed && retried) {
3052 ret = -ENOSPC;
3053 lock_chunks(root);
3054
3055 device->total_bytes = old_size;
3056 if (device->writeable)
3057 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003058 spin_lock(&root->fs_info->free_chunk_lock);
3059 root->fs_info->free_chunk_space += diff;
3060 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003061 unlock_chunks(root);
3062 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003063 }
3064
Chris Balld6397ba2009-04-27 07:29:03 -04003065 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003066 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003067 if (IS_ERR(trans)) {
3068 ret = PTR_ERR(trans);
3069 goto done;
3070 }
3071
Chris Balld6397ba2009-04-27 07:29:03 -04003072 lock_chunks(root);
3073
3074 device->disk_total_bytes = new_size;
3075 /* Now btrfs_update_device() will change the on-disk size. */
3076 ret = btrfs_update_device(trans, device);
3077 if (ret) {
3078 unlock_chunks(root);
3079 btrfs_end_transaction(trans, root);
3080 goto done;
3081 }
3082 WARN_ON(diff > old_total);
3083 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3084 unlock_chunks(root);
3085 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003086done:
3087 btrfs_free_path(path);
3088 return ret;
3089}
3090
Li Zefan125ccb02011-12-08 15:07:24 +08003091static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003092 struct btrfs_key *key,
3093 struct btrfs_chunk *chunk, int item_size)
3094{
David Sterba6c417612011-04-13 15:41:04 +02003095 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003096 struct btrfs_disk_key disk_key;
3097 u32 array_size;
3098 u8 *ptr;
3099
3100 array_size = btrfs_super_sys_array_size(super_copy);
3101 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3102 return -EFBIG;
3103
3104 ptr = super_copy->sys_chunk_array + array_size;
3105 btrfs_cpu_key_to_disk(&disk_key, key);
3106 memcpy(ptr, &disk_key, sizeof(disk_key));
3107 ptr += sizeof(disk_key);
3108 memcpy(ptr, chunk, item_size);
3109 item_size += sizeof(disk_key);
3110 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3111 return 0;
3112}
3113
Miao Xieb2117a32011-01-05 10:07:28 +00003114/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003115 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003116 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003117static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003118{
Arne Jansen73c5de02011-04-12 12:07:57 +02003119 const struct btrfs_device_info *di_a = a;
3120 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003121
Arne Jansen73c5de02011-04-12 12:07:57 +02003122 if (di_a->max_avail > di_b->max_avail)
3123 return -1;
3124 if (di_a->max_avail < di_b->max_avail)
3125 return 1;
3126 if (di_a->total_avail > di_b->total_avail)
3127 return -1;
3128 if (di_a->total_avail < di_b->total_avail)
3129 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003130 return 0;
3131}
3132
3133static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3134 struct btrfs_root *extent_root,
3135 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003136 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003137 u64 start, u64 type)
3138{
3139 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003140 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3141 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003142 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003143 struct extent_map_tree *em_tree;
3144 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003145 struct btrfs_device_info *devices_info = NULL;
3146 u64 total_avail;
3147 int num_stripes; /* total number of stripes to allocate */
3148 int sub_stripes; /* sub_stripes info for map */
3149 int dev_stripes; /* stripes per dev */
3150 int devs_max; /* max devs to use */
3151 int devs_min; /* min devs needed */
3152 int devs_increment; /* ndevs has to be a multiple of this */
3153 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003154 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003155 u64 max_stripe_size;
3156 u64 max_chunk_size;
3157 u64 stripe_size;
3158 u64 num_bytes;
3159 int ndevs;
3160 int i;
3161 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003162
3163 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
3164 (type & BTRFS_BLOCK_GROUP_DUP)) {
3165 WARN_ON(1);
3166 type &= ~BTRFS_BLOCK_GROUP_DUP;
3167 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003168
Miao Xieb2117a32011-01-05 10:07:28 +00003169 if (list_empty(&fs_devices->alloc_list))
3170 return -ENOSPC;
3171
Arne Jansen73c5de02011-04-12 12:07:57 +02003172 sub_stripes = 1;
3173 dev_stripes = 1;
3174 devs_increment = 1;
3175 ncopies = 1;
3176 devs_max = 0; /* 0 == as many as possible */
3177 devs_min = 1;
3178
3179 /*
3180 * define the properties of each RAID type.
3181 * FIXME: move this to a global table and use it in all RAID
3182 * calculation code
3183 */
3184 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3185 dev_stripes = 2;
3186 ncopies = 2;
3187 devs_max = 1;
3188 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3189 devs_min = 2;
3190 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3191 devs_increment = 2;
3192 ncopies = 2;
3193 devs_max = 2;
3194 devs_min = 2;
3195 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3196 sub_stripes = 2;
3197 devs_increment = 2;
3198 ncopies = 2;
3199 devs_min = 4;
3200 } else {
3201 devs_max = 1;
3202 }
3203
3204 if (type & BTRFS_BLOCK_GROUP_DATA) {
3205 max_stripe_size = 1024 * 1024 * 1024;
3206 max_chunk_size = 10 * max_stripe_size;
3207 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003208 /* for larger filesystems, use larger metadata chunks */
3209 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3210 max_stripe_size = 1024 * 1024 * 1024;
3211 else
3212 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003213 max_chunk_size = max_stripe_size;
3214 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003215 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003216 max_chunk_size = 2 * max_stripe_size;
3217 } else {
3218 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3219 type);
3220 BUG_ON(1);
3221 }
3222
3223 /* we don't want a chunk larger than 10% of writeable space */
3224 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3225 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003226
3227 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3228 GFP_NOFS);
3229 if (!devices_info)
3230 return -ENOMEM;
3231
Arne Jansen73c5de02011-04-12 12:07:57 +02003232 cur = fs_devices->alloc_list.next;
3233
3234 /*
3235 * in the first pass through the devices list, we gather information
3236 * about the available holes on each device.
3237 */
3238 ndevs = 0;
3239 while (cur != &fs_devices->alloc_list) {
3240 struct btrfs_device *device;
3241 u64 max_avail;
3242 u64 dev_offset;
3243
3244 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3245
3246 cur = cur->next;
3247
3248 if (!device->writeable) {
3249 printk(KERN_ERR
3250 "btrfs: read-only device in alloc_list\n");
3251 WARN_ON(1);
3252 continue;
3253 }
3254
3255 if (!device->in_fs_metadata)
3256 continue;
3257
3258 if (device->total_bytes > device->bytes_used)
3259 total_avail = device->total_bytes - device->bytes_used;
3260 else
3261 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003262
3263 /* If there is no space on this device, skip it. */
3264 if (total_avail == 0)
3265 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003266
Li Zefan125ccb02011-12-08 15:07:24 +08003267 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003268 max_stripe_size * dev_stripes,
3269 &dev_offset, &max_avail);
3270 if (ret && ret != -ENOSPC)
3271 goto error;
3272
3273 if (ret == 0)
3274 max_avail = max_stripe_size * dev_stripes;
3275
3276 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3277 continue;
3278
3279 devices_info[ndevs].dev_offset = dev_offset;
3280 devices_info[ndevs].max_avail = max_avail;
3281 devices_info[ndevs].total_avail = total_avail;
3282 devices_info[ndevs].dev = device;
3283 ++ndevs;
3284 }
3285
3286 /*
3287 * now sort the devices by hole size / available space
3288 */
3289 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3290 btrfs_cmp_device_info, NULL);
3291
3292 /* round down to number of usable stripes */
3293 ndevs -= ndevs % devs_increment;
3294
3295 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3296 ret = -ENOSPC;
3297 goto error;
3298 }
3299
3300 if (devs_max && ndevs > devs_max)
3301 ndevs = devs_max;
3302 /*
3303 * the primary goal is to maximize the number of stripes, so use as many
3304 * devices as possible, even if the stripes are not maximum sized.
3305 */
3306 stripe_size = devices_info[ndevs-1].max_avail;
3307 num_stripes = ndevs * dev_stripes;
3308
3309 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3310 stripe_size = max_chunk_size * ncopies;
3311 do_div(stripe_size, num_stripes);
3312 }
3313
3314 do_div(stripe_size, dev_stripes);
3315 do_div(stripe_size, BTRFS_STRIPE_LEN);
3316 stripe_size *= BTRFS_STRIPE_LEN;
3317
Miao Xieb2117a32011-01-05 10:07:28 +00003318 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3319 if (!map) {
3320 ret = -ENOMEM;
3321 goto error;
3322 }
3323 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003324
Arne Jansen73c5de02011-04-12 12:07:57 +02003325 for (i = 0; i < ndevs; ++i) {
3326 for (j = 0; j < dev_stripes; ++j) {
3327 int s = i * dev_stripes + j;
3328 map->stripes[s].dev = devices_info[i].dev;
3329 map->stripes[s].physical = devices_info[i].dev_offset +
3330 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003331 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003332 }
Chris Mason593060d2008-03-25 16:50:33 -04003333 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003334 map->stripe_len = BTRFS_STRIPE_LEN;
3335 map->io_align = BTRFS_STRIPE_LEN;
3336 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003337 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003338 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003339
Yan Zheng2b820322008-11-17 21:11:30 -05003340 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003341 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003342
Arne Jansen73c5de02011-04-12 12:07:57 +02003343 *stripe_size_out = stripe_size;
3344 *num_bytes_out = num_bytes;
3345
3346 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003347
David Sterba172ddd62011-04-21 00:48:27 +02003348 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003349 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003350 ret = -ENOMEM;
3351 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003352 }
Chris Mason0b86a832008-03-24 15:01:56 -04003353 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003354 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003355 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003356 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003357 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003358
Chris Mason0b86a832008-03-24 15:01:56 -04003359 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003360 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003361 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003362 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003363 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003364 if (ret)
3365 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003366
3367 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3368 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003369 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003370 if (ret)
3371 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003372
Arne Jansen73c5de02011-04-12 12:07:57 +02003373 for (i = 0; i < map->num_stripes; ++i) {
3374 struct btrfs_device *device;
3375 u64 dev_offset;
3376
3377 device = map->stripes[i].dev;
3378 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003379
3380 ret = btrfs_alloc_dev_extent(trans, device,
3381 info->chunk_root->root_key.objectid,
3382 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003383 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003384 if (ret) {
3385 btrfs_abort_transaction(trans, extent_root, ret);
3386 goto error;
3387 }
Yan Zheng2b820322008-11-17 21:11:30 -05003388 }
3389
Miao Xieb2117a32011-01-05 10:07:28 +00003390 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003391 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003392
3393error:
3394 kfree(map);
3395 kfree(devices_info);
3396 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003397}
3398
3399static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3400 struct btrfs_root *extent_root,
3401 struct map_lookup *map, u64 chunk_offset,
3402 u64 chunk_size, u64 stripe_size)
3403{
3404 u64 dev_offset;
3405 struct btrfs_key key;
3406 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3407 struct btrfs_device *device;
3408 struct btrfs_chunk *chunk;
3409 struct btrfs_stripe *stripe;
3410 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3411 int index = 0;
3412 int ret;
3413
3414 chunk = kzalloc(item_size, GFP_NOFS);
3415 if (!chunk)
3416 return -ENOMEM;
3417
3418 index = 0;
3419 while (index < map->num_stripes) {
3420 device = map->stripes[index].dev;
3421 device->bytes_used += stripe_size;
3422 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003423 if (ret)
3424 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003425 index++;
3426 }
3427
Josef Bacik2bf64752011-09-26 17:12:22 -04003428 spin_lock(&extent_root->fs_info->free_chunk_lock);
3429 extent_root->fs_info->free_chunk_space -= (stripe_size *
3430 map->num_stripes);
3431 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3432
Yan Zheng2b820322008-11-17 21:11:30 -05003433 index = 0;
3434 stripe = &chunk->stripe;
3435 while (index < map->num_stripes) {
3436 device = map->stripes[index].dev;
3437 dev_offset = map->stripes[index].physical;
3438
3439 btrfs_set_stack_stripe_devid(stripe, device->devid);
3440 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3441 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3442 stripe++;
3443 index++;
3444 }
3445
3446 btrfs_set_stack_chunk_length(chunk, chunk_size);
3447 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3448 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3449 btrfs_set_stack_chunk_type(chunk, map->type);
3450 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3451 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3452 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3453 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3454 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3455
3456 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3457 key.type = BTRFS_CHUNK_ITEM_KEY;
3458 key.offset = chunk_offset;
3459
3460 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003461
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003462 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3463 /*
3464 * TODO: Cleanup of inserted chunk root in case of
3465 * failure.
3466 */
Li Zefan125ccb02011-12-08 15:07:24 +08003467 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003468 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003469 }
liubo1abe9b82011-03-24 11:18:59 +00003470
Mark Fasheh3acd3952011-09-08 17:40:01 -07003471out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003472 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003473 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003474}
3475
3476/*
3477 * Chunk allocation falls into two parts. The first part does works
3478 * that make the new allocated chunk useable, but not do any operation
3479 * that modifies the chunk tree. The second part does the works that
3480 * require modifying the chunk tree. This division is important for the
3481 * bootstrap process of adding storage to a seed btrfs.
3482 */
3483int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3484 struct btrfs_root *extent_root, u64 type)
3485{
3486 u64 chunk_offset;
3487 u64 chunk_size;
3488 u64 stripe_size;
3489 struct map_lookup *map;
3490 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3491 int ret;
3492
3493 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3494 &chunk_offset);
3495 if (ret)
3496 return ret;
3497
3498 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3499 &stripe_size, chunk_offset, type);
3500 if (ret)
3501 return ret;
3502
3503 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3504 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003505 if (ret)
3506 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003507 return 0;
3508}
3509
Chris Masond3977122009-01-05 21:25:51 -05003510static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003511 struct btrfs_root *root,
3512 struct btrfs_device *device)
3513{
3514 u64 chunk_offset;
3515 u64 sys_chunk_offset;
3516 u64 chunk_size;
3517 u64 sys_chunk_size;
3518 u64 stripe_size;
3519 u64 sys_stripe_size;
3520 u64 alloc_profile;
3521 struct map_lookup *map;
3522 struct map_lookup *sys_map;
3523 struct btrfs_fs_info *fs_info = root->fs_info;
3524 struct btrfs_root *extent_root = fs_info->extent_root;
3525 int ret;
3526
3527 ret = find_next_chunk(fs_info->chunk_root,
3528 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003529 if (ret)
3530 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003531
3532 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003533 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003534 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3535
3536 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3537 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003538 if (ret)
3539 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003540
3541 sys_chunk_offset = chunk_offset + chunk_size;
3542
3543 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003544 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003545 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3546
3547 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3548 &sys_chunk_size, &sys_stripe_size,
3549 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003550 if (ret)
3551 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003552
3553 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003554 if (ret)
3555 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003556
3557 /*
3558 * Modifying chunk tree needs allocating new blocks from both
3559 * system block group and metadata block group. So we only can
3560 * do operations require modifying the chunk tree after both
3561 * block groups were created.
3562 */
3563 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3564 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003565 if (ret)
3566 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003567
3568 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3569 sys_chunk_offset, sys_chunk_size,
3570 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003571 if (ret)
3572 goto abort;
3573
Yan Zheng2b820322008-11-17 21:11:30 -05003574 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003575
3576abort:
3577 btrfs_abort_transaction(trans, root, ret);
3578 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003579}
3580
3581int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3582{
3583 struct extent_map *em;
3584 struct map_lookup *map;
3585 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3586 int readonly = 0;
3587 int i;
3588
Chris Mason890871b2009-09-02 16:24:52 -04003589 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003590 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003591 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003592 if (!em)
3593 return 1;
3594
Josef Bacikf48b9072010-01-27 02:07:59 +00003595 if (btrfs_test_opt(root, DEGRADED)) {
3596 free_extent_map(em);
3597 return 0;
3598 }
3599
Yan Zheng2b820322008-11-17 21:11:30 -05003600 map = (struct map_lookup *)em->bdev;
3601 for (i = 0; i < map->num_stripes; i++) {
3602 if (!map->stripes[i].dev->writeable) {
3603 readonly = 1;
3604 break;
3605 }
3606 }
3607 free_extent_map(em);
3608 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003609}
3610
3611void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3612{
David Sterbaa8067e02011-04-21 00:34:43 +02003613 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003614}
3615
3616void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3617{
3618 struct extent_map *em;
3619
Chris Masond3977122009-01-05 21:25:51 -05003620 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003621 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003622 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3623 if (em)
3624 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003625 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003626 if (!em)
3627 break;
3628 kfree(em->bdev);
3629 /* once for us */
3630 free_extent_map(em);
3631 /* once for the tree */
3632 free_extent_map(em);
3633 }
3634}
3635
Chris Masonf1885912008-04-09 16:28:12 -04003636int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3637{
3638 struct extent_map *em;
3639 struct map_lookup *map;
3640 struct extent_map_tree *em_tree = &map_tree->map_tree;
3641 int ret;
3642
Chris Mason890871b2009-09-02 16:24:52 -04003643 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003644 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003645 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003646 BUG_ON(!em);
3647
3648 BUG_ON(em->start > logical || em->start + em->len < logical);
3649 map = (struct map_lookup *)em->bdev;
3650 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3651 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003652 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3653 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003654 else
3655 ret = 1;
3656 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003657 return ret;
3658}
3659
Chris Masondfe25022008-05-13 13:46:40 -04003660static int find_live_mirror(struct map_lookup *map, int first, int num,
3661 int optimal)
3662{
3663 int i;
3664 if (map->stripes[optimal].dev->bdev)
3665 return optimal;
3666 for (i = first; i < first + num; i++) {
3667 if (map->stripes[i].dev->bdev)
3668 return i;
3669 }
3670 /* we couldn't find one that doesn't fail. Just return something
3671 * and the io error handling code will clean up eventually
3672 */
3673 return optimal;
3674}
3675
Chris Masonf2d8d742008-04-21 10:03:05 -04003676static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3677 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003678 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003679 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003680{
3681 struct extent_map *em;
3682 struct map_lookup *map;
3683 struct extent_map_tree *em_tree = &map_tree->map_tree;
3684 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003685 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003686 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003687 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003688 u64 stripe_nr_orig;
3689 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003690 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003691 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003692 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003693 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003694 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003695 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003696
Chris Mason890871b2009-09-02 16:24:52 -04003697 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003698 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003699 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003700
Chris Mason3b951512008-04-17 11:29:12 -04003701 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003702 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3703 (unsigned long long)logical,
3704 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003705 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003706 }
Chris Mason0b86a832008-03-24 15:01:56 -04003707
3708 BUG_ON(em->start > logical || em->start + em->len < logical);
3709 map = (struct map_lookup *)em->bdev;
3710 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003711
Chris Masonf1885912008-04-09 16:28:12 -04003712 if (mirror_num > map->num_stripes)
3713 mirror_num = 0;
3714
Chris Mason593060d2008-03-25 16:50:33 -04003715 stripe_nr = offset;
3716 /*
3717 * stripe_nr counts the total number of stripes we have to stride
3718 * to get to this block
3719 */
3720 do_div(stripe_nr, map->stripe_len);
3721
3722 stripe_offset = stripe_nr * map->stripe_len;
3723 BUG_ON(offset < stripe_offset);
3724
3725 /* stripe_offset is the offset of this block in its stripe*/
3726 stripe_offset = offset - stripe_offset;
3727
Li Dongyangfce3bb92011-03-24 10:24:26 +00003728 if (rw & REQ_DISCARD)
3729 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003730 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003731 /* we limit the length of each bio to what fits in a stripe */
3732 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003733 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003734 } else {
3735 *length = em->len - offset;
3736 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003737
Jan Schmidta1d3c472011-08-04 17:15:33 +02003738 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003739 goto out;
3740
Chris Masonf2d8d742008-04-21 10:03:05 -04003741 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003742 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003743 stripe_nr_orig = stripe_nr;
3744 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3745 (~(map->stripe_len - 1));
3746 do_div(stripe_nr_end, map->stripe_len);
3747 stripe_end_offset = stripe_nr_end * map->stripe_len -
3748 (offset + *length);
3749 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3750 if (rw & REQ_DISCARD)
3751 num_stripes = min_t(u64, map->num_stripes,
3752 stripe_nr_end - stripe_nr_orig);
3753 stripe_index = do_div(stripe_nr, map->num_stripes);
3754 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003755 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003756 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003757 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003758 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003759 else {
3760 stripe_index = find_live_mirror(map, 0,
3761 map->num_stripes,
3762 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003763 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003764 }
Chris Mason2fff7342008-04-29 14:12:09 -04003765
Chris Mason611f0e02008-04-03 16:29:03 -04003766 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003767 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003768 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003769 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003770 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003771 } else {
3772 mirror_num = 1;
3773 }
Chris Mason2fff7342008-04-29 14:12:09 -04003774
Chris Mason321aecc2008-04-16 10:49:51 -04003775 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3776 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003777
3778 stripe_index = do_div(stripe_nr, factor);
3779 stripe_index *= map->sub_stripes;
3780
Jens Axboe7eaceac2011-03-10 08:52:07 +01003781 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003782 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003783 else if (rw & REQ_DISCARD)
3784 num_stripes = min_t(u64, map->sub_stripes *
3785 (stripe_nr_end - stripe_nr_orig),
3786 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003787 else if (mirror_num)
3788 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003789 else {
3790 stripe_index = find_live_mirror(map, stripe_index,
3791 map->sub_stripes, stripe_index +
3792 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003793 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003794 }
Chris Mason8790d502008-04-03 16:29:03 -04003795 } else {
3796 /*
3797 * after this do_div call, stripe_nr is the number of stripes
3798 * on this device we have to walk to find the data, and
3799 * stripe_index is the number of our device in the stripe array
3800 */
3801 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003802 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003803 }
Chris Mason593060d2008-03-25 16:50:33 -04003804 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003805
Li Zefande11cc12011-12-01 12:55:47 +08003806 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3807 if (!bbio) {
3808 ret = -ENOMEM;
3809 goto out;
3810 }
3811 atomic_set(&bbio->error, 0);
3812
Li Dongyangfce3bb92011-03-24 10:24:26 +00003813 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003814 int factor = 0;
3815 int sub_stripes = 0;
3816 u64 stripes_per_dev = 0;
3817 u32 remaining_stripes = 0;
3818
3819 if (map->type &
3820 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3821 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3822 sub_stripes = 1;
3823 else
3824 sub_stripes = map->sub_stripes;
3825
3826 factor = map->num_stripes / sub_stripes;
3827 stripes_per_dev = div_u64_rem(stripe_nr_end -
3828 stripe_nr_orig,
3829 factor,
3830 &remaining_stripes);
3831 }
3832
Li Dongyangfce3bb92011-03-24 10:24:26 +00003833 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003834 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003835 map->stripes[stripe_index].physical +
3836 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003837 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003838
Li Zefanec9ef7a2011-12-01 14:06:42 +08003839 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3840 BTRFS_BLOCK_GROUP_RAID10)) {
3841 bbio->stripes[i].length = stripes_per_dev *
3842 map->stripe_len;
3843 if (i / sub_stripes < remaining_stripes)
3844 bbio->stripes[i].length +=
3845 map->stripe_len;
3846 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003847 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003848 stripe_offset;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003849 if ((i / sub_stripes + 1) %
3850 sub_stripes == remaining_stripes)
3851 bbio->stripes[i].length -=
3852 stripe_end_offset;
3853 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003854 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003855 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003856 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003857
3858 stripe_index++;
3859 if (stripe_index == map->num_stripes) {
3860 /* This could only happen for RAID0/10 */
3861 stripe_index = 0;
3862 stripe_nr++;
3863 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003864 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003865 } else {
3866 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003867 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003868 map->stripes[stripe_index].physical +
3869 stripe_offset +
3870 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003871 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003872 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003873 stripe_index++;
3874 }
Chris Mason593060d2008-03-25 16:50:33 -04003875 }
Li Zefande11cc12011-12-01 12:55:47 +08003876
3877 if (rw & REQ_WRITE) {
3878 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3879 BTRFS_BLOCK_GROUP_RAID10 |
3880 BTRFS_BLOCK_GROUP_DUP)) {
3881 max_errors = 1;
3882 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003883 }
Li Zefande11cc12011-12-01 12:55:47 +08003884
3885 *bbio_ret = bbio;
3886 bbio->num_stripes = num_stripes;
3887 bbio->max_errors = max_errors;
3888 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003889out:
Chris Mason0b86a832008-03-24 15:01:56 -04003890 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003891 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003892}
3893
Chris Masonf2d8d742008-04-21 10:03:05 -04003894int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3895 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003896 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003897{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003898 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003899 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003900}
3901
Yan Zhenga512bbf2008-12-08 16:46:26 -05003902int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3903 u64 chunk_start, u64 physical, u64 devid,
3904 u64 **logical, int *naddrs, int *stripe_len)
3905{
3906 struct extent_map_tree *em_tree = &map_tree->map_tree;
3907 struct extent_map *em;
3908 struct map_lookup *map;
3909 u64 *buf;
3910 u64 bytenr;
3911 u64 length;
3912 u64 stripe_nr;
3913 int i, j, nr = 0;
3914
Chris Mason890871b2009-09-02 16:24:52 -04003915 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003916 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003917 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003918
3919 BUG_ON(!em || em->start != chunk_start);
3920 map = (struct map_lookup *)em->bdev;
3921
3922 length = em->len;
3923 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3924 do_div(length, map->num_stripes / map->sub_stripes);
3925 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3926 do_div(length, map->num_stripes);
3927
3928 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003929 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003930
3931 for (i = 0; i < map->num_stripes; i++) {
3932 if (devid && map->stripes[i].dev->devid != devid)
3933 continue;
3934 if (map->stripes[i].physical > physical ||
3935 map->stripes[i].physical + length <= physical)
3936 continue;
3937
3938 stripe_nr = physical - map->stripes[i].physical;
3939 do_div(stripe_nr, map->stripe_len);
3940
3941 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3942 stripe_nr = stripe_nr * map->num_stripes + i;
3943 do_div(stripe_nr, map->sub_stripes);
3944 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3945 stripe_nr = stripe_nr * map->num_stripes + i;
3946 }
3947 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003948 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003949 for (j = 0; j < nr; j++) {
3950 if (buf[j] == bytenr)
3951 break;
3952 }
Chris Mason934d3752008-12-08 16:43:10 -05003953 if (j == nr) {
3954 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003955 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003956 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003957 }
3958
Yan Zhenga512bbf2008-12-08 16:46:26 -05003959 *logical = buf;
3960 *naddrs = nr;
3961 *stripe_len = map->stripe_len;
3962
3963 free_extent_map(em);
3964 return 0;
3965}
3966
Jan Schmidta1d3c472011-08-04 17:15:33 +02003967static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003968{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003969 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003970 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003971
Chris Mason8790d502008-04-03 16:29:03 -04003972 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003973 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003974
Jan Schmidta1d3c472011-08-04 17:15:33 +02003975 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003976 is_orig_bio = 1;
3977
Jan Schmidta1d3c472011-08-04 17:15:33 +02003978 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003979 if (!is_orig_bio) {
3980 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003981 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003982 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003983 bio->bi_private = bbio->private;
3984 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003985 bio->bi_bdev = (struct block_device *)
3986 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003987 /* only send an error to the higher layers if it is
3988 * beyond the tolerance of the multi-bio
3989 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003990 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003991 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003992 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003993 /*
3994 * this bio is actually up to date, we didn't
3995 * go over the max number of errors
3996 */
3997 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003998 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003999 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004000 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004001
4002 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004003 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004004 bio_put(bio);
4005 }
Chris Mason8790d502008-04-03 16:29:03 -04004006}
4007
Chris Mason8b712842008-06-11 16:50:36 -04004008struct async_sched {
4009 struct bio *bio;
4010 int rw;
4011 struct btrfs_fs_info *info;
4012 struct btrfs_work work;
4013};
4014
4015/*
4016 * see run_scheduled_bios for a description of why bios are collected for
4017 * async submit.
4018 *
4019 * This will add one bio to the pending list for a device and make sure
4020 * the work struct is scheduled.
4021 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004022static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004023 struct btrfs_device *device,
4024 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004025{
4026 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004027 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004028
4029 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004030 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004031 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004032 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004033 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004034 return;
Chris Mason8b712842008-06-11 16:50:36 -04004035 }
4036
4037 /*
Chris Mason0986fe92008-08-15 15:34:15 -04004038 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004039 * higher layers. Otherwise, the async bio makes it appear we have
4040 * made progress against dirty pages when we've really just put it
4041 * on a queue for later
4042 */
Chris Mason0986fe92008-08-15 15:34:15 -04004043 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004044 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004045 bio->bi_next = NULL;
4046 bio->bi_rw |= rw;
4047
4048 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004049 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004050 pending_bios = &device->pending_sync_bios;
4051 else
4052 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004053
Chris Masonffbd5172009-04-20 15:50:09 -04004054 if (pending_bios->tail)
4055 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004056
Chris Masonffbd5172009-04-20 15:50:09 -04004057 pending_bios->tail = bio;
4058 if (!pending_bios->head)
4059 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004060 if (device->running_pending)
4061 should_queue = 0;
4062
4063 spin_unlock(&device->io_lock);
4064
4065 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004066 btrfs_queue_worker(&root->fs_info->submit_workers,
4067 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004068}
4069
Chris Masonf1885912008-04-09 16:28:12 -04004070int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004071 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004072{
4073 struct btrfs_mapping_tree *map_tree;
4074 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004075 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004076 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004077 u64 length = 0;
4078 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004079 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004080 int dev_nr = 0;
4081 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004082 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004083
Chris Masonf2d8d742008-04-21 10:03:05 -04004084 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004085 map_tree = &root->fs_info->mapping_tree;
4086 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004087
Jan Schmidta1d3c472011-08-04 17:15:33 +02004088 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004089 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004090 if (ret) /* -ENOMEM */
4091 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004092
Jan Schmidta1d3c472011-08-04 17:15:33 +02004093 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004094 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004095 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4096 "len %llu\n", (unsigned long long)logical,
4097 (unsigned long long)length,
4098 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004099 BUG();
4100 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004101
4102 bbio->orig_bio = first_bio;
4103 bbio->private = first_bio->bi_private;
4104 bbio->end_io = first_bio->bi_end_io;
4105 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004106
Chris Masond3977122009-01-05 21:25:51 -05004107 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004108 if (dev_nr < total_devs - 1) {
4109 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004111 } else {
4112 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004113 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004114 bio->bi_private = bbio;
4115 bio->bi_end_io = btrfs_end_bio;
4116 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4117 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004118 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004119 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4120 "(%s id %llu), size=%u\n", rw,
4121 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4122 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004123 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004124 if (async_submit)
4125 schedule_bio(root, dev, rw, bio);
4126 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004127 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004128 } else {
4129 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4130 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004131 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004132 }
Chris Mason8790d502008-04-03 16:29:03 -04004133 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004134 }
Chris Mason0b86a832008-03-24 15:01:56 -04004135 return 0;
4136}
4137
Chris Masona4437552008-04-18 10:29:38 -04004138struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004139 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004140{
Yan Zheng2b820322008-11-17 21:11:30 -05004141 struct btrfs_device *device;
4142 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004143
Yan Zheng2b820322008-11-17 21:11:30 -05004144 cur_devices = root->fs_info->fs_devices;
4145 while (cur_devices) {
4146 if (!fsid ||
4147 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4148 device = __find_device(&cur_devices->devices,
4149 devid, uuid);
4150 if (device)
4151 return device;
4152 }
4153 cur_devices = cur_devices->seed;
4154 }
4155 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004156}
4157
Chris Masondfe25022008-05-13 13:46:40 -04004158static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4159 u64 devid, u8 *dev_uuid)
4160{
4161 struct btrfs_device *device;
4162 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4163
4164 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004165 if (!device)
4166 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004167 list_add(&device->dev_list,
4168 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004169 device->dev_root = root->fs_info->dev_root;
4170 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004171 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004172 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004173 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004174 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004175 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004176 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004177 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004178 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4179 return device;
4180}
4181
Chris Mason0b86a832008-03-24 15:01:56 -04004182static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4183 struct extent_buffer *leaf,
4184 struct btrfs_chunk *chunk)
4185{
4186 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4187 struct map_lookup *map;
4188 struct extent_map *em;
4189 u64 logical;
4190 u64 length;
4191 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004192 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004193 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004194 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004195 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004196
Chris Masone17cade2008-04-15 15:41:47 -04004197 logical = key->offset;
4198 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004199
Chris Mason890871b2009-09-02 16:24:52 -04004200 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004201 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004202 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004203
4204 /* already mapped? */
4205 if (em && em->start <= logical && em->start + em->len > logical) {
4206 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004207 return 0;
4208 } else if (em) {
4209 free_extent_map(em);
4210 }
Chris Mason0b86a832008-03-24 15:01:56 -04004211
David Sterba172ddd62011-04-21 00:48:27 +02004212 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004213 if (!em)
4214 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004215 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4216 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004217 if (!map) {
4218 free_extent_map(em);
4219 return -ENOMEM;
4220 }
4221
4222 em->bdev = (struct block_device *)map;
4223 em->start = logical;
4224 em->len = length;
4225 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004226 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004227
Chris Mason593060d2008-03-25 16:50:33 -04004228 map->num_stripes = num_stripes;
4229 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4230 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4231 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4232 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4233 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004234 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004235 for (i = 0; i < num_stripes; i++) {
4236 map->stripes[i].physical =
4237 btrfs_stripe_offset_nr(leaf, chunk, i);
4238 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004239 read_extent_buffer(leaf, uuid, (unsigned long)
4240 btrfs_stripe_dev_uuid_nr(chunk, i),
4241 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004242 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4243 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004244 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004245 kfree(map);
4246 free_extent_map(em);
4247 return -EIO;
4248 }
Chris Masondfe25022008-05-13 13:46:40 -04004249 if (!map->stripes[i].dev) {
4250 map->stripes[i].dev =
4251 add_missing_dev(root, devid, uuid);
4252 if (!map->stripes[i].dev) {
4253 kfree(map);
4254 free_extent_map(em);
4255 return -EIO;
4256 }
4257 }
4258 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004259 }
4260
Chris Mason890871b2009-09-02 16:24:52 -04004261 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004262 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004263 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004265 free_extent_map(em);
4266
4267 return 0;
4268}
4269
Jeff Mahoney143bede2012-03-01 14:56:26 +01004270static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004271 struct btrfs_dev_item *dev_item,
4272 struct btrfs_device *device)
4273{
4274 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004275
4276 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004277 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4278 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004279 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4280 device->type = btrfs_device_type(leaf, dev_item);
4281 device->io_align = btrfs_device_io_align(leaf, dev_item);
4282 device->io_width = btrfs_device_io_width(leaf, dev_item);
4283 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004284
4285 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004286 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004287}
4288
Yan Zheng2b820322008-11-17 21:11:30 -05004289static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4290{
4291 struct btrfs_fs_devices *fs_devices;
4292 int ret;
4293
Li Zefanb367e472011-12-07 11:38:24 +08004294 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004295
4296 fs_devices = root->fs_info->fs_devices->seed;
4297 while (fs_devices) {
4298 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4299 ret = 0;
4300 goto out;
4301 }
4302 fs_devices = fs_devices->seed;
4303 }
4304
4305 fs_devices = find_fsid(fsid);
4306 if (!fs_devices) {
4307 ret = -ENOENT;
4308 goto out;
4309 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004310
4311 fs_devices = clone_fs_devices(fs_devices);
4312 if (IS_ERR(fs_devices)) {
4313 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004314 goto out;
4315 }
4316
Christoph Hellwig97288f22008-12-02 06:36:09 -05004317 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004318 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004319 if (ret)
4320 goto out;
4321
4322 if (!fs_devices->seeding) {
4323 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004324 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004325 ret = -EINVAL;
4326 goto out;
4327 }
4328
4329 fs_devices->seed = root->fs_info->fs_devices->seed;
4330 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004331out:
Yan Zheng2b820322008-11-17 21:11:30 -05004332 return ret;
4333}
4334
Chris Mason0d81ba52008-03-24 15:02:07 -04004335static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004336 struct extent_buffer *leaf,
4337 struct btrfs_dev_item *dev_item)
4338{
4339 struct btrfs_device *device;
4340 u64 devid;
4341 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004342 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004343 u8 dev_uuid[BTRFS_UUID_SIZE];
4344
Chris Mason0b86a832008-03-24 15:01:56 -04004345 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004346 read_extent_buffer(leaf, dev_uuid,
4347 (unsigned long)btrfs_device_uuid(dev_item),
4348 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004349 read_extent_buffer(leaf, fs_uuid,
4350 (unsigned long)btrfs_device_fsid(dev_item),
4351 BTRFS_UUID_SIZE);
4352
4353 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4354 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004355 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004356 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004357 }
4358
4359 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4360 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004361 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004362 return -EIO;
4363
4364 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004365 printk(KERN_WARNING "warning devid %llu missing\n",
4366 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004367 device = add_missing_dev(root, devid, dev_uuid);
4368 if (!device)
4369 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004370 } else if (!device->missing) {
4371 /*
4372 * this happens when a device that was properly setup
4373 * in the device info lists suddenly goes bad.
4374 * device->bdev is NULL, and so we have to set
4375 * device->missing to one here
4376 */
4377 root->fs_info->fs_devices->missing_devices++;
4378 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004379 }
4380 }
4381
4382 if (device->fs_devices != root->fs_info->fs_devices) {
4383 BUG_ON(device->writeable);
4384 if (device->generation !=
4385 btrfs_device_generation(leaf, dev_item))
4386 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004387 }
Chris Mason0b86a832008-03-24 15:01:56 -04004388
4389 fill_device_from_item(leaf, dev_item, device);
4390 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004391 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004392 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004393 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004394 spin_lock(&root->fs_info->free_chunk_lock);
4395 root->fs_info->free_chunk_space += device->total_bytes -
4396 device->bytes_used;
4397 spin_unlock(&root->fs_info->free_chunk_lock);
4398 }
Chris Mason0b86a832008-03-24 15:01:56 -04004399 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004400 return ret;
4401}
4402
Yan Zhenge4404d62008-12-12 10:03:26 -05004403int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004404{
David Sterba6c417612011-04-13 15:41:04 +02004405 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004406 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004407 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004408 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004409 u8 *ptr;
4410 unsigned long sb_ptr;
4411 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004412 u32 num_stripes;
4413 u32 array_size;
4414 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004415 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004416 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004417
Yan Zhenge4404d62008-12-12 10:03:26 -05004418 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004419 BTRFS_SUPER_INFO_SIZE);
4420 if (!sb)
4421 return -ENOMEM;
4422 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004423 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004424 /*
4425 * The sb extent buffer is artifical and just used to read the system array.
4426 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4427 * pages up-to-date when the page is larger: extent does not cover the
4428 * whole page and consequently check_page_uptodate does not find all
4429 * the page's extents up-to-date (the hole beyond sb),
4430 * write_extent_buffer then triggers a WARN_ON.
4431 *
4432 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4433 * but sb spans only this function. Add an explicit SetPageUptodate call
4434 * to silence the warning eg. on PowerPC 64.
4435 */
4436 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
4437 SetPageUptodate(sb->first_page);
Chris Mason4008c042009-02-12 14:09:45 -05004438
Chris Masona061fc82008-05-07 11:43:44 -04004439 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004440 array_size = btrfs_super_sys_array_size(super_copy);
4441
Chris Mason0b86a832008-03-24 15:01:56 -04004442 ptr = super_copy->sys_chunk_array;
4443 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4444 cur = 0;
4445
4446 while (cur < array_size) {
4447 disk_key = (struct btrfs_disk_key *)ptr;
4448 btrfs_disk_key_to_cpu(&key, disk_key);
4449
Chris Masona061fc82008-05-07 11:43:44 -04004450 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004451 sb_ptr += len;
4452 cur += len;
4453
Chris Mason0d81ba52008-03-24 15:02:07 -04004454 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004455 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004456 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004457 if (ret)
4458 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004459 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4460 len = btrfs_chunk_item_size(num_stripes);
4461 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004462 ret = -EIO;
4463 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004464 }
4465 ptr += len;
4466 sb_ptr += len;
4467 cur += len;
4468 }
Chris Masona061fc82008-05-07 11:43:44 -04004469 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004470 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004471}
4472
4473int btrfs_read_chunk_tree(struct btrfs_root *root)
4474{
4475 struct btrfs_path *path;
4476 struct extent_buffer *leaf;
4477 struct btrfs_key key;
4478 struct btrfs_key found_key;
4479 int ret;
4480 int slot;
4481
4482 root = root->fs_info->chunk_root;
4483
4484 path = btrfs_alloc_path();
4485 if (!path)
4486 return -ENOMEM;
4487
Li Zefanb367e472011-12-07 11:38:24 +08004488 mutex_lock(&uuid_mutex);
4489 lock_chunks(root);
4490
Chris Mason0b86a832008-03-24 15:01:56 -04004491 /* first we search for all of the device items, and then we
4492 * read in all of the chunk items. This way we can create chunk
4493 * mappings that reference all of the devices that are afound
4494 */
4495 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4496 key.offset = 0;
4497 key.type = 0;
4498again:
4499 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004500 if (ret < 0)
4501 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004502 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004503 leaf = path->nodes[0];
4504 slot = path->slots[0];
4505 if (slot >= btrfs_header_nritems(leaf)) {
4506 ret = btrfs_next_leaf(root, path);
4507 if (ret == 0)
4508 continue;
4509 if (ret < 0)
4510 goto error;
4511 break;
4512 }
4513 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4514 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4515 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4516 break;
4517 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4518 struct btrfs_dev_item *dev_item;
4519 dev_item = btrfs_item_ptr(leaf, slot,
4520 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004521 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004522 if (ret)
4523 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004524 }
4525 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4526 struct btrfs_chunk *chunk;
4527 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4528 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004529 if (ret)
4530 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004531 }
4532 path->slots[0]++;
4533 }
4534 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4535 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004536 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004537 goto again;
4538 }
Chris Mason0b86a832008-03-24 15:01:56 -04004539 ret = 0;
4540error:
Li Zefanb367e472011-12-07 11:38:24 +08004541 unlock_chunks(root);
4542 mutex_unlock(&uuid_mutex);
4543
Yan Zheng2b820322008-11-17 21:11:30 -05004544 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004545 return ret;
4546}