blob: 6e0e82a1b188aa4b8f24d4770b91261816e1065f [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>
Chris Mason593060d2008-03-25 16:50:33 -040026#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "ctree.h"
29#include "extent_map.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "print-tree.h"
33#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040034#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040035
Chris Mason593060d2008-03-25 16:50:33 -040036struct map_lookup {
37 u64 type;
38 int io_align;
39 int io_width;
40 int stripe_len;
41 int sector_size;
42 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040043 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040044 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040045};
46
Yan Zheng2b820322008-11-17 21:11:30 -050047static int init_first_rw_device(struct btrfs_trans_handle *trans,
48 struct btrfs_root *root,
49 struct btrfs_device *device);
50static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
51
Chris Mason593060d2008-03-25 16:50:33 -040052#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040053 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040054
Chris Mason8a4b83c2008-03-24 15:02:07 -040055static DEFINE_MUTEX(uuid_mutex);
56static LIST_HEAD(fs_uuids);
57
Chris Masona061fc82008-05-07 11:43:44 -040058void btrfs_lock_volumes(void)
59{
60 mutex_lock(&uuid_mutex);
61}
62
63void btrfs_unlock_volumes(void)
64{
65 mutex_unlock(&uuid_mutex);
66}
67
Chris Mason7d9eb122008-07-08 14:19:17 -040068static void lock_chunks(struct btrfs_root *root)
69{
Chris Mason7d9eb122008-07-08 14:19:17 -040070 mutex_lock(&root->fs_info->chunk_mutex);
71}
72
73static void unlock_chunks(struct btrfs_root *root)
74{
Chris Mason7d9eb122008-07-08 14:19:17 -040075 mutex_unlock(&root->fs_info->chunk_mutex);
76}
77
Yan Zhenge4404d62008-12-12 10:03:26 -050078static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
79{
80 struct btrfs_device *device;
81 WARN_ON(fs_devices->opened);
82 while (!list_empty(&fs_devices->devices)) {
83 device = list_entry(fs_devices->devices.next,
84 struct btrfs_device, dev_list);
85 list_del(&device->dev_list);
86 kfree(device->name);
87 kfree(device);
88 }
89 kfree(fs_devices);
90}
91
Chris Mason8a4b83c2008-03-24 15:02:07 -040092int btrfs_cleanup_fs_uuids(void)
93{
94 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040095
Yan Zheng2b820322008-11-17 21:11:30 -050096 while (!list_empty(&fs_uuids)) {
97 fs_devices = list_entry(fs_uuids.next,
98 struct btrfs_fs_devices, list);
99 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500100 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101 }
102 return 0;
103}
104
Chris Masona1b32a52008-09-05 16:09:51 -0400105static noinline struct btrfs_device *__find_device(struct list_head *head,
106 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400107{
108 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400109
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500110 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400111 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400112 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400114 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400115 }
116 return NULL;
117}
118
Chris Masona1b32a52008-09-05 16:09:51 -0400119static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400120{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400121 struct btrfs_fs_devices *fs_devices;
122
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500123 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400124 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
125 return fs_devices;
126 }
127 return NULL;
128}
129
Chris Masonffbd5172009-04-20 15:50:09 -0400130static void requeue_list(struct btrfs_pending_bios *pending_bios,
131 struct bio *head, struct bio *tail)
132{
133
134 struct bio *old_head;
135
136 old_head = pending_bios->head;
137 pending_bios->head = head;
138 if (pending_bios->tail)
139 tail->bi_next = old_head;
140 else
141 pending_bios->tail = tail;
142}
143
Chris Mason8b712842008-06-11 16:50:36 -0400144/*
145 * we try to collect pending bios for a device so we don't get a large
146 * number of procs sending bios down to the same device. This greatly
147 * improves the schedulers ability to collect and merge the bios.
148 *
149 * But, it also turns into a long list of bios to process and that is sure
150 * to eventually make the worker thread block. The solution here is to
151 * make some progress and then put this work struct back at the end of
152 * the list if the block device is congested. This way, multiple devices
153 * can make progress from a single worker thread.
154 */
Chris Masond3977122009-01-05 21:25:51 -0500155static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400156{
157 struct bio *pending;
158 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400159 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400160 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400161 struct bio *tail;
162 struct bio *cur;
163 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400164 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400165 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400166 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400167 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400168 int force_reg = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400169
Chris Masonbedf7622009-04-03 10:32:58 -0400170 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400171 fs_info = device->dev_root->fs_info;
172 limit = btrfs_async_submit_limit(fs_info);
173 limit = limit * 2 / 3;
174
Chris Mason8b712842008-06-11 16:50:36 -0400175loop:
176 spin_lock(&device->io_lock);
177
Chris Masona6837052009-02-04 09:19:41 -0500178loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400179 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400180
Chris Mason8b712842008-06-11 16:50:36 -0400181 /* take all the bios off the list at once and process them
182 * later on (without the lock held). But, remember the
183 * tail and other pointers so the bios can be properly reinserted
184 * into the list if we hit congestion
185 */
Chris Masond84275c2009-06-09 15:39:08 -0400186 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400187 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400188 force_reg = 1;
189 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400190 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400191 force_reg = 0;
192 }
Chris Masonffbd5172009-04-20 15:50:09 -0400193
194 pending = pending_bios->head;
195 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400196 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400197
198 /*
199 * if pending was null this time around, no bios need processing
200 * at all and we can stop. Otherwise it'll loop back up again
201 * and do an additional check so no bios are missed.
202 *
203 * device->running_pending is used to synchronize with the
204 * schedule_bio code.
205 */
Chris Masonffbd5172009-04-20 15:50:09 -0400206 if (device->pending_sync_bios.head == NULL &&
207 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400208 again = 0;
209 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400210 } else {
211 again = 1;
212 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400213 }
Chris Masonffbd5172009-04-20 15:50:09 -0400214
215 pending_bios->head = NULL;
216 pending_bios->tail = NULL;
217
Chris Mason8b712842008-06-11 16:50:36 -0400218 spin_unlock(&device->io_lock);
219
Chris Masond3977122009-01-05 21:25:51 -0500220 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400221
222 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400223 /* we want to work on both lists, but do more bios on the
224 * sync list than the regular list
225 */
226 if ((num_run > 32 &&
227 pending_bios != &device->pending_sync_bios &&
228 device->pending_sync_bios.head) ||
229 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
230 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400231 spin_lock(&device->io_lock);
232 requeue_list(pending_bios, pending, tail);
233 goto loop_lock;
234 }
235
Chris Mason8b712842008-06-11 16:50:36 -0400236 cur = pending;
237 pending = pending->bi_next;
238 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400239 atomic_dec(&fs_info->nr_async_bios);
240
241 if (atomic_read(&fs_info->nr_async_bios) < limit &&
242 waitqueue_active(&fs_info->async_submit_wait))
243 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400244
245 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400246
Chris Mason5ff7ba32010-03-15 10:21:30 -0400247 submit_bio(cur->bi_rw, cur);
248 num_run++;
249 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100250 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400251 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400252
253 /*
254 * we made progress, there is more work to do and the bdi
255 * is now congested. Back off and let other work structs
256 * run instead
257 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400258 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500259 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400260 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400261
Chris Masonb765ead2009-04-03 10:27:10 -0400262 ioc = current->io_context;
263
264 /*
265 * the main goal here is that we don't want to
266 * block if we're going to be able to submit
267 * more requests without blocking.
268 *
269 * This code does two great things, it pokes into
270 * the elevator code from a filesystem _and_
271 * it makes assumptions about how batching works.
272 */
273 if (ioc && ioc->nr_batch_requests > 0 &&
274 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
275 (last_waited == 0 ||
276 ioc->last_waited == last_waited)) {
277 /*
278 * we want to go through our batch of
279 * requests and stop. So, we copy out
280 * the ioc->last_waited time and test
281 * against it before looping
282 */
283 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100284 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400285 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400286 continue;
287 }
Chris Mason8b712842008-06-11 16:50:36 -0400288 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400289 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500290 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400291
292 spin_unlock(&device->io_lock);
293 btrfs_requeue_work(&device->work);
294 goto done;
295 }
296 }
Chris Masonffbd5172009-04-20 15:50:09 -0400297
Chris Mason51684082010-03-10 15:33:32 -0500298 cond_resched();
299 if (again)
300 goto loop;
301
302 spin_lock(&device->io_lock);
303 if (device->pending_bios.head || device->pending_sync_bios.head)
304 goto loop_lock;
305 spin_unlock(&device->io_lock);
306
Chris Mason8b712842008-06-11 16:50:36 -0400307done:
308 return 0;
309}
310
Christoph Hellwigb2950862008-12-02 09:54:17 -0500311static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400312{
313 struct btrfs_device *device;
314
315 device = container_of(work, struct btrfs_device, work);
316 run_scheduled_bios(device);
317}
318
Chris Masona1b32a52008-09-05 16:09:51 -0400319static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400320 struct btrfs_super_block *disk_super,
321 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
322{
323 struct btrfs_device *device;
324 struct btrfs_fs_devices *fs_devices;
325 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000326 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400327
328 fs_devices = find_fsid(disk_super->fsid);
329 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400330 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400331 if (!fs_devices)
332 return -ENOMEM;
333 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400334 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400335 list_add(&fs_devices->list, &fs_uuids);
336 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
337 fs_devices->latest_devid = devid;
338 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400339 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400340 device = NULL;
341 } else {
Chris Masona4437552008-04-18 10:29:38 -0400342 device = __find_device(&fs_devices->devices, devid,
343 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344 }
345 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500346 if (fs_devices->opened)
347 return -EBUSY;
348
Chris Mason8a4b83c2008-03-24 15:02:07 -0400349 device = kzalloc(sizeof(*device), GFP_NOFS);
350 if (!device) {
351 /* we can safely leave the fs_devices entry around */
352 return -ENOMEM;
353 }
354 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400355 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400356 memcpy(device->uuid, disk_super->dev_item.uuid,
357 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400358 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400359 device->name = kstrdup(path, GFP_NOFS);
360 if (!device->name) {
361 kfree(device);
362 return -ENOMEM;
363 }
Yan Zheng2b820322008-11-17 21:11:30 -0500364 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400365
366 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400367 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400368 mutex_unlock(&fs_devices->device_list_mutex);
369
Yan Zheng2b820322008-11-17 21:11:30 -0500370 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400371 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500372 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000373 name = kstrdup(path, GFP_NOFS);
374 if (!name)
375 return -ENOMEM;
376 kfree(device->name);
377 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500378 if (device->missing) {
379 fs_devices->missing_devices--;
380 device->missing = 0;
381 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400382 }
383
384 if (found_transid > fs_devices->latest_trans) {
385 fs_devices->latest_devid = devid;
386 fs_devices->latest_trans = found_transid;
387 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 *fs_devices_ret = fs_devices;
389 return 0;
390}
391
Yan Zhenge4404d62008-12-12 10:03:26 -0500392static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
393{
394 struct btrfs_fs_devices *fs_devices;
395 struct btrfs_device *device;
396 struct btrfs_device *orig_dev;
397
398 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
399 if (!fs_devices)
400 return ERR_PTR(-ENOMEM);
401
402 INIT_LIST_HEAD(&fs_devices->devices);
403 INIT_LIST_HEAD(&fs_devices->alloc_list);
404 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400405 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500406 fs_devices->latest_devid = orig->latest_devid;
407 fs_devices->latest_trans = orig->latest_trans;
408 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
409
Chris Masone5e9a522009-06-10 15:17:02 -0400410 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500411 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
412 device = kzalloc(sizeof(*device), GFP_NOFS);
413 if (!device)
414 goto error;
415
416 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400417 if (!device->name) {
418 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500419 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400420 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500421
422 device->devid = orig_dev->devid;
423 device->work.func = pending_bios_fn;
424 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500425 spin_lock_init(&device->io_lock);
426 INIT_LIST_HEAD(&device->dev_list);
427 INIT_LIST_HEAD(&device->dev_alloc_list);
428
429 list_add(&device->dev_list, &fs_devices->devices);
430 device->fs_devices = fs_devices;
431 fs_devices->num_devices++;
432 }
Chris Masone5e9a522009-06-10 15:17:02 -0400433 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500434 return fs_devices;
435error:
Chris Masone5e9a522009-06-10 15:17:02 -0400436 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500437 free_fs_devices(fs_devices);
438 return ERR_PTR(-ENOMEM);
439}
440
Chris Masondfe25022008-05-13 13:46:40 -0400441int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
442{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500443 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400444
445 mutex_lock(&uuid_mutex);
446again:
Chris Masone5e9a522009-06-10 15:17:02 -0400447 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500448 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500449 if (device->in_fs_metadata)
450 continue;
451
452 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100453 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500454 device->bdev = NULL;
455 fs_devices->open_devices--;
456 }
457 if (device->writeable) {
458 list_del_init(&device->dev_alloc_list);
459 device->writeable = 0;
460 fs_devices->rw_devices--;
461 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500462 list_del_init(&device->dev_list);
463 fs_devices->num_devices--;
464 kfree(device->name);
465 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400466 }
Chris Masone5e9a522009-06-10 15:17:02 -0400467 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500468
469 if (fs_devices->seed) {
470 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500471 goto again;
472 }
473
Chris Masondfe25022008-05-13 13:46:40 -0400474 mutex_unlock(&uuid_mutex);
475 return 0;
476}
Chris Masona0af4692008-05-13 16:03:06 -0400477
Yan Zheng2b820322008-11-17 21:11:30 -0500478static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400479{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400480 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500481
Yan Zheng2b820322008-11-17 21:11:30 -0500482 if (--fs_devices->opened > 0)
483 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400484
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500485 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400486 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100487 blkdev_put(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400488 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400489 }
Yan Zheng2b820322008-11-17 21:11:30 -0500490 if (device->writeable) {
491 list_del_init(&device->dev_alloc_list);
492 fs_devices->rw_devices--;
493 }
494
Chris Mason8a4b83c2008-03-24 15:02:07 -0400495 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500496 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400497 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500499 WARN_ON(fs_devices->open_devices);
500 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500501 fs_devices->opened = 0;
502 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500503
Chris Mason8a4b83c2008-03-24 15:02:07 -0400504 return 0;
505}
506
Yan Zheng2b820322008-11-17 21:11:30 -0500507int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
508{
Yan Zhenge4404d62008-12-12 10:03:26 -0500509 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500510 int ret;
511
512 mutex_lock(&uuid_mutex);
513 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500514 if (!fs_devices->opened) {
515 seed_devices = fs_devices->seed;
516 fs_devices->seed = NULL;
517 }
Yan Zheng2b820322008-11-17 21:11:30 -0500518 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500519
520 while (seed_devices) {
521 fs_devices = seed_devices;
522 seed_devices = fs_devices->seed;
523 __btrfs_close_devices(fs_devices);
524 free_fs_devices(fs_devices);
525 }
Yan Zheng2b820322008-11-17 21:11:30 -0500526 return ret;
527}
528
Yan Zhenge4404d62008-12-12 10:03:26 -0500529static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
530 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531{
532 struct block_device *bdev;
533 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400534 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400535 struct block_device *latest_bdev = NULL;
536 struct buffer_head *bh;
537 struct btrfs_super_block *disk_super;
538 u64 latest_devid = 0;
539 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400540 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500541 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400542 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400543
Tejun Heod4d77622010-11-13 11:55:18 +0100544 flags |= FMODE_EXCL;
545
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500546 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400547 if (device->bdev)
548 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400549 if (!device->name)
550 continue;
551
Tejun Heod4d77622010-11-13 11:55:18 +0100552 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400553 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500554 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400555 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400556 }
Chris Masona061fc82008-05-07 11:43:44 -0400557 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400558
Yan Zhenga512bbf2008-12-08 16:46:26 -0500559 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000560 if (!bh) {
561 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400562 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000563 }
Chris Masona0af4692008-05-13 16:03:06 -0400564
565 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000566 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400567 if (devid != device->devid)
568 goto error_brelse;
569
Yan Zheng2b820322008-11-17 21:11:30 -0500570 if (memcmp(device->uuid, disk_super->dev_item.uuid,
571 BTRFS_UUID_SIZE))
572 goto error_brelse;
573
574 device->generation = btrfs_super_generation(disk_super);
575 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400576 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500577 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400578 latest_bdev = bdev;
579 }
580
Yan Zheng2b820322008-11-17 21:11:30 -0500581 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
582 device->writeable = 0;
583 } else {
584 device->writeable = !bdev_read_only(bdev);
585 seeding = 0;
586 }
587
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400589 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500590 device->mode = flags;
591
Chris Masonc2898112009-06-10 09:51:32 -0400592 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
593 fs_devices->rotating = 1;
594
Chris Masona0af4692008-05-13 16:03:06 -0400595 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500596 if (device->writeable) {
597 fs_devices->rw_devices++;
598 list_add(&device->dev_alloc_list,
599 &fs_devices->alloc_list);
600 }
Chris Masona0af4692008-05-13 16:03:06 -0400601 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400602
Chris Masona0af4692008-05-13 16:03:06 -0400603error_brelse:
604 brelse(bh);
605error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100606 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400607error:
608 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400609 }
Chris Masona0af4692008-05-13 16:03:06 -0400610 if (fs_devices->open_devices == 0) {
611 ret = -EIO;
612 goto out;
613 }
Yan Zheng2b820322008-11-17 21:11:30 -0500614 fs_devices->seeding = seeding;
615 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400616 fs_devices->latest_bdev = latest_bdev;
617 fs_devices->latest_devid = latest_devid;
618 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500619 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400620out:
Yan Zheng2b820322008-11-17 21:11:30 -0500621 return ret;
622}
623
624int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500625 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500626{
627 int ret;
628
629 mutex_lock(&uuid_mutex);
630 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500631 fs_devices->opened++;
632 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500633 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500634 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500635 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400636 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400637 return ret;
638}
639
Christoph Hellwig97288f22008-12-02 06:36:09 -0500640int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400641 struct btrfs_fs_devices **fs_devices_ret)
642{
643 struct btrfs_super_block *disk_super;
644 struct block_device *bdev;
645 struct buffer_head *bh;
646 int ret;
647 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400648 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400649
650 mutex_lock(&uuid_mutex);
651
Tejun Heod4d77622010-11-13 11:55:18 +0100652 flags |= FMODE_EXCL;
653 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400654
655 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400656 ret = PTR_ERR(bdev);
657 goto error;
658 }
659
660 ret = set_blocksize(bdev, 4096);
661 if (ret)
662 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500663 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400664 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000665 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400666 goto error_close;
667 }
668 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000669 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400670 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400671 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500672 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400673 else {
674 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500675 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400676 *(unsigned long long *)disk_super->fsid,
677 *(unsigned long long *)(disk_super->fsid + 8));
678 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500679 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500680 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400681 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
682
Chris Mason8a4b83c2008-03-24 15:02:07 -0400683 brelse(bh);
684error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100685 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400686error:
687 mutex_unlock(&uuid_mutex);
688 return ret;
689}
Chris Mason0b86a832008-03-24 15:01:56 -0400690
Miao Xie6d07bce2011-01-05 10:07:31 +0000691/* helper to account the used device space in the range */
692int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
693 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400694{
695 struct btrfs_key key;
696 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000697 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500698 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000699 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400700 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000701 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400702 struct extent_buffer *l;
703
Miao Xie6d07bce2011-01-05 10:07:31 +0000704 *length = 0;
705
706 if (start >= device->total_bytes)
707 return 0;
708
Yan Zheng2b820322008-11-17 21:11:30 -0500709 path = btrfs_alloc_path();
710 if (!path)
711 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400712 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400713
Chris Mason0b86a832008-03-24 15:01:56 -0400714 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000715 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400716 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000717
718 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400719 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000720 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400721 if (ret > 0) {
722 ret = btrfs_previous_item(root, path, key.objectid, key.type);
723 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000724 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400725 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000726
Chris Mason0b86a832008-03-24 15:01:56 -0400727 while (1) {
728 l = path->nodes[0];
729 slot = path->slots[0];
730 if (slot >= btrfs_header_nritems(l)) {
731 ret = btrfs_next_leaf(root, path);
732 if (ret == 0)
733 continue;
734 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000735 goto out;
736
737 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400738 }
739 btrfs_item_key_to_cpu(l, &key, slot);
740
741 if (key.objectid < device->devid)
742 goto next;
743
744 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000745 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400746
Chris Masond3977122009-01-05 21:25:51 -0500747 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400748 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400749
Chris Mason0b86a832008-03-24 15:01:56 -0400750 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000751 extent_end = key.offset + btrfs_dev_extent_length(l,
752 dev_extent);
753 if (key.offset <= start && extent_end > end) {
754 *length = end - start + 1;
755 break;
756 } else if (key.offset <= start && extent_end > start)
757 *length += extent_end - start;
758 else if (key.offset > start && extent_end <= end)
759 *length += extent_end - key.offset;
760 else if (key.offset > start && key.offset <= end) {
761 *length += end - key.offset + 1;
762 break;
763 } else if (key.offset > end)
764 break;
765
766next:
767 path->slots[0]++;
768 }
769 ret = 0;
770out:
771 btrfs_free_path(path);
772 return ret;
773}
774
Chris Mason0b86a832008-03-24 15:01:56 -0400775/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000776 * find_free_dev_extent - find free space in the specified device
777 * @trans: transaction handler
778 * @device: the device which we search the free space in
779 * @num_bytes: the size of the free space that we need
780 * @start: store the start of the free space.
781 * @len: the size of the free space. that we find, or the size of the max
782 * free space if we don't find suitable free space
783 *
Chris Mason0b86a832008-03-24 15:01:56 -0400784 * this uses a pretty simple search, the expectation is that it is
785 * called very infrequently and that a given device has a small number
786 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000787 *
788 * @start is used to store the start of the free space if we find. But if we
789 * don't find suitable free space, it will be used to store the start position
790 * of the max free space.
791 *
792 * @len is used to store the size of the free space that we find.
793 * But if we don't find suitable free space, it is used to store the size of
794 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400795 */
796int find_free_dev_extent(struct btrfs_trans_handle *trans,
797 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000798 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400799{
800 struct btrfs_key key;
801 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000802 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400803 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000804 u64 hole_size;
805 u64 max_hole_start;
806 u64 max_hole_size;
807 u64 extent_end;
808 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400809 u64 search_end = device->total_bytes;
810 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000811 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400812 struct extent_buffer *l;
813
Chris Mason0b86a832008-03-24 15:01:56 -0400814 /* FIXME use last free of some kind */
815
816 /* we don't want to overwrite the superblock on the drive,
817 * so we make sure to start at an offset of at least 1MB
818 */
Miao Xie7bfc8372011-01-05 10:07:26 +0000819 search_start = 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400820
Miao Xie7bfc8372011-01-05 10:07:26 +0000821 if (root->fs_info->alloc_start + num_bytes <= search_end)
Chris Mason0b86a832008-03-24 15:01:56 -0400822 search_start = max(root->fs_info->alloc_start, search_start);
823
Miao Xie7bfc8372011-01-05 10:07:26 +0000824 max_hole_start = search_start;
825 max_hole_size = 0;
826
827 if (search_start >= search_end) {
828 ret = -ENOSPC;
829 goto error;
830 }
831
832 path = btrfs_alloc_path();
833 if (!path) {
834 ret = -ENOMEM;
835 goto error;
836 }
837 path->reada = 2;
838
Chris Mason0b86a832008-03-24 15:01:56 -0400839 key.objectid = device->devid;
840 key.offset = search_start;
841 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000842
Chris Mason0b86a832008-03-24 15:01:56 -0400843 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
844 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000845 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400846 if (ret > 0) {
847 ret = btrfs_previous_item(root, path, key.objectid, key.type);
848 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000849 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400850 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000851
Chris Mason0b86a832008-03-24 15:01:56 -0400852 while (1) {
853 l = path->nodes[0];
854 slot = path->slots[0];
855 if (slot >= btrfs_header_nritems(l)) {
856 ret = btrfs_next_leaf(root, path);
857 if (ret == 0)
858 continue;
859 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000860 goto out;
861
862 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400863 }
864 btrfs_item_key_to_cpu(l, &key, slot);
865
866 if (key.objectid < device->devid)
867 goto next;
868
869 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000870 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400871
Chris Mason0b86a832008-03-24 15:01:56 -0400872 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
873 goto next;
874
Miao Xie7bfc8372011-01-05 10:07:26 +0000875 if (key.offset > search_start) {
876 hole_size = key.offset - search_start;
877
878 if (hole_size > max_hole_size) {
879 max_hole_start = search_start;
880 max_hole_size = hole_size;
881 }
882
883 /*
884 * If this free space is greater than which we need,
885 * it must be the max free space that we have found
886 * until now, so max_hole_start must point to the start
887 * of this free space and the length of this free space
888 * is stored in max_hole_size. Thus, we return
889 * max_hole_start and max_hole_size and go back to the
890 * caller.
891 */
892 if (hole_size >= num_bytes) {
893 ret = 0;
894 goto out;
895 }
896 }
897
Chris Mason0b86a832008-03-24 15:01:56 -0400898 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000899 extent_end = key.offset + btrfs_dev_extent_length(l,
900 dev_extent);
901 if (extent_end > search_start)
902 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400903next:
904 path->slots[0]++;
905 cond_resched();
906 }
Chris Mason0b86a832008-03-24 15:01:56 -0400907
Miao Xie7bfc8372011-01-05 10:07:26 +0000908 hole_size = search_end- search_start;
909 if (hole_size > max_hole_size) {
910 max_hole_start = search_start;
911 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400912 }
Chris Mason0b86a832008-03-24 15:01:56 -0400913
Miao Xie7bfc8372011-01-05 10:07:26 +0000914 /* See above. */
915 if (hole_size < num_bytes)
916 ret = -ENOSPC;
917 else
918 ret = 0;
919
920out:
Yan Zheng2b820322008-11-17 21:11:30 -0500921 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000922error:
923 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000924 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000925 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400926 return ret;
927}
928
Christoph Hellwigb2950862008-12-02 09:54:17 -0500929static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400930 struct btrfs_device *device,
931 u64 start)
932{
933 int ret;
934 struct btrfs_path *path;
935 struct btrfs_root *root = device->dev_root;
936 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400937 struct btrfs_key found_key;
938 struct extent_buffer *leaf = NULL;
939 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400940
941 path = btrfs_alloc_path();
942 if (!path)
943 return -ENOMEM;
944
945 key.objectid = device->devid;
946 key.offset = start;
947 key.type = BTRFS_DEV_EXTENT_KEY;
948
949 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400950 if (ret > 0) {
951 ret = btrfs_previous_item(root, path, key.objectid,
952 BTRFS_DEV_EXTENT_KEY);
953 BUG_ON(ret);
954 leaf = path->nodes[0];
955 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
956 extent = btrfs_item_ptr(leaf, path->slots[0],
957 struct btrfs_dev_extent);
958 BUG_ON(found_key.offset > start || found_key.offset +
959 btrfs_dev_extent_length(leaf, extent) < start);
960 ret = 0;
961 } else if (ret == 0) {
962 leaf = path->nodes[0];
963 extent = btrfs_item_ptr(leaf, path->slots[0],
964 struct btrfs_dev_extent);
965 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400966 BUG_ON(ret);
967
Chris Masondfe25022008-05-13 13:46:40 -0400968 if (device->bytes_used > 0)
969 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400970 ret = btrfs_del_item(trans, root, path);
971 BUG_ON(ret);
972
973 btrfs_free_path(path);
974 return ret;
975}
976
Yan Zheng2b820322008-11-17 21:11:30 -0500977int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400978 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400979 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500980 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400981{
982 int ret;
983 struct btrfs_path *path;
984 struct btrfs_root *root = device->dev_root;
985 struct btrfs_dev_extent *extent;
986 struct extent_buffer *leaf;
987 struct btrfs_key key;
988
Chris Masondfe25022008-05-13 13:46:40 -0400989 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400990 path = btrfs_alloc_path();
991 if (!path)
992 return -ENOMEM;
993
Chris Mason0b86a832008-03-24 15:01:56 -0400994 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500995 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400996 key.type = BTRFS_DEV_EXTENT_KEY;
997 ret = btrfs_insert_empty_item(trans, root, path, &key,
998 sizeof(*extent));
999 BUG_ON(ret);
1000
1001 leaf = path->nodes[0];
1002 extent = btrfs_item_ptr(leaf, path->slots[0],
1003 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001004 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1005 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1006 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1007
1008 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1009 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1010 BTRFS_UUID_SIZE);
1011
Chris Mason0b86a832008-03-24 15:01:56 -04001012 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1013 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001014 btrfs_free_path(path);
1015 return ret;
1016}
1017
Chris Masona1b32a52008-09-05 16:09:51 -04001018static noinline int find_next_chunk(struct btrfs_root *root,
1019 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001020{
1021 struct btrfs_path *path;
1022 int ret;
1023 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001024 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001025 struct btrfs_key found_key;
1026
1027 path = btrfs_alloc_path();
1028 BUG_ON(!path);
1029
Chris Masone17cade2008-04-15 15:41:47 -04001030 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001031 key.offset = (u64)-1;
1032 key.type = BTRFS_CHUNK_ITEM_KEY;
1033
1034 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1035 if (ret < 0)
1036 goto error;
1037
1038 BUG_ON(ret == 0);
1039
1040 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1041 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001042 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001043 } else {
1044 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1045 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001046 if (found_key.objectid != objectid)
1047 *offset = 0;
1048 else {
1049 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1050 struct btrfs_chunk);
1051 *offset = found_key.offset +
1052 btrfs_chunk_length(path->nodes[0], chunk);
1053 }
Chris Mason0b86a832008-03-24 15:01:56 -04001054 }
1055 ret = 0;
1056error:
1057 btrfs_free_path(path);
1058 return ret;
1059}
1060
Yan Zheng2b820322008-11-17 21:11:30 -05001061static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001062{
1063 int ret;
1064 struct btrfs_key key;
1065 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001066 struct btrfs_path *path;
1067
1068 root = root->fs_info->chunk_root;
1069
1070 path = btrfs_alloc_path();
1071 if (!path)
1072 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001073
1074 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1075 key.type = BTRFS_DEV_ITEM_KEY;
1076 key.offset = (u64)-1;
1077
1078 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1079 if (ret < 0)
1080 goto error;
1081
1082 BUG_ON(ret == 0);
1083
1084 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1085 BTRFS_DEV_ITEM_KEY);
1086 if (ret) {
1087 *objectid = 1;
1088 } else {
1089 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1090 path->slots[0]);
1091 *objectid = found_key.offset + 1;
1092 }
1093 ret = 0;
1094error:
Yan Zheng2b820322008-11-17 21:11:30 -05001095 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001096 return ret;
1097}
1098
1099/*
1100 * the device information is stored in the chunk root
1101 * the btrfs_device struct should be fully filled in
1102 */
1103int btrfs_add_device(struct btrfs_trans_handle *trans,
1104 struct btrfs_root *root,
1105 struct btrfs_device *device)
1106{
1107 int ret;
1108 struct btrfs_path *path;
1109 struct btrfs_dev_item *dev_item;
1110 struct extent_buffer *leaf;
1111 struct btrfs_key key;
1112 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001113
1114 root = root->fs_info->chunk_root;
1115
1116 path = btrfs_alloc_path();
1117 if (!path)
1118 return -ENOMEM;
1119
Chris Mason0b86a832008-03-24 15:01:56 -04001120 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1121 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001122 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001123
1124 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001125 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001126 if (ret)
1127 goto out;
1128
1129 leaf = path->nodes[0];
1130 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1131
1132 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001133 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001134 btrfs_set_device_type(leaf, dev_item, device->type);
1135 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1136 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1137 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001138 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1139 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001140 btrfs_set_device_group(leaf, dev_item, 0);
1141 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1142 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001143 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001144
Chris Mason0b86a832008-03-24 15:01:56 -04001145 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001146 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001147 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1148 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001149 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001150
Yan Zheng2b820322008-11-17 21:11:30 -05001151 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001152out:
1153 btrfs_free_path(path);
1154 return ret;
1155}
Chris Mason8f18cf12008-04-25 16:53:30 -04001156
Chris Masona061fc82008-05-07 11:43:44 -04001157static int btrfs_rm_dev_item(struct btrfs_root *root,
1158 struct btrfs_device *device)
1159{
1160 int ret;
1161 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001162 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001163 struct btrfs_trans_handle *trans;
1164
1165 root = root->fs_info->chunk_root;
1166
1167 path = btrfs_alloc_path();
1168 if (!path)
1169 return -ENOMEM;
1170
Yan, Zhenga22285a2010-05-16 10:48:46 -04001171 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001172 if (IS_ERR(trans)) {
1173 btrfs_free_path(path);
1174 return PTR_ERR(trans);
1175 }
Chris Masona061fc82008-05-07 11:43:44 -04001176 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1177 key.type = BTRFS_DEV_ITEM_KEY;
1178 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001179 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001180
1181 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1182 if (ret < 0)
1183 goto out;
1184
1185 if (ret > 0) {
1186 ret = -ENOENT;
1187 goto out;
1188 }
1189
1190 ret = btrfs_del_item(trans, root, path);
1191 if (ret)
1192 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001193out:
1194 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001195 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001196 btrfs_commit_transaction(trans, root);
1197 return ret;
1198}
1199
1200int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1201{
1202 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001203 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001204 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001205 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001206 struct btrfs_super_block *disk_super;
1207 u64 all_avail;
1208 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001209 u64 num_devices;
1210 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001211 int ret = 0;
1212
Chris Masona061fc82008-05-07 11:43:44 -04001213 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001214 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001215
1216 all_avail = root->fs_info->avail_data_alloc_bits |
1217 root->fs_info->avail_system_alloc_bits |
1218 root->fs_info->avail_metadata_alloc_bits;
1219
1220 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001221 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001222 printk(KERN_ERR "btrfs: unable to go below four devices "
1223 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001224 ret = -EINVAL;
1225 goto out;
1226 }
1227
1228 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001229 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001230 printk(KERN_ERR "btrfs: unable to go below two "
1231 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001232 ret = -EINVAL;
1233 goto out;
1234 }
1235
Chris Masondfe25022008-05-13 13:46:40 -04001236 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001237 struct list_head *devices;
1238 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001239
Chris Masondfe25022008-05-13 13:46:40 -04001240 device = NULL;
1241 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001242 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001243 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001244 if (tmp->in_fs_metadata && !tmp->bdev) {
1245 device = tmp;
1246 break;
1247 }
1248 }
Chris Masone5e9a522009-06-10 15:17:02 -04001249 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001250 bdev = NULL;
1251 bh = NULL;
1252 disk_super = NULL;
1253 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001254 printk(KERN_ERR "btrfs: no missing devices found to "
1255 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001256 goto out;
1257 }
Chris Masondfe25022008-05-13 13:46:40 -04001258 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001259 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1260 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001261 if (IS_ERR(bdev)) {
1262 ret = PTR_ERR(bdev);
1263 goto out;
1264 }
1265
Yan Zheng2b820322008-11-17 21:11:30 -05001266 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001267 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001268 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001269 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001270 goto error_close;
1271 }
1272 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001273 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001274 dev_uuid = disk_super->dev_item.uuid;
1275 device = btrfs_find_device(root, devid, dev_uuid,
1276 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001277 if (!device) {
1278 ret = -ENOENT;
1279 goto error_brelse;
1280 }
Chris Masondfe25022008-05-13 13:46:40 -04001281 }
Yan Zheng2b820322008-11-17 21:11:30 -05001282
1283 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001284 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1285 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001286 ret = -EINVAL;
1287 goto error_brelse;
1288 }
1289
1290 if (device->writeable) {
1291 list_del_init(&device->dev_alloc_list);
1292 root->fs_info->fs_devices->rw_devices--;
1293 }
Chris Masona061fc82008-05-07 11:43:44 -04001294
1295 ret = btrfs_shrink_device(device, 0);
1296 if (ret)
1297 goto error_brelse;
1298
Chris Masona061fc82008-05-07 11:43:44 -04001299 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1300 if (ret)
1301 goto error_brelse;
1302
Yan Zheng2b820322008-11-17 21:11:30 -05001303 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001304
1305 /*
1306 * the device list mutex makes sure that we don't change
1307 * the device list while someone else is writing out all
1308 * the device supers.
1309 */
1310 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001311 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001312 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1313
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001315
Chris Masoncd02dca2010-12-13 14:56:23 -05001316 if (device->missing)
1317 root->fs_info->fs_devices->missing_devices--;
1318
Yan Zheng2b820322008-11-17 21:11:30 -05001319 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1320 struct btrfs_device, dev_list);
1321 if (device->bdev == root->fs_info->sb->s_bdev)
1322 root->fs_info->sb->s_bdev = next_device->bdev;
1323 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1324 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1325
Yan Zhenge4404d62008-12-12 10:03:26 -05001326 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001327 blkdev_put(device->bdev, device->mode);
Yan Zhenge4404d62008-12-12 10:03:26 -05001328 device->bdev = NULL;
1329 device->fs_devices->open_devices--;
1330 }
1331
Yan Zheng2b820322008-11-17 21:11:30 -05001332 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1333 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1334
Yan Zhenge4404d62008-12-12 10:03:26 -05001335 if (device->fs_devices->open_devices == 0) {
1336 struct btrfs_fs_devices *fs_devices;
1337 fs_devices = root->fs_info->fs_devices;
1338 while (fs_devices) {
1339 if (fs_devices->seed == device->fs_devices)
1340 break;
1341 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001342 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001343 fs_devices->seed = device->fs_devices->seed;
1344 device->fs_devices->seed = NULL;
1345 __btrfs_close_devices(device->fs_devices);
1346 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001347 }
1348
1349 /*
1350 * at this point, the device is zero sized. We want to
1351 * remove it from the devices list and zero out the old super
1352 */
1353 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001354 /* make sure this device isn't detected as part of
1355 * the FS anymore
1356 */
1357 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1358 set_buffer_dirty(bh);
1359 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001360 }
Chris Masona061fc82008-05-07 11:43:44 -04001361
Chris Masona061fc82008-05-07 11:43:44 -04001362 kfree(device->name);
1363 kfree(device);
1364 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001365
1366error_brelse:
1367 brelse(bh);
1368error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001369 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001370 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001371out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001372 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001373 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001374 return ret;
1375}
1376
Yan Zheng2b820322008-11-17 21:11:30 -05001377/*
1378 * does all the dirty work required for changing file system's UUID.
1379 */
1380static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1381 struct btrfs_root *root)
1382{
1383 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1384 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001385 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001386 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1387 struct btrfs_device *device;
1388 u64 super_flags;
1389
1390 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001391 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001392 return -EINVAL;
1393
Yan Zhenge4404d62008-12-12 10:03:26 -05001394 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1395 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001396 return -ENOMEM;
1397
Yan Zhenge4404d62008-12-12 10:03:26 -05001398 old_devices = clone_fs_devices(fs_devices);
1399 if (IS_ERR(old_devices)) {
1400 kfree(seed_devices);
1401 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001402 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001403
Yan Zheng2b820322008-11-17 21:11:30 -05001404 list_add(&old_devices->list, &fs_uuids);
1405
Yan Zhenge4404d62008-12-12 10:03:26 -05001406 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1407 seed_devices->opened = 1;
1408 INIT_LIST_HEAD(&seed_devices->devices);
1409 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001410 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001411 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1412 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1413 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1414 device->fs_devices = seed_devices;
1415 }
1416
Yan Zheng2b820322008-11-17 21:11:30 -05001417 fs_devices->seeding = 0;
1418 fs_devices->num_devices = 0;
1419 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001420 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001421
1422 generate_random_uuid(fs_devices->fsid);
1423 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1424 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1425 super_flags = btrfs_super_flags(disk_super) &
1426 ~BTRFS_SUPER_FLAG_SEEDING;
1427 btrfs_set_super_flags(disk_super, super_flags);
1428
1429 return 0;
1430}
1431
1432/*
1433 * strore the expected generation for seed devices in device items.
1434 */
1435static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1436 struct btrfs_root *root)
1437{
1438 struct btrfs_path *path;
1439 struct extent_buffer *leaf;
1440 struct btrfs_dev_item *dev_item;
1441 struct btrfs_device *device;
1442 struct btrfs_key key;
1443 u8 fs_uuid[BTRFS_UUID_SIZE];
1444 u8 dev_uuid[BTRFS_UUID_SIZE];
1445 u64 devid;
1446 int ret;
1447
1448 path = btrfs_alloc_path();
1449 if (!path)
1450 return -ENOMEM;
1451
1452 root = root->fs_info->chunk_root;
1453 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1454 key.offset = 0;
1455 key.type = BTRFS_DEV_ITEM_KEY;
1456
1457 while (1) {
1458 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1459 if (ret < 0)
1460 goto error;
1461
1462 leaf = path->nodes[0];
1463next_slot:
1464 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1465 ret = btrfs_next_leaf(root, path);
1466 if (ret > 0)
1467 break;
1468 if (ret < 0)
1469 goto error;
1470 leaf = path->nodes[0];
1471 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1472 btrfs_release_path(root, path);
1473 continue;
1474 }
1475
1476 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1477 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1478 key.type != BTRFS_DEV_ITEM_KEY)
1479 break;
1480
1481 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1482 struct btrfs_dev_item);
1483 devid = btrfs_device_id(leaf, dev_item);
1484 read_extent_buffer(leaf, dev_uuid,
1485 (unsigned long)btrfs_device_uuid(dev_item),
1486 BTRFS_UUID_SIZE);
1487 read_extent_buffer(leaf, fs_uuid,
1488 (unsigned long)btrfs_device_fsid(dev_item),
1489 BTRFS_UUID_SIZE);
1490 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1491 BUG_ON(!device);
1492
1493 if (device->fs_devices->seeding) {
1494 btrfs_set_device_generation(leaf, dev_item,
1495 device->generation);
1496 btrfs_mark_buffer_dirty(leaf);
1497 }
1498
1499 path->slots[0]++;
1500 goto next_slot;
1501 }
1502 ret = 0;
1503error:
1504 btrfs_free_path(path);
1505 return ret;
1506}
1507
Chris Mason788f20e2008-04-28 15:29:42 -04001508int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1509{
1510 struct btrfs_trans_handle *trans;
1511 struct btrfs_device *device;
1512 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001513 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001514 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001515 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001516 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001517 int ret = 0;
1518
Yan Zheng2b820322008-11-17 21:11:30 -05001519 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1520 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001521
Tejun Heod4d77622010-11-13 11:55:18 +01001522 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1523 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001524 if (IS_ERR(bdev))
1525 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001526
Yan Zheng2b820322008-11-17 21:11:30 -05001527 if (root->fs_info->fs_devices->seeding) {
1528 seeding_dev = 1;
1529 down_write(&sb->s_umount);
1530 mutex_lock(&uuid_mutex);
1531 }
1532
Chris Mason8c8bee12008-09-29 11:19:10 -04001533 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001534 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001535
Chris Mason788f20e2008-04-28 15:29:42 -04001536 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001537 /*
1538 * we have the volume lock, so we don't need the extra
1539 * device list mutex while reading the list here.
1540 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001541 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001542 if (device->bdev == bdev) {
1543 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001544 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001545 }
1546 }
1547
1548 device = kzalloc(sizeof(*device), GFP_NOFS);
1549 if (!device) {
1550 /* we can safely leave the fs_devices entry around */
1551 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001552 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001553 }
1554
Chris Mason788f20e2008-04-28 15:29:42 -04001555 device->name = kstrdup(device_path, GFP_NOFS);
1556 if (!device->name) {
1557 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001558 ret = -ENOMEM;
1559 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001560 }
Yan Zheng2b820322008-11-17 21:11:30 -05001561
1562 ret = find_next_devid(root, &device->devid);
1563 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001564 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001565 kfree(device);
1566 goto error;
1567 }
1568
Yan, Zhenga22285a2010-05-16 10:48:46 -04001569 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001570 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001571 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001572 kfree(device);
1573 ret = PTR_ERR(trans);
1574 goto error;
1575 }
1576
Yan Zheng2b820322008-11-17 21:11:30 -05001577 lock_chunks(root);
1578
Yan Zheng2b820322008-11-17 21:11:30 -05001579 device->writeable = 1;
1580 device->work.func = pending_bios_fn;
1581 generate_random_uuid(device->uuid);
1582 spin_lock_init(&device->io_lock);
1583 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001584 device->io_width = root->sectorsize;
1585 device->io_align = root->sectorsize;
1586 device->sector_size = root->sectorsize;
1587 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001588 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001589 device->dev_root = root->fs_info->dev_root;
1590 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001591 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001592 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001593 set_blocksize(device->bdev, 4096);
1594
Yan Zheng2b820322008-11-17 21:11:30 -05001595 if (seeding_dev) {
1596 sb->s_flags &= ~MS_RDONLY;
1597 ret = btrfs_prepare_sprout(trans, root);
1598 BUG_ON(ret);
1599 }
1600
1601 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001602
1603 /*
1604 * we don't want write_supers to jump in here with our device
1605 * half setup
1606 */
1607 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001608 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1609 list_add(&device->dev_alloc_list,
1610 &root->fs_info->fs_devices->alloc_list);
1611 root->fs_info->fs_devices->num_devices++;
1612 root->fs_info->fs_devices->open_devices++;
1613 root->fs_info->fs_devices->rw_devices++;
1614 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1615
Chris Masonc2898112009-06-10 09:51:32 -04001616 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1617 root->fs_info->fs_devices->rotating = 1;
1618
Chris Mason788f20e2008-04-28 15:29:42 -04001619 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1620 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1621 total_bytes + device->total_bytes);
1622
1623 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1624 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1625 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001626 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001627
Yan Zheng2b820322008-11-17 21:11:30 -05001628 if (seeding_dev) {
1629 ret = init_first_rw_device(trans, root, device);
1630 BUG_ON(ret);
1631 ret = btrfs_finish_sprout(trans, root);
1632 BUG_ON(ret);
1633 } else {
1634 ret = btrfs_add_device(trans, root, device);
1635 }
1636
Chris Mason913d9522009-03-10 13:17:18 -04001637 /*
1638 * we've got more storage, clear any full flags on the space
1639 * infos
1640 */
1641 btrfs_clear_space_info_full(root->fs_info);
1642
Chris Mason7d9eb122008-07-08 14:19:17 -04001643 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001644 btrfs_commit_transaction(trans, root);
1645
1646 if (seeding_dev) {
1647 mutex_unlock(&uuid_mutex);
1648 up_write(&sb->s_umount);
1649
1650 ret = btrfs_relocate_sys_chunks(root);
1651 BUG_ON(ret);
1652 }
1653out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001654 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001655 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001656error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001657 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001658 if (seeding_dev) {
1659 mutex_unlock(&uuid_mutex);
1660 up_write(&sb->s_umount);
1661 }
Chris Mason788f20e2008-04-28 15:29:42 -04001662 goto out;
1663}
1664
Chris Masond3977122009-01-05 21:25:51 -05001665static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1666 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001667{
1668 int ret;
1669 struct btrfs_path *path;
1670 struct btrfs_root *root;
1671 struct btrfs_dev_item *dev_item;
1672 struct extent_buffer *leaf;
1673 struct btrfs_key key;
1674
1675 root = device->dev_root->fs_info->chunk_root;
1676
1677 path = btrfs_alloc_path();
1678 if (!path)
1679 return -ENOMEM;
1680
1681 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1682 key.type = BTRFS_DEV_ITEM_KEY;
1683 key.offset = device->devid;
1684
1685 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1686 if (ret < 0)
1687 goto out;
1688
1689 if (ret > 0) {
1690 ret = -ENOENT;
1691 goto out;
1692 }
1693
1694 leaf = path->nodes[0];
1695 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1696
1697 btrfs_set_device_id(leaf, dev_item, device->devid);
1698 btrfs_set_device_type(leaf, dev_item, device->type);
1699 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1700 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1701 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001702 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001703 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1704 btrfs_mark_buffer_dirty(leaf);
1705
1706out:
1707 btrfs_free_path(path);
1708 return ret;
1709}
1710
Chris Mason7d9eb122008-07-08 14:19:17 -04001711static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001712 struct btrfs_device *device, u64 new_size)
1713{
1714 struct btrfs_super_block *super_copy =
1715 &device->dev_root->fs_info->super_copy;
1716 u64 old_total = btrfs_super_total_bytes(super_copy);
1717 u64 diff = new_size - device->total_bytes;
1718
Yan Zheng2b820322008-11-17 21:11:30 -05001719 if (!device->writeable)
1720 return -EACCES;
1721 if (new_size <= device->total_bytes)
1722 return -EINVAL;
1723
Chris Mason8f18cf12008-04-25 16:53:30 -04001724 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001725 device->fs_devices->total_rw_bytes += diff;
1726
1727 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001728 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001729 btrfs_clear_space_info_full(device->dev_root->fs_info);
1730
Chris Mason8f18cf12008-04-25 16:53:30 -04001731 return btrfs_update_device(trans, device);
1732}
1733
Chris Mason7d9eb122008-07-08 14:19:17 -04001734int btrfs_grow_device(struct btrfs_trans_handle *trans,
1735 struct btrfs_device *device, u64 new_size)
1736{
1737 int ret;
1738 lock_chunks(device->dev_root);
1739 ret = __btrfs_grow_device(trans, device, new_size);
1740 unlock_chunks(device->dev_root);
1741 return ret;
1742}
1743
Chris Mason8f18cf12008-04-25 16:53:30 -04001744static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1745 struct btrfs_root *root,
1746 u64 chunk_tree, u64 chunk_objectid,
1747 u64 chunk_offset)
1748{
1749 int ret;
1750 struct btrfs_path *path;
1751 struct btrfs_key key;
1752
1753 root = root->fs_info->chunk_root;
1754 path = btrfs_alloc_path();
1755 if (!path)
1756 return -ENOMEM;
1757
1758 key.objectid = chunk_objectid;
1759 key.offset = chunk_offset;
1760 key.type = BTRFS_CHUNK_ITEM_KEY;
1761
1762 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1763 BUG_ON(ret);
1764
1765 ret = btrfs_del_item(trans, root, path);
1766 BUG_ON(ret);
1767
1768 btrfs_free_path(path);
1769 return 0;
1770}
1771
Christoph Hellwigb2950862008-12-02 09:54:17 -05001772static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001773 chunk_offset)
1774{
1775 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1776 struct btrfs_disk_key *disk_key;
1777 struct btrfs_chunk *chunk;
1778 u8 *ptr;
1779 int ret = 0;
1780 u32 num_stripes;
1781 u32 array_size;
1782 u32 len = 0;
1783 u32 cur;
1784 struct btrfs_key key;
1785
1786 array_size = btrfs_super_sys_array_size(super_copy);
1787
1788 ptr = super_copy->sys_chunk_array;
1789 cur = 0;
1790
1791 while (cur < array_size) {
1792 disk_key = (struct btrfs_disk_key *)ptr;
1793 btrfs_disk_key_to_cpu(&key, disk_key);
1794
1795 len = sizeof(*disk_key);
1796
1797 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1798 chunk = (struct btrfs_chunk *)(ptr + len);
1799 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1800 len += btrfs_chunk_item_size(num_stripes);
1801 } else {
1802 ret = -EIO;
1803 break;
1804 }
1805 if (key.objectid == chunk_objectid &&
1806 key.offset == chunk_offset) {
1807 memmove(ptr, ptr + len, array_size - (cur + len));
1808 array_size -= len;
1809 btrfs_set_super_sys_array_size(super_copy, array_size);
1810 } else {
1811 ptr += len;
1812 cur += len;
1813 }
1814 }
1815 return ret;
1816}
1817
Christoph Hellwigb2950862008-12-02 09:54:17 -05001818static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001819 u64 chunk_tree, u64 chunk_objectid,
1820 u64 chunk_offset)
1821{
1822 struct extent_map_tree *em_tree;
1823 struct btrfs_root *extent_root;
1824 struct btrfs_trans_handle *trans;
1825 struct extent_map *em;
1826 struct map_lookup *map;
1827 int ret;
1828 int i;
1829
1830 root = root->fs_info->chunk_root;
1831 extent_root = root->fs_info->extent_root;
1832 em_tree = &root->fs_info->mapping_tree.map_tree;
1833
Josef Bacikba1bf482009-09-11 16:11:19 -04001834 ret = btrfs_can_relocate(extent_root, chunk_offset);
1835 if (ret)
1836 return -ENOSPC;
1837
Chris Mason8f18cf12008-04-25 16:53:30 -04001838 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001839 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001840 if (ret)
1841 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001842
Yan, Zhenga22285a2010-05-16 10:48:46 -04001843 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001844 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001845
Chris Mason7d9eb122008-07-08 14:19:17 -04001846 lock_chunks(root);
1847
Chris Mason8f18cf12008-04-25 16:53:30 -04001848 /*
1849 * step two, delete the device extents and the
1850 * chunk tree entries
1851 */
Chris Mason890871b2009-09-02 16:24:52 -04001852 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001853 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001854 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001855
Chris Masona061fc82008-05-07 11:43:44 -04001856 BUG_ON(em->start > chunk_offset ||
1857 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001858 map = (struct map_lookup *)em->bdev;
1859
1860 for (i = 0; i < map->num_stripes; i++) {
1861 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1862 map->stripes[i].physical);
1863 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001864
Chris Masondfe25022008-05-13 13:46:40 -04001865 if (map->stripes[i].dev) {
1866 ret = btrfs_update_device(trans, map->stripes[i].dev);
1867 BUG_ON(ret);
1868 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001869 }
1870 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1871 chunk_offset);
1872
1873 BUG_ON(ret);
1874
1875 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1876 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1877 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001878 }
1879
Zheng Yan1a40e232008-09-26 10:09:34 -04001880 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1881 BUG_ON(ret);
1882
Chris Mason890871b2009-09-02 16:24:52 -04001883 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001884 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001885 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001886
Chris Mason8f18cf12008-04-25 16:53:30 -04001887 kfree(map);
1888 em->bdev = NULL;
1889
1890 /* once for the tree */
1891 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001892 /* once for us */
1893 free_extent_map(em);
1894
Chris Mason7d9eb122008-07-08 14:19:17 -04001895 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001896 btrfs_end_transaction(trans, root);
1897 return 0;
1898}
1899
Yan Zheng2b820322008-11-17 21:11:30 -05001900static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1901{
1902 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1903 struct btrfs_path *path;
1904 struct extent_buffer *leaf;
1905 struct btrfs_chunk *chunk;
1906 struct btrfs_key key;
1907 struct btrfs_key found_key;
1908 u64 chunk_tree = chunk_root->root_key.objectid;
1909 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001910 bool retried = false;
1911 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001912 int ret;
1913
1914 path = btrfs_alloc_path();
1915 if (!path)
1916 return -ENOMEM;
1917
Josef Bacikba1bf482009-09-11 16:11:19 -04001918again:
Yan Zheng2b820322008-11-17 21:11:30 -05001919 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1920 key.offset = (u64)-1;
1921 key.type = BTRFS_CHUNK_ITEM_KEY;
1922
1923 while (1) {
1924 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1925 if (ret < 0)
1926 goto error;
1927 BUG_ON(ret == 0);
1928
1929 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1930 key.type);
1931 if (ret < 0)
1932 goto error;
1933 if (ret > 0)
1934 break;
1935
1936 leaf = path->nodes[0];
1937 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1938
1939 chunk = btrfs_item_ptr(leaf, path->slots[0],
1940 struct btrfs_chunk);
1941 chunk_type = btrfs_chunk_type(leaf, chunk);
1942 btrfs_release_path(chunk_root, path);
1943
1944 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1945 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1946 found_key.objectid,
1947 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001948 if (ret == -ENOSPC)
1949 failed++;
1950 else if (ret)
1951 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001952 }
1953
1954 if (found_key.offset == 0)
1955 break;
1956 key.offset = found_key.offset - 1;
1957 }
1958 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001959 if (failed && !retried) {
1960 failed = 0;
1961 retried = true;
1962 goto again;
1963 } else if (failed && retried) {
1964 WARN_ON(1);
1965 ret = -ENOSPC;
1966 }
Yan Zheng2b820322008-11-17 21:11:30 -05001967error:
1968 btrfs_free_path(path);
1969 return ret;
1970}
1971
Chris Masonec44a352008-04-28 15:29:52 -04001972static u64 div_factor(u64 num, int factor)
1973{
1974 if (factor == 10)
1975 return num;
1976 num *= factor;
1977 do_div(num, 10);
1978 return num;
1979}
1980
Chris Masonec44a352008-04-28 15:29:52 -04001981int btrfs_balance(struct btrfs_root *dev_root)
1982{
1983 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001984 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1985 struct btrfs_device *device;
1986 u64 old_size;
1987 u64 size_to_free;
1988 struct btrfs_path *path;
1989 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04001990 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1991 struct btrfs_trans_handle *trans;
1992 struct btrfs_key found_key;
1993
Yan Zheng2b820322008-11-17 21:11:30 -05001994 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1995 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001996
Ben Hutchings6f88a442010-12-29 14:55:03 +00001997 if (!capable(CAP_SYS_ADMIN))
1998 return -EPERM;
1999
Chris Mason7d9eb122008-07-08 14:19:17 -04002000 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002001 dev_root = dev_root->fs_info->dev_root;
2002
Chris Masonec44a352008-04-28 15:29:52 -04002003 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002004 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002005 old_size = device->total_bytes;
2006 size_to_free = div_factor(old_size, 1);
2007 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002008 if (!device->writeable ||
2009 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002010 continue;
2011
2012 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002013 if (ret == -ENOSPC)
2014 break;
Chris Masonec44a352008-04-28 15:29:52 -04002015 BUG_ON(ret);
2016
Yan, Zhenga22285a2010-05-16 10:48:46 -04002017 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002018 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002019
2020 ret = btrfs_grow_device(trans, device, old_size);
2021 BUG_ON(ret);
2022
2023 btrfs_end_transaction(trans, dev_root);
2024 }
2025
2026 /* step two, relocate all the chunks */
2027 path = btrfs_alloc_path();
2028 BUG_ON(!path);
2029
2030 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2031 key.offset = (u64)-1;
2032 key.type = BTRFS_CHUNK_ITEM_KEY;
2033
Chris Masond3977122009-01-05 21:25:51 -05002034 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002035 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2036 if (ret < 0)
2037 goto error;
2038
2039 /*
2040 * this shouldn't happen, it means the last relocate
2041 * failed
2042 */
2043 if (ret == 0)
2044 break;
2045
2046 ret = btrfs_previous_item(chunk_root, path, 0,
2047 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002048 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002049 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002050
Chris Masonec44a352008-04-28 15:29:52 -04002051 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2052 path->slots[0]);
2053 if (found_key.objectid != key.objectid)
2054 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002055
Chris Masonec44a352008-04-28 15:29:52 -04002056 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002057 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002058 break;
2059
Chris Mason7d9eb122008-07-08 14:19:17 -04002060 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002061 ret = btrfs_relocate_chunk(chunk_root,
2062 chunk_root->root_key.objectid,
2063 found_key.objectid,
2064 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002065 BUG_ON(ret && ret != -ENOSPC);
2066 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002067 }
2068 ret = 0;
2069error:
2070 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002071 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002072 return ret;
2073}
2074
Chris Mason8f18cf12008-04-25 16:53:30 -04002075/*
2076 * shrinking a device means finding all of the device extents past
2077 * the new size, and then following the back refs to the chunks.
2078 * The chunk relocation code actually frees the device extent
2079 */
2080int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2081{
2082 struct btrfs_trans_handle *trans;
2083 struct btrfs_root *root = device->dev_root;
2084 struct btrfs_dev_extent *dev_extent = NULL;
2085 struct btrfs_path *path;
2086 u64 length;
2087 u64 chunk_tree;
2088 u64 chunk_objectid;
2089 u64 chunk_offset;
2090 int ret;
2091 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002092 int failed = 0;
2093 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002094 struct extent_buffer *l;
2095 struct btrfs_key key;
2096 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2097 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002098 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002099 u64 diff = device->total_bytes - new_size;
2100
Yan Zheng2b820322008-11-17 21:11:30 -05002101 if (new_size >= device->total_bytes)
2102 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002103
2104 path = btrfs_alloc_path();
2105 if (!path)
2106 return -ENOMEM;
2107
Chris Mason8f18cf12008-04-25 16:53:30 -04002108 path->reada = 2;
2109
Chris Mason7d9eb122008-07-08 14:19:17 -04002110 lock_chunks(root);
2111
Chris Mason8f18cf12008-04-25 16:53:30 -04002112 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002113 if (device->writeable)
2114 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002115 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002116
Josef Bacikba1bf482009-09-11 16:11:19 -04002117again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002118 key.objectid = device->devid;
2119 key.offset = (u64)-1;
2120 key.type = BTRFS_DEV_EXTENT_KEY;
2121
2122 while (1) {
2123 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2124 if (ret < 0)
2125 goto done;
2126
2127 ret = btrfs_previous_item(root, path, 0, key.type);
2128 if (ret < 0)
2129 goto done;
2130 if (ret) {
2131 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002132 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002133 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002134 }
2135
2136 l = path->nodes[0];
2137 slot = path->slots[0];
2138 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2139
Josef Bacikba1bf482009-09-11 16:11:19 -04002140 if (key.objectid != device->devid) {
2141 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002142 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002143 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002144
2145 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2146 length = btrfs_dev_extent_length(l, dev_extent);
2147
Josef Bacikba1bf482009-09-11 16:11:19 -04002148 if (key.offset + length <= new_size) {
2149 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002150 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002151 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002152
2153 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2154 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2155 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2156 btrfs_release_path(root, path);
2157
2158 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2159 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002160 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002161 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002162 if (ret == -ENOSPC)
2163 failed++;
2164 key.offset -= 1;
2165 }
2166
2167 if (failed && !retried) {
2168 failed = 0;
2169 retried = true;
2170 goto again;
2171 } else if (failed && retried) {
2172 ret = -ENOSPC;
2173 lock_chunks(root);
2174
2175 device->total_bytes = old_size;
2176 if (device->writeable)
2177 device->fs_devices->total_rw_bytes += diff;
2178 unlock_chunks(root);
2179 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002180 }
2181
Chris Balld6397ba2009-04-27 07:29:03 -04002182 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002183 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002184 if (IS_ERR(trans)) {
2185 ret = PTR_ERR(trans);
2186 goto done;
2187 }
2188
Chris Balld6397ba2009-04-27 07:29:03 -04002189 lock_chunks(root);
2190
2191 device->disk_total_bytes = new_size;
2192 /* Now btrfs_update_device() will change the on-disk size. */
2193 ret = btrfs_update_device(trans, device);
2194 if (ret) {
2195 unlock_chunks(root);
2196 btrfs_end_transaction(trans, root);
2197 goto done;
2198 }
2199 WARN_ON(diff > old_total);
2200 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2201 unlock_chunks(root);
2202 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002203done:
2204 btrfs_free_path(path);
2205 return ret;
2206}
2207
Christoph Hellwigb2950862008-12-02 09:54:17 -05002208static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002209 struct btrfs_root *root,
2210 struct btrfs_key *key,
2211 struct btrfs_chunk *chunk, int item_size)
2212{
2213 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2214 struct btrfs_disk_key disk_key;
2215 u32 array_size;
2216 u8 *ptr;
2217
2218 array_size = btrfs_super_sys_array_size(super_copy);
2219 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2220 return -EFBIG;
2221
2222 ptr = super_copy->sys_chunk_array + array_size;
2223 btrfs_cpu_key_to_disk(&disk_key, key);
2224 memcpy(ptr, &disk_key, sizeof(disk_key));
2225 ptr += sizeof(disk_key);
2226 memcpy(ptr, chunk, item_size);
2227 item_size += sizeof(disk_key);
2228 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2229 return 0;
2230}
2231
Chris Masond3977122009-01-05 21:25:51 -05002232static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002233 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002234{
2235 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2236 return calc_size;
2237 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2238 return calc_size * (num_stripes / sub_stripes);
2239 else
2240 return calc_size * num_stripes;
2241}
2242
Miao Xieb2117a32011-01-05 10:07:28 +00002243/* Used to sort the devices by max_avail(descending sort) */
2244int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
Chris Mason0b86a832008-03-24 15:01:56 -04002245{
Miao Xieb2117a32011-01-05 10:07:28 +00002246 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2247 ((struct btrfs_device_info *)dev_info2)->max_avail)
2248 return -1;
2249 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2250 ((struct btrfs_device_info *)dev_info2)->max_avail)
2251 return 1;
2252 else
2253 return 0;
2254}
Chris Mason0b86a832008-03-24 15:01:56 -04002255
Miao Xieb2117a32011-01-05 10:07:28 +00002256static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2257 int *num_stripes, int *min_stripes,
2258 int *sub_stripes)
2259{
2260 *num_stripes = 1;
2261 *min_stripes = 1;
2262 *sub_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002263
Chris Masona40a90a2008-04-18 11:55:51 -04002264 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002265 *num_stripes = fs_devices->rw_devices;
2266 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002267 }
2268 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002269 *num_stripes = 2;
2270 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002271 }
Chris Mason8790d502008-04-03 16:29:03 -04002272 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002273 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002274 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002275 *num_stripes = 2;
2276 *min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002277 }
Chris Mason321aecc2008-04-16 10:49:51 -04002278 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002279 *num_stripes = fs_devices->rw_devices;
2280 if (*num_stripes < 4)
Chris Mason321aecc2008-04-16 10:49:51 -04002281 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002282 *num_stripes &= ~(u32)1;
2283 *sub_stripes = 2;
2284 *min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002285 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002286
Miao Xieb2117a32011-01-05 10:07:28 +00002287 return 0;
2288}
2289
2290static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2291 u64 proposed_size, u64 type,
2292 int num_stripes, int small_stripe)
2293{
2294 int min_stripe_size = 1 * 1024 * 1024;
2295 u64 calc_size = proposed_size;
2296 u64 max_chunk_size = calc_size;
2297 int ncopies = 1;
2298
2299 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2300 BTRFS_BLOCK_GROUP_DUP |
2301 BTRFS_BLOCK_GROUP_RAID10))
2302 ncopies = 2;
2303
Chris Mason9b3f68b2008-04-18 10:29:51 -04002304 if (type & BTRFS_BLOCK_GROUP_DATA) {
2305 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002306 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002307 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002308 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002309 min_stripe_size = 32 * 1024 * 1024;
2310 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2311 calc_size = 8 * 1024 * 1024;
2312 max_chunk_size = calc_size * 2;
2313 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002314 }
2315
Yan Zheng2b820322008-11-17 21:11:30 -05002316 /* we don't want a chunk larger than 10% of writeable space */
2317 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2318 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002319
Miao Xie1974a3b2011-01-05 10:07:24 +00002320 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2321 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002322 do_div(calc_size, num_stripes);
Miao Xieb2117a32011-01-05 10:07:28 +00002323 do_div(calc_size, BTRFS_STRIPE_LEN);
2324 calc_size *= BTRFS_STRIPE_LEN;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002325 }
Josef Bacik0cad8a112010-03-17 20:45:56 +00002326
Chris Mason9b3f68b2008-04-18 10:29:51 -04002327 /* we don't want tiny stripes */
Miao Xieb2117a32011-01-05 10:07:28 +00002328 if (!small_stripe)
Josef Bacik0cad8a112010-03-17 20:45:56 +00002329 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002330
Chris Mason9f680ce2010-04-06 09:37:47 -04002331 /*
Miao Xieb2117a32011-01-05 10:07:28 +00002332 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
Chris Mason9f680ce2010-04-06 09:37:47 -04002333 * we end up with something bigger than a stripe
2334 */
Miao Xieb2117a32011-01-05 10:07:28 +00002335 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
Chris Mason9f680ce2010-04-06 09:37:47 -04002336
Miao Xieb2117a32011-01-05 10:07:28 +00002337 do_div(calc_size, BTRFS_STRIPE_LEN);
2338 calc_size *= BTRFS_STRIPE_LEN;
2339
2340 return calc_size;
2341}
2342
2343static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2344 int num_stripes)
2345{
2346 struct map_lookup *new;
2347 size_t len = map_lookup_size(num_stripes);
2348
2349 BUG_ON(map->num_stripes < num_stripes);
2350
2351 if (map->num_stripes == num_stripes)
2352 return map;
2353
2354 new = kmalloc(len, GFP_NOFS);
2355 if (!new) {
2356 /* just change map->num_stripes */
2357 map->num_stripes = num_stripes;
2358 return map;
2359 }
2360
2361 memcpy(new, map, len);
2362 new->num_stripes = num_stripes;
2363 kfree(map);
2364 return new;
2365}
2366
2367/*
2368 * helper to allocate device space from btrfs_device_info, in which we stored
2369 * max free space information of every device. It is used when we can not
2370 * allocate chunks by default size.
2371 *
2372 * By this helper, we can allocate a new chunk as larger as possible.
2373 */
2374static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2375 struct btrfs_fs_devices *fs_devices,
2376 struct btrfs_device_info *devices,
2377 int nr_device, u64 type,
2378 struct map_lookup **map_lookup,
2379 int min_stripes, u64 *stripe_size)
2380{
2381 int i, index, sort_again = 0;
2382 int min_devices = min_stripes;
2383 u64 max_avail, min_free;
2384 struct map_lookup *map = *map_lookup;
2385 int ret;
2386
2387 if (nr_device < min_stripes)
2388 return -ENOSPC;
2389
2390 btrfs_descending_sort_devices(devices, nr_device);
2391
2392 max_avail = devices[0].max_avail;
2393 if (!max_avail)
2394 return -ENOSPC;
2395
2396 for (i = 0; i < nr_device; i++) {
2397 /*
2398 * if dev_offset = 0, it means the free space of this device
2399 * is less than what we need, and we didn't search max avail
2400 * extent on this device, so do it now.
2401 */
2402 if (!devices[i].dev_offset) {
2403 ret = find_free_dev_extent(trans, devices[i].dev,
2404 max_avail,
2405 &devices[i].dev_offset,
2406 &devices[i].max_avail);
2407 if (ret != 0 && ret != -ENOSPC)
2408 return ret;
2409 sort_again = 1;
2410 }
2411 }
2412
2413 /* we update the max avail free extent of each devices, sort again */
2414 if (sort_again)
2415 btrfs_descending_sort_devices(devices, nr_device);
2416
2417 if (type & BTRFS_BLOCK_GROUP_DUP)
2418 min_devices = 1;
2419
2420 if (!devices[min_devices - 1].max_avail)
2421 return -ENOSPC;
2422
2423 max_avail = devices[min_devices - 1].max_avail;
2424 if (type & BTRFS_BLOCK_GROUP_DUP)
2425 do_div(max_avail, 2);
2426
2427 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2428 min_stripes, 1);
2429 if (type & BTRFS_BLOCK_GROUP_DUP)
2430 min_free = max_avail * 2;
2431 else
2432 min_free = max_avail;
2433
2434 if (min_free > devices[min_devices - 1].max_avail)
2435 return -ENOSPC;
2436
2437 map = __shrink_map_lookup_stripes(map, min_stripes);
2438 *stripe_size = max_avail;
2439
2440 index = 0;
2441 for (i = 0; i < min_stripes; i++) {
2442 map->stripes[i].dev = devices[index].dev;
2443 map->stripes[i].physical = devices[index].dev_offset;
2444 if (type & BTRFS_BLOCK_GROUP_DUP) {
2445 i++;
2446 map->stripes[i].dev = devices[index].dev;
2447 map->stripes[i].physical = devices[index].dev_offset +
2448 max_avail;
2449 }
2450 index++;
2451 }
2452 *map_lookup = map;
2453
2454 return 0;
2455}
2456
2457static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2458 struct btrfs_root *extent_root,
2459 struct map_lookup **map_ret,
2460 u64 *num_bytes, u64 *stripe_size,
2461 u64 start, u64 type)
2462{
2463 struct btrfs_fs_info *info = extent_root->fs_info;
2464 struct btrfs_device *device = NULL;
2465 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2466 struct list_head *cur;
2467 struct map_lookup *map;
2468 struct extent_map_tree *em_tree;
2469 struct extent_map *em;
2470 struct btrfs_device_info *devices_info;
2471 struct list_head private_devs;
2472 u64 calc_size = 1024 * 1024 * 1024;
2473 u64 min_free;
2474 u64 avail;
2475 u64 dev_offset;
2476 int num_stripes;
2477 int min_stripes;
2478 int sub_stripes;
2479 int min_devices; /* the min number of devices we need */
2480 int i;
2481 int ret;
2482 int index;
2483
2484 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2485 (type & BTRFS_BLOCK_GROUP_DUP)) {
2486 WARN_ON(1);
2487 type &= ~BTRFS_BLOCK_GROUP_DUP;
2488 }
2489 if (list_empty(&fs_devices->alloc_list))
2490 return -ENOSPC;
2491
2492 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2493 &min_stripes, &sub_stripes);
2494 if (ret)
2495 return ret;
2496
2497 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2498 GFP_NOFS);
2499 if (!devices_info)
2500 return -ENOMEM;
2501
2502 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2503 if (!map) {
2504 ret = -ENOMEM;
2505 goto error;
2506 }
2507 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002508
Yan Zheng2b820322008-11-17 21:11:30 -05002509 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002510 index = 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002511 i = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002512
Miao Xieb2117a32011-01-05 10:07:28 +00002513 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2514 num_stripes, 0);
2515
2516 if (type & BTRFS_BLOCK_GROUP_DUP) {
Chris Mason611f0e02008-04-03 16:29:03 -04002517 min_free = calc_size * 2;
Miao Xieb2117a32011-01-05 10:07:28 +00002518 min_devices = 1;
2519 } else {
Chris Mason9b3f68b2008-04-18 10:29:51 -04002520 min_free = calc_size;
Miao Xieb2117a32011-01-05 10:07:28 +00002521 min_devices = min_stripes;
2522 }
Chris Masonad5bd912008-04-21 08:28:10 -04002523
Yan Zheng2b820322008-11-17 21:11:30 -05002524 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002525 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002526 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002527 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002528 if (device->total_bytes > device->bytes_used)
2529 avail = device->total_bytes - device->bytes_used;
2530 else
2531 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002532 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002533
Chris Masondfe25022008-05-13 13:46:40 -04002534 if (device->in_fs_metadata && avail >= min_free) {
Miao Xieb2117a32011-01-05 10:07:28 +00002535 ret = find_free_dev_extent(trans, device, min_free,
2536 &devices_info[i].dev_offset,
2537 &devices_info[i].max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002538 if (ret == 0) {
2539 list_move_tail(&device->dev_alloc_list,
2540 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002541 map->stripes[index].dev = device;
Miao Xieb2117a32011-01-05 10:07:28 +00002542 map->stripes[index].physical =
2543 devices_info[i].dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002544 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002545 if (type & BTRFS_BLOCK_GROUP_DUP) {
2546 map->stripes[index].dev = device;
2547 map->stripes[index].physical =
Miao Xieb2117a32011-01-05 10:07:28 +00002548 devices_info[i].dev_offset +
2549 calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002550 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002551 }
Miao Xieb2117a32011-01-05 10:07:28 +00002552 } else if (ret != -ENOSPC)
2553 goto error;
2554
2555 devices_info[i].dev = device;
2556 i++;
2557 } else if (device->in_fs_metadata &&
2558 avail >= BTRFS_STRIPE_LEN) {
2559 devices_info[i].dev = device;
2560 devices_info[i].max_avail = avail;
2561 i++;
2562 }
2563
Yan Zheng2b820322008-11-17 21:11:30 -05002564 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002565 break;
2566 }
Miao Xieb2117a32011-01-05 10:07:28 +00002567
Yan Zheng2b820322008-11-17 21:11:30 -05002568 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002569 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002570 if (index >= min_stripes) {
2571 num_stripes = index;
2572 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2573 num_stripes /= sub_stripes;
2574 num_stripes *= sub_stripes;
2575 }
Miao Xieb2117a32011-01-05 10:07:28 +00002576
2577 map = __shrink_map_lookup_stripes(map, num_stripes);
2578 } else if (i >= min_devices) {
2579 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2580 devices_info, i, type,
2581 &map, min_stripes,
2582 &calc_size);
2583 if (ret)
2584 goto error;
2585 } else {
2586 ret = -ENOSPC;
2587 goto error;
Chris Masona40a90a2008-04-18 11:55:51 -04002588 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002589 }
Chris Mason593060d2008-03-25 16:50:33 -04002590 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002591 map->stripe_len = BTRFS_STRIPE_LEN;
2592 map->io_align = BTRFS_STRIPE_LEN;
2593 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002594 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002595 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002596
Yan Zheng2b820322008-11-17 21:11:30 -05002597 *map_ret = map;
2598 *stripe_size = calc_size;
2599 *num_bytes = chunk_bytes_by_type(type, calc_size,
Miao Xieb2117a32011-01-05 10:07:28 +00002600 map->num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002601
2602 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002603 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002604 ret = -ENOMEM;
2605 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002606 }
Chris Mason0b86a832008-03-24 15:01:56 -04002607 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002608 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002609 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002610 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002611 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002612
Chris Mason0b86a832008-03-24 15:01:56 -04002613 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002614 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002615 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002616 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002617 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002618 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002619
2620 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2621 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2622 start, *num_bytes);
2623 BUG_ON(ret);
2624
2625 index = 0;
2626 while (index < map->num_stripes) {
2627 device = map->stripes[index].dev;
2628 dev_offset = map->stripes[index].physical;
2629
2630 ret = btrfs_alloc_dev_extent(trans, device,
2631 info->chunk_root->root_key.objectid,
2632 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2633 start, dev_offset, calc_size);
2634 BUG_ON(ret);
2635 index++;
2636 }
2637
Miao Xieb2117a32011-01-05 10:07:28 +00002638 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002639 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002640
2641error:
2642 kfree(map);
2643 kfree(devices_info);
2644 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002645}
2646
2647static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2648 struct btrfs_root *extent_root,
2649 struct map_lookup *map, u64 chunk_offset,
2650 u64 chunk_size, u64 stripe_size)
2651{
2652 u64 dev_offset;
2653 struct btrfs_key key;
2654 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2655 struct btrfs_device *device;
2656 struct btrfs_chunk *chunk;
2657 struct btrfs_stripe *stripe;
2658 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2659 int index = 0;
2660 int ret;
2661
2662 chunk = kzalloc(item_size, GFP_NOFS);
2663 if (!chunk)
2664 return -ENOMEM;
2665
2666 index = 0;
2667 while (index < map->num_stripes) {
2668 device = map->stripes[index].dev;
2669 device->bytes_used += stripe_size;
2670 ret = btrfs_update_device(trans, device);
2671 BUG_ON(ret);
2672 index++;
2673 }
2674
2675 index = 0;
2676 stripe = &chunk->stripe;
2677 while (index < map->num_stripes) {
2678 device = map->stripes[index].dev;
2679 dev_offset = map->stripes[index].physical;
2680
2681 btrfs_set_stack_stripe_devid(stripe, device->devid);
2682 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2683 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2684 stripe++;
2685 index++;
2686 }
2687
2688 btrfs_set_stack_chunk_length(chunk, chunk_size);
2689 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2690 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2691 btrfs_set_stack_chunk_type(chunk, map->type);
2692 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2693 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2694 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2695 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2696 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2697
2698 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2699 key.type = BTRFS_CHUNK_ITEM_KEY;
2700 key.offset = chunk_offset;
2701
2702 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2703 BUG_ON(ret);
2704
2705 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2706 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2707 item_size);
2708 BUG_ON(ret);
2709 }
2710 kfree(chunk);
2711 return 0;
2712}
2713
2714/*
2715 * Chunk allocation falls into two parts. The first part does works
2716 * that make the new allocated chunk useable, but not do any operation
2717 * that modifies the chunk tree. The second part does the works that
2718 * require modifying the chunk tree. This division is important for the
2719 * bootstrap process of adding storage to a seed btrfs.
2720 */
2721int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2722 struct btrfs_root *extent_root, u64 type)
2723{
2724 u64 chunk_offset;
2725 u64 chunk_size;
2726 u64 stripe_size;
2727 struct map_lookup *map;
2728 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2729 int ret;
2730
2731 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2732 &chunk_offset);
2733 if (ret)
2734 return ret;
2735
2736 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2737 &stripe_size, chunk_offset, type);
2738 if (ret)
2739 return ret;
2740
2741 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2742 chunk_size, stripe_size);
2743 BUG_ON(ret);
2744 return 0;
2745}
2746
Chris Masond3977122009-01-05 21:25:51 -05002747static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002748 struct btrfs_root *root,
2749 struct btrfs_device *device)
2750{
2751 u64 chunk_offset;
2752 u64 sys_chunk_offset;
2753 u64 chunk_size;
2754 u64 sys_chunk_size;
2755 u64 stripe_size;
2756 u64 sys_stripe_size;
2757 u64 alloc_profile;
2758 struct map_lookup *map;
2759 struct map_lookup *sys_map;
2760 struct btrfs_fs_info *fs_info = root->fs_info;
2761 struct btrfs_root *extent_root = fs_info->extent_root;
2762 int ret;
2763
2764 ret = find_next_chunk(fs_info->chunk_root,
2765 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2766 BUG_ON(ret);
2767
2768 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2769 (fs_info->metadata_alloc_profile &
2770 fs_info->avail_metadata_alloc_bits);
2771 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2772
2773 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2774 &stripe_size, chunk_offset, alloc_profile);
2775 BUG_ON(ret);
2776
2777 sys_chunk_offset = chunk_offset + chunk_size;
2778
2779 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2780 (fs_info->system_alloc_profile &
2781 fs_info->avail_system_alloc_bits);
2782 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2783
2784 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2785 &sys_chunk_size, &sys_stripe_size,
2786 sys_chunk_offset, alloc_profile);
2787 BUG_ON(ret);
2788
2789 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2790 BUG_ON(ret);
2791
2792 /*
2793 * Modifying chunk tree needs allocating new blocks from both
2794 * system block group and metadata block group. So we only can
2795 * do operations require modifying the chunk tree after both
2796 * block groups were created.
2797 */
2798 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2799 chunk_size, stripe_size);
2800 BUG_ON(ret);
2801
2802 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2803 sys_chunk_offset, sys_chunk_size,
2804 sys_stripe_size);
2805 BUG_ON(ret);
2806 return 0;
2807}
2808
2809int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2810{
2811 struct extent_map *em;
2812 struct map_lookup *map;
2813 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2814 int readonly = 0;
2815 int i;
2816
Chris Mason890871b2009-09-02 16:24:52 -04002817 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002818 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002819 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002820 if (!em)
2821 return 1;
2822
Josef Bacikf48b9072010-01-27 02:07:59 +00002823 if (btrfs_test_opt(root, DEGRADED)) {
2824 free_extent_map(em);
2825 return 0;
2826 }
2827
Yan Zheng2b820322008-11-17 21:11:30 -05002828 map = (struct map_lookup *)em->bdev;
2829 for (i = 0; i < map->num_stripes; i++) {
2830 if (!map->stripes[i].dev->writeable) {
2831 readonly = 1;
2832 break;
2833 }
2834 }
2835 free_extent_map(em);
2836 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002837}
2838
2839void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2840{
2841 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2842}
2843
2844void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2845{
2846 struct extent_map *em;
2847
Chris Masond3977122009-01-05 21:25:51 -05002848 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002849 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002850 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2851 if (em)
2852 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002853 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002854 if (!em)
2855 break;
2856 kfree(em->bdev);
2857 /* once for us */
2858 free_extent_map(em);
2859 /* once for the tree */
2860 free_extent_map(em);
2861 }
2862}
2863
Chris Masonf1885912008-04-09 16:28:12 -04002864int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2865{
2866 struct extent_map *em;
2867 struct map_lookup *map;
2868 struct extent_map_tree *em_tree = &map_tree->map_tree;
2869 int ret;
2870
Chris Mason890871b2009-09-02 16:24:52 -04002871 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002872 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002873 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002874 BUG_ON(!em);
2875
2876 BUG_ON(em->start > logical || em->start + em->len < logical);
2877 map = (struct map_lookup *)em->bdev;
2878 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2879 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002880 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2881 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002882 else
2883 ret = 1;
2884 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002885 return ret;
2886}
2887
Chris Masondfe25022008-05-13 13:46:40 -04002888static int find_live_mirror(struct map_lookup *map, int first, int num,
2889 int optimal)
2890{
2891 int i;
2892 if (map->stripes[optimal].dev->bdev)
2893 return optimal;
2894 for (i = first; i < first + num; i++) {
2895 if (map->stripes[i].dev->bdev)
2896 return i;
2897 }
2898 /* we couldn't find one that doesn't fail. Just return something
2899 * and the io error handling code will clean up eventually
2900 */
2901 return optimal;
2902}
2903
Chris Masonf2d8d742008-04-21 10:03:05 -04002904static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2905 u64 logical, u64 *length,
2906 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002907 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002908{
2909 struct extent_map *em;
2910 struct map_lookup *map;
2911 struct extent_map_tree *em_tree = &map_tree->map_tree;
2912 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002913 u64 stripe_offset;
2914 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002915 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002916 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002917 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002918 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002919 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002920 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002921 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002922
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002923 if (multi_ret && !(rw & REQ_WRITE))
Chris Masoncea9e442008-04-09 16:28:12 -04002924 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002925again:
2926 if (multi_ret) {
2927 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2928 GFP_NOFS);
2929 if (!multi)
2930 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002931
2932 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002933 }
Chris Mason0b86a832008-03-24 15:01:56 -04002934
Chris Mason890871b2009-09-02 16:24:52 -04002935 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002936 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002937 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002938
Chris Mason3b951512008-04-17 11:29:12 -04002939 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002940 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2941 (unsigned long long)logical,
2942 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002943 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002944 }
Chris Mason0b86a832008-03-24 15:01:56 -04002945
2946 BUG_ON(em->start > logical || em->start + em->len < logical);
2947 map = (struct map_lookup *)em->bdev;
2948 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002949
Chris Masonf1885912008-04-09 16:28:12 -04002950 if (mirror_num > map->num_stripes)
2951 mirror_num = 0;
2952
Chris Masoncea9e442008-04-09 16:28:12 -04002953 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002954 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002955 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2956 BTRFS_BLOCK_GROUP_DUP)) {
2957 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002958 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002959 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2960 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002961 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002962 }
2963 }
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002964 if (multi_ret && (rw & REQ_WRITE) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002965 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002966 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002967 free_extent_map(em);
2968 kfree(multi);
2969 goto again;
2970 }
Chris Mason593060d2008-03-25 16:50:33 -04002971 stripe_nr = offset;
2972 /*
2973 * stripe_nr counts the total number of stripes we have to stride
2974 * to get to this block
2975 */
2976 do_div(stripe_nr, map->stripe_len);
2977
2978 stripe_offset = stripe_nr * map->stripe_len;
2979 BUG_ON(offset < stripe_offset);
2980
2981 /* stripe_offset is the offset of this block in its stripe*/
2982 stripe_offset = offset - stripe_offset;
2983
Chris Masoncea9e442008-04-09 16:28:12 -04002984 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002985 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002986 BTRFS_BLOCK_GROUP_DUP)) {
2987 /* we limit the length of each bio to what fits in a stripe */
2988 *length = min_t(u64, em->len - offset,
2989 map->stripe_len - stripe_offset);
2990 } else {
2991 *length = em->len - offset;
2992 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002993
Jens Axboe7eaceac2011-03-10 08:52:07 +01002994 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002995 goto out;
2996
Chris Masonf2d8d742008-04-21 10:03:05 -04002997 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002998 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002999 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Jens Axboe7eaceac2011-03-10 08:52:07 +01003000 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003001 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003002 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003003 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003004 else {
3005 stripe_index = find_live_mirror(map, 0,
3006 map->num_stripes,
3007 current->pid % map->num_stripes);
3008 }
Chris Mason2fff7342008-04-29 14:12:09 -04003009
Chris Mason611f0e02008-04-03 16:29:03 -04003010 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003011 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003012 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003013 else if (mirror_num)
3014 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003015
Chris Mason321aecc2008-04-16 10:49:51 -04003016 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3017 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003018
3019 stripe_index = do_div(stripe_nr, factor);
3020 stripe_index *= map->sub_stripes;
3021
Jens Axboe7eaceac2011-03-10 08:52:07 +01003022 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003023 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003024 else if (mirror_num)
3025 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003026 else {
3027 stripe_index = find_live_mirror(map, stripe_index,
3028 map->sub_stripes, stripe_index +
3029 current->pid % map->sub_stripes);
3030 }
Chris Mason8790d502008-04-03 16:29:03 -04003031 } else {
3032 /*
3033 * after this do_div call, stripe_nr is the number of stripes
3034 * on this device we have to walk to find the data, and
3035 * stripe_index is the number of our device in the stripe array
3036 */
3037 stripe_index = do_div(stripe_nr, map->num_stripes);
3038 }
Chris Mason593060d2008-03-25 16:50:33 -04003039 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003040
Chris Masonf2d8d742008-04-21 10:03:05 -04003041 for (i = 0; i < num_stripes; i++) {
Jens Axboe7eaceac2011-03-10 08:52:07 +01003042 multi->stripes[i].physical =
3043 map->stripes[stripe_index].physical +
3044 stripe_offset + stripe_nr * map->stripe_len;
3045 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Chris Masoncea9e442008-04-09 16:28:12 -04003046 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04003047 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003048 if (multi_ret) {
3049 *multi_ret = multi;
3050 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003051 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003052 }
Chris Masoncea9e442008-04-09 16:28:12 -04003053out:
Chris Mason0b86a832008-03-24 15:01:56 -04003054 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003055 return 0;
3056}
3057
Chris Masonf2d8d742008-04-21 10:03:05 -04003058int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3059 u64 logical, u64 *length,
3060 struct btrfs_multi_bio **multi_ret, int mirror_num)
3061{
3062 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003063 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003064}
3065
Yan Zhenga512bbf2008-12-08 16:46:26 -05003066int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3067 u64 chunk_start, u64 physical, u64 devid,
3068 u64 **logical, int *naddrs, int *stripe_len)
3069{
3070 struct extent_map_tree *em_tree = &map_tree->map_tree;
3071 struct extent_map *em;
3072 struct map_lookup *map;
3073 u64 *buf;
3074 u64 bytenr;
3075 u64 length;
3076 u64 stripe_nr;
3077 int i, j, nr = 0;
3078
Chris Mason890871b2009-09-02 16:24:52 -04003079 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003080 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003081 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003082
3083 BUG_ON(!em || em->start != chunk_start);
3084 map = (struct map_lookup *)em->bdev;
3085
3086 length = em->len;
3087 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3088 do_div(length, map->num_stripes / map->sub_stripes);
3089 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3090 do_div(length, map->num_stripes);
3091
3092 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3093 BUG_ON(!buf);
3094
3095 for (i = 0; i < map->num_stripes; i++) {
3096 if (devid && map->stripes[i].dev->devid != devid)
3097 continue;
3098 if (map->stripes[i].physical > physical ||
3099 map->stripes[i].physical + length <= physical)
3100 continue;
3101
3102 stripe_nr = physical - map->stripes[i].physical;
3103 do_div(stripe_nr, map->stripe_len);
3104
3105 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3106 stripe_nr = stripe_nr * map->num_stripes + i;
3107 do_div(stripe_nr, map->sub_stripes);
3108 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3109 stripe_nr = stripe_nr * map->num_stripes + i;
3110 }
3111 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003112 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003113 for (j = 0; j < nr; j++) {
3114 if (buf[j] == bytenr)
3115 break;
3116 }
Chris Mason934d3752008-12-08 16:43:10 -05003117 if (j == nr) {
3118 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003119 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003120 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003121 }
3122
Yan Zhenga512bbf2008-12-08 16:46:26 -05003123 *logical = buf;
3124 *naddrs = nr;
3125 *stripe_len = map->stripe_len;
3126
3127 free_extent_map(em);
3128 return 0;
3129}
3130
Chris Mason8790d502008-04-03 16:29:03 -04003131static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003132{
Chris Masoncea9e442008-04-09 16:28:12 -04003133 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003134 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003135
Chris Mason8790d502008-04-03 16:29:03 -04003136 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003137 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003138
Chris Mason7d2b4da2008-08-05 10:13:57 -04003139 if (bio == multi->orig_bio)
3140 is_orig_bio = 1;
3141
Chris Masoncea9e442008-04-09 16:28:12 -04003142 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003143 if (!is_orig_bio) {
3144 bio_put(bio);
3145 bio = multi->orig_bio;
3146 }
Chris Mason8790d502008-04-03 16:29:03 -04003147 bio->bi_private = multi->private;
3148 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003149 /* only send an error to the higher layers if it is
3150 * beyond the tolerance of the multi-bio
3151 */
Chris Mason1259ab72008-05-12 13:39:03 -04003152 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003153 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003154 } else if (err) {
3155 /*
3156 * this bio is actually up to date, we didn't
3157 * go over the max number of errors
3158 */
3159 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003160 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003161 }
Chris Mason8790d502008-04-03 16:29:03 -04003162 kfree(multi);
3163
3164 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003165 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003166 bio_put(bio);
3167 }
Chris Mason8790d502008-04-03 16:29:03 -04003168}
3169
Chris Mason8b712842008-06-11 16:50:36 -04003170struct async_sched {
3171 struct bio *bio;
3172 int rw;
3173 struct btrfs_fs_info *info;
3174 struct btrfs_work work;
3175};
3176
3177/*
3178 * see run_scheduled_bios for a description of why bios are collected for
3179 * async submit.
3180 *
3181 * This will add one bio to the pending list for a device and make sure
3182 * the work struct is scheduled.
3183 */
Chris Masond3977122009-01-05 21:25:51 -05003184static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003185 struct btrfs_device *device,
3186 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003187{
3188 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003189 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003190
3191 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003192 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003193 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003194 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003195 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003196 return 0;
3197 }
3198
3199 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003200 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003201 * higher layers. Otherwise, the async bio makes it appear we have
3202 * made progress against dirty pages when we've really just put it
3203 * on a queue for later
3204 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003205 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003206 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003207 bio->bi_next = NULL;
3208 bio->bi_rw |= rw;
3209
3210 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003211 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003212 pending_bios = &device->pending_sync_bios;
3213 else
3214 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003215
Chris Masonffbd5172009-04-20 15:50:09 -04003216 if (pending_bios->tail)
3217 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003218
Chris Masonffbd5172009-04-20 15:50:09 -04003219 pending_bios->tail = bio;
3220 if (!pending_bios->head)
3221 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003222 if (device->running_pending)
3223 should_queue = 0;
3224
3225 spin_unlock(&device->io_lock);
3226
3227 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003228 btrfs_queue_worker(&root->fs_info->submit_workers,
3229 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003230 return 0;
3231}
3232
Chris Masonf1885912008-04-09 16:28:12 -04003233int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003234 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003235{
3236 struct btrfs_mapping_tree *map_tree;
3237 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003238 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003239 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003240 u64 length = 0;
3241 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003242 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003243 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003244 int dev_nr = 0;
3245 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003246
Chris Masonf2d8d742008-04-21 10:03:05 -04003247 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003248 map_tree = &root->fs_info->mapping_tree;
3249 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003250
Chris Masonf1885912008-04-09 16:28:12 -04003251 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3252 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003253 BUG_ON(ret);
3254
3255 total_devs = multi->num_stripes;
3256 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003257 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3258 "len %llu\n", (unsigned long long)logical,
3259 (unsigned long long)length,
3260 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003261 BUG();
3262 }
3263 multi->end_io = first_bio->bi_end_io;
3264 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003265 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003266 atomic_set(&multi->stripes_pending, multi->num_stripes);
3267
Chris Masond3977122009-01-05 21:25:51 -05003268 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003269 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003270 if (dev_nr < total_devs - 1) {
3271 bio = bio_clone(first_bio, GFP_NOFS);
3272 BUG_ON(!bio);
3273 } else {
3274 bio = first_bio;
3275 }
3276 bio->bi_private = multi;
3277 bio->bi_end_io = end_bio_multi_stripe;
3278 }
Chris Masoncea9e442008-04-09 16:28:12 -04003279 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3280 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003281 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003282 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003283 if (async_submit)
3284 schedule_bio(root, dev, rw, bio);
3285 else
3286 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003287 } else {
3288 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3289 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003290 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003291 }
Chris Mason8790d502008-04-03 16:29:03 -04003292 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003293 }
Chris Masoncea9e442008-04-09 16:28:12 -04003294 if (total_devs == 1)
3295 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003296 return 0;
3297}
3298
Chris Masona4437552008-04-18 10:29:38 -04003299struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003300 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003301{
Yan Zheng2b820322008-11-17 21:11:30 -05003302 struct btrfs_device *device;
3303 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003304
Yan Zheng2b820322008-11-17 21:11:30 -05003305 cur_devices = root->fs_info->fs_devices;
3306 while (cur_devices) {
3307 if (!fsid ||
3308 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3309 device = __find_device(&cur_devices->devices,
3310 devid, uuid);
3311 if (device)
3312 return device;
3313 }
3314 cur_devices = cur_devices->seed;
3315 }
3316 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003317}
3318
Chris Masondfe25022008-05-13 13:46:40 -04003319static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3320 u64 devid, u8 *dev_uuid)
3321{
3322 struct btrfs_device *device;
3323 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3324
3325 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003326 if (!device)
3327 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003328 list_add(&device->dev_list,
3329 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003330 device->dev_root = root->fs_info->dev_root;
3331 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003332 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003333 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003334 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003335 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003336 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003337 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003338 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003339 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3340 return device;
3341}
3342
Chris Mason0b86a832008-03-24 15:01:56 -04003343static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3344 struct extent_buffer *leaf,
3345 struct btrfs_chunk *chunk)
3346{
3347 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3348 struct map_lookup *map;
3349 struct extent_map *em;
3350 u64 logical;
3351 u64 length;
3352 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003353 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003354 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003355 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003356 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003357
Chris Masone17cade2008-04-15 15:41:47 -04003358 logical = key->offset;
3359 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003360
Chris Mason890871b2009-09-02 16:24:52 -04003361 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003362 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003363 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003364
3365 /* already mapped? */
3366 if (em && em->start <= logical && em->start + em->len > logical) {
3367 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003368 return 0;
3369 } else if (em) {
3370 free_extent_map(em);
3371 }
Chris Mason0b86a832008-03-24 15:01:56 -04003372
Chris Mason0b86a832008-03-24 15:01:56 -04003373 em = alloc_extent_map(GFP_NOFS);
3374 if (!em)
3375 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003376 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3377 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003378 if (!map) {
3379 free_extent_map(em);
3380 return -ENOMEM;
3381 }
3382
3383 em->bdev = (struct block_device *)map;
3384 em->start = logical;
3385 em->len = length;
3386 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003387 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003388
Chris Mason593060d2008-03-25 16:50:33 -04003389 map->num_stripes = num_stripes;
3390 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3391 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3392 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3393 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3394 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003395 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003396 for (i = 0; i < num_stripes; i++) {
3397 map->stripes[i].physical =
3398 btrfs_stripe_offset_nr(leaf, chunk, i);
3399 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003400 read_extent_buffer(leaf, uuid, (unsigned long)
3401 btrfs_stripe_dev_uuid_nr(chunk, i),
3402 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003403 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3404 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003405 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003406 kfree(map);
3407 free_extent_map(em);
3408 return -EIO;
3409 }
Chris Masondfe25022008-05-13 13:46:40 -04003410 if (!map->stripes[i].dev) {
3411 map->stripes[i].dev =
3412 add_missing_dev(root, devid, uuid);
3413 if (!map->stripes[i].dev) {
3414 kfree(map);
3415 free_extent_map(em);
3416 return -EIO;
3417 }
3418 }
3419 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003420 }
3421
Chris Mason890871b2009-09-02 16:24:52 -04003422 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003423 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003424 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003425 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003426 free_extent_map(em);
3427
3428 return 0;
3429}
3430
3431static int fill_device_from_item(struct extent_buffer *leaf,
3432 struct btrfs_dev_item *dev_item,
3433 struct btrfs_device *device)
3434{
3435 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003436
3437 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003438 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3439 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003440 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3441 device->type = btrfs_device_type(leaf, dev_item);
3442 device->io_align = btrfs_device_io_align(leaf, dev_item);
3443 device->io_width = btrfs_device_io_width(leaf, dev_item);
3444 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003445
3446 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003447 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003448
Chris Mason0b86a832008-03-24 15:01:56 -04003449 return 0;
3450}
3451
Yan Zheng2b820322008-11-17 21:11:30 -05003452static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3453{
3454 struct btrfs_fs_devices *fs_devices;
3455 int ret;
3456
3457 mutex_lock(&uuid_mutex);
3458
3459 fs_devices = root->fs_info->fs_devices->seed;
3460 while (fs_devices) {
3461 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3462 ret = 0;
3463 goto out;
3464 }
3465 fs_devices = fs_devices->seed;
3466 }
3467
3468 fs_devices = find_fsid(fsid);
3469 if (!fs_devices) {
3470 ret = -ENOENT;
3471 goto out;
3472 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003473
3474 fs_devices = clone_fs_devices(fs_devices);
3475 if (IS_ERR(fs_devices)) {
3476 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003477 goto out;
3478 }
3479
Christoph Hellwig97288f22008-12-02 06:36:09 -05003480 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003481 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003482 if (ret)
3483 goto out;
3484
3485 if (!fs_devices->seeding) {
3486 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003487 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003488 ret = -EINVAL;
3489 goto out;
3490 }
3491
3492 fs_devices->seed = root->fs_info->fs_devices->seed;
3493 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003494out:
3495 mutex_unlock(&uuid_mutex);
3496 return ret;
3497}
3498
Chris Mason0d81ba52008-03-24 15:02:07 -04003499static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003500 struct extent_buffer *leaf,
3501 struct btrfs_dev_item *dev_item)
3502{
3503 struct btrfs_device *device;
3504 u64 devid;
3505 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003506 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003507 u8 dev_uuid[BTRFS_UUID_SIZE];
3508
Chris Mason0b86a832008-03-24 15:01:56 -04003509 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003510 read_extent_buffer(leaf, dev_uuid,
3511 (unsigned long)btrfs_device_uuid(dev_item),
3512 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003513 read_extent_buffer(leaf, fs_uuid,
3514 (unsigned long)btrfs_device_fsid(dev_item),
3515 BTRFS_UUID_SIZE);
3516
3517 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3518 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003519 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003520 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003521 }
3522
3523 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3524 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003525 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003526 return -EIO;
3527
3528 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003529 printk(KERN_WARNING "warning devid %llu missing\n",
3530 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003531 device = add_missing_dev(root, devid, dev_uuid);
3532 if (!device)
3533 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003534 } else if (!device->missing) {
3535 /*
3536 * this happens when a device that was properly setup
3537 * in the device info lists suddenly goes bad.
3538 * device->bdev is NULL, and so we have to set
3539 * device->missing to one here
3540 */
3541 root->fs_info->fs_devices->missing_devices++;
3542 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003543 }
3544 }
3545
3546 if (device->fs_devices != root->fs_info->fs_devices) {
3547 BUG_ON(device->writeable);
3548 if (device->generation !=
3549 btrfs_device_generation(leaf, dev_item))
3550 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003551 }
Chris Mason0b86a832008-03-24 15:01:56 -04003552
3553 fill_device_from_item(leaf, dev_item, device);
3554 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003555 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003556 if (device->writeable)
3557 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003558 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003559 return ret;
3560}
3561
Chris Mason0d81ba52008-03-24 15:02:07 -04003562int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3563{
3564 struct btrfs_dev_item *dev_item;
3565
3566 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3567 dev_item);
3568 return read_one_dev(root, buf, dev_item);
3569}
3570
Yan Zhenge4404d62008-12-12 10:03:26 -05003571int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003572{
3573 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003574 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003575 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003576 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003577 u8 *ptr;
3578 unsigned long sb_ptr;
3579 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003580 u32 num_stripes;
3581 u32 array_size;
3582 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003583 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003584 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003585
Yan Zhenge4404d62008-12-12 10:03:26 -05003586 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003587 BTRFS_SUPER_INFO_SIZE);
3588 if (!sb)
3589 return -ENOMEM;
3590 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003591 btrfs_set_buffer_lockdep_class(sb, 0);
3592
Chris Masona061fc82008-05-07 11:43:44 -04003593 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003594 array_size = btrfs_super_sys_array_size(super_copy);
3595
Chris Mason0b86a832008-03-24 15:01:56 -04003596 ptr = super_copy->sys_chunk_array;
3597 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3598 cur = 0;
3599
3600 while (cur < array_size) {
3601 disk_key = (struct btrfs_disk_key *)ptr;
3602 btrfs_disk_key_to_cpu(&key, disk_key);
3603
Chris Masona061fc82008-05-07 11:43:44 -04003604 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003605 sb_ptr += len;
3606 cur += len;
3607
Chris Mason0d81ba52008-03-24 15:02:07 -04003608 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003609 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003610 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003611 if (ret)
3612 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003613 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3614 len = btrfs_chunk_item_size(num_stripes);
3615 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003616 ret = -EIO;
3617 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003618 }
3619 ptr += len;
3620 sb_ptr += len;
3621 cur += len;
3622 }
Chris Masona061fc82008-05-07 11:43:44 -04003623 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003624 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003625}
3626
3627int btrfs_read_chunk_tree(struct btrfs_root *root)
3628{
3629 struct btrfs_path *path;
3630 struct extent_buffer *leaf;
3631 struct btrfs_key key;
3632 struct btrfs_key found_key;
3633 int ret;
3634 int slot;
3635
3636 root = root->fs_info->chunk_root;
3637
3638 path = btrfs_alloc_path();
3639 if (!path)
3640 return -ENOMEM;
3641
3642 /* first we search for all of the device items, and then we
3643 * read in all of the chunk items. This way we can create chunk
3644 * mappings that reference all of the devices that are afound
3645 */
3646 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3647 key.offset = 0;
3648 key.type = 0;
3649again:
3650 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003651 if (ret < 0)
3652 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003653 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003654 leaf = path->nodes[0];
3655 slot = path->slots[0];
3656 if (slot >= btrfs_header_nritems(leaf)) {
3657 ret = btrfs_next_leaf(root, path);
3658 if (ret == 0)
3659 continue;
3660 if (ret < 0)
3661 goto error;
3662 break;
3663 }
3664 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3665 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3666 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3667 break;
3668 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3669 struct btrfs_dev_item *dev_item;
3670 dev_item = btrfs_item_ptr(leaf, slot,
3671 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003672 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003673 if (ret)
3674 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003675 }
3676 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3677 struct btrfs_chunk *chunk;
3678 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3679 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003680 if (ret)
3681 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003682 }
3683 path->slots[0]++;
3684 }
3685 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3686 key.objectid = 0;
3687 btrfs_release_path(root, path);
3688 goto again;
3689 }
Chris Mason0b86a832008-03-24 15:01:56 -04003690 ret = 0;
3691error:
Yan Zheng2b820322008-11-17 21:11:30 -05003692 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003693 return ret;
3694}