blob: 37a8ea23e81de085ed67b40ac0bc47e3e925f669 [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>
Chris Mason8a4b83c2008-03-24 15:02:07 -040020#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040021#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040022#include <linux/random.h>
Chris Mason593060d2008-03-25 16:50:33 -040023#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040024#include "ctree.h"
25#include "extent_map.h"
26#include "disk-io.h"
27#include "transaction.h"
28#include "print-tree.h"
29#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040030#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031
Chris Mason593060d2008-03-25 16:50:33 -040032struct map_lookup {
33 u64 type;
34 int io_align;
35 int io_width;
36 int stripe_len;
37 int sector_size;
38 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040039 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040040 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040041};
42
43#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040044 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040045
Chris Mason8a4b83c2008-03-24 15:02:07 -040046static DEFINE_MUTEX(uuid_mutex);
47static LIST_HEAD(fs_uuids);
48
Chris Masona061fc82008-05-07 11:43:44 -040049void btrfs_lock_volumes(void)
50{
51 mutex_lock(&uuid_mutex);
52}
53
54void btrfs_unlock_volumes(void)
55{
56 mutex_unlock(&uuid_mutex);
57}
58
Chris Mason7d9eb122008-07-08 14:19:17 -040059static void lock_chunks(struct btrfs_root *root)
60{
61 mutex_lock(&root->fs_info->alloc_mutex);
62 mutex_lock(&root->fs_info->chunk_mutex);
63}
64
65static void unlock_chunks(struct btrfs_root *root)
66{
67 mutex_unlock(&root->fs_info->alloc_mutex);
68 mutex_unlock(&root->fs_info->chunk_mutex);
69}
70
Chris Mason8a4b83c2008-03-24 15:02:07 -040071int btrfs_cleanup_fs_uuids(void)
72{
73 struct btrfs_fs_devices *fs_devices;
74 struct list_head *uuid_cur;
75 struct list_head *devices_cur;
76 struct btrfs_device *dev;
77
78 list_for_each(uuid_cur, &fs_uuids) {
79 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
80 list);
81 while(!list_empty(&fs_devices->devices)) {
82 devices_cur = fs_devices->devices.next;
83 dev = list_entry(devices_cur, struct btrfs_device,
84 dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -040085 if (dev->bdev) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040086 close_bdev_excl(dev->bdev);
Chris Masona0af4692008-05-13 16:03:06 -040087 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -040088 }
89 list_del(&dev->dev_list);
Chris Masondfe25022008-05-13 13:46:40 -040090 kfree(dev->name);
Chris Mason8a4b83c2008-03-24 15:02:07 -040091 kfree(dev);
92 }
93 }
94 return 0;
95}
96
Chris Masona1b32a52008-09-05 16:09:51 -040097static noinline struct btrfs_device *__find_device(struct list_head *head,
98 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040099{
100 struct btrfs_device *dev;
101 struct list_head *cur;
102
103 list_for_each(cur, head) {
104 dev = list_entry(cur, struct btrfs_device, dev_list);
Chris Masona4437552008-04-18 10:29:38 -0400105 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400106 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400107 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400108 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400109 }
110 return NULL;
111}
112
Chris Masona1b32a52008-09-05 16:09:51 -0400113static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400114{
115 struct list_head *cur;
116 struct btrfs_fs_devices *fs_devices;
117
118 list_for_each(cur, &fs_uuids) {
119 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
120 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
121 return fs_devices;
122 }
123 return NULL;
124}
125
Chris Mason8b712842008-06-11 16:50:36 -0400126/*
127 * we try to collect pending bios for a device so we don't get a large
128 * number of procs sending bios down to the same device. This greatly
129 * improves the schedulers ability to collect and merge the bios.
130 *
131 * But, it also turns into a long list of bios to process and that is sure
132 * to eventually make the worker thread block. The solution here is to
133 * make some progress and then put this work struct back at the end of
134 * the list if the block device is congested. This way, multiple devices
135 * can make progress from a single worker thread.
136 */
Chris Masona1b32a52008-09-05 16:09:51 -0400137static int noinline run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400138{
139 struct bio *pending;
140 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400141 struct btrfs_fs_info *fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400142 struct bio *tail;
143 struct bio *cur;
144 int again = 0;
145 unsigned long num_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400146 unsigned long limit;
Chris Mason8b712842008-06-11 16:50:36 -0400147
148 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
Chris Masonb64a2852008-08-20 13:39:41 -0400149 fs_info = device->dev_root->fs_info;
150 limit = btrfs_async_submit_limit(fs_info);
151 limit = limit * 2 / 3;
152
Chris Mason8b712842008-06-11 16:50:36 -0400153loop:
154 spin_lock(&device->io_lock);
155
156 /* take all the bios off the list at once and process them
157 * later on (without the lock held). But, remember the
158 * tail and other pointers so the bios can be properly reinserted
159 * into the list if we hit congestion
160 */
161 pending = device->pending_bios;
162 tail = device->pending_bio_tail;
163 WARN_ON(pending && !tail);
164 device->pending_bios = NULL;
165 device->pending_bio_tail = NULL;
166
167 /*
168 * if pending was null this time around, no bios need processing
169 * at all and we can stop. Otherwise it'll loop back up again
170 * and do an additional check so no bios are missed.
171 *
172 * device->running_pending is used to synchronize with the
173 * schedule_bio code.
174 */
175 if (pending) {
176 again = 1;
177 device->running_pending = 1;
178 } else {
179 again = 0;
180 device->running_pending = 0;
181 }
182 spin_unlock(&device->io_lock);
183
184 while(pending) {
185 cur = pending;
186 pending = pending->bi_next;
187 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400188 atomic_dec(&fs_info->nr_async_bios);
189
190 if (atomic_read(&fs_info->nr_async_bios) < limit &&
191 waitqueue_active(&fs_info->async_submit_wait))
192 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400193
194 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
195 bio_get(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400196 submit_bio(cur->bi_rw, cur);
Chris Mason492bb6d2008-07-31 16:29:02 -0400197 bio_put(cur);
Chris Mason8b712842008-06-11 16:50:36 -0400198 num_run++;
199
200 /*
201 * we made progress, there is more work to do and the bdi
202 * is now congested. Back off and let other work structs
203 * run instead
204 */
Chris Mason492bb6d2008-07-31 16:29:02 -0400205 if (pending && bdi_write_congested(bdi)) {
Chris Mason8b712842008-06-11 16:50:36 -0400206 struct bio *old_head;
207
208 spin_lock(&device->io_lock);
Chris Mason492bb6d2008-07-31 16:29:02 -0400209
Chris Mason8b712842008-06-11 16:50:36 -0400210 old_head = device->pending_bios;
211 device->pending_bios = pending;
212 if (device->pending_bio_tail)
213 tail->bi_next = old_head;
214 else
215 device->pending_bio_tail = tail;
216
217 spin_unlock(&device->io_lock);
218 btrfs_requeue_work(&device->work);
219 goto done;
220 }
221 }
222 if (again)
223 goto loop;
224done:
225 return 0;
226}
227
228void pending_bios_fn(struct btrfs_work *work)
229{
230 struct btrfs_device *device;
231
232 device = container_of(work, struct btrfs_device, work);
233 run_scheduled_bios(device);
234}
235
Chris Masona1b32a52008-09-05 16:09:51 -0400236static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400237 struct btrfs_super_block *disk_super,
238 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
239{
240 struct btrfs_device *device;
241 struct btrfs_fs_devices *fs_devices;
242 u64 found_transid = btrfs_super_generation(disk_super);
243
244 fs_devices = find_fsid(disk_super->fsid);
245 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400246 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400247 if (!fs_devices)
248 return -ENOMEM;
249 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400250 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400251 list_add(&fs_devices->list, &fs_uuids);
252 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
253 fs_devices->latest_devid = devid;
254 fs_devices->latest_trans = found_transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400255 device = NULL;
256 } else {
Chris Masona4437552008-04-18 10:29:38 -0400257 device = __find_device(&fs_devices->devices, devid,
258 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400259 }
260 if (!device) {
261 device = kzalloc(sizeof(*device), GFP_NOFS);
262 if (!device) {
263 /* we can safely leave the fs_devices entry around */
264 return -ENOMEM;
265 }
266 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400267 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400268 memcpy(device->uuid, disk_super->dev_item.uuid,
269 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400270 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400271 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400272 device->name = kstrdup(path, GFP_NOFS);
273 if (!device->name) {
274 kfree(device);
275 return -ENOMEM;
276 }
277 list_add(&device->dev_list, &fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400278 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400279 fs_devices->num_devices++;
280 }
281
282 if (found_transid > fs_devices->latest_trans) {
283 fs_devices->latest_devid = devid;
284 fs_devices->latest_trans = found_transid;
285 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400286 *fs_devices_ret = fs_devices;
287 return 0;
288}
289
Chris Masondfe25022008-05-13 13:46:40 -0400290int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
291{
292 struct list_head *head = &fs_devices->devices;
293 struct list_head *cur;
294 struct btrfs_device *device;
295
296 mutex_lock(&uuid_mutex);
297again:
298 list_for_each(cur, head) {
299 device = list_entry(cur, struct btrfs_device, dev_list);
300 if (!device->in_fs_metadata) {
Chris Masona74a4b92008-06-25 16:01:31 -0400301 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400302 list_del(&device->dev_list);
303 list_del(&device->dev_alloc_list);
304 fs_devices->num_devices--;
Chris Masona74a4b92008-06-25 16:01:31 -0400305 if (device->bdev) {
306 bdev = device->bdev;
307 fs_devices->open_devices--;
308 mutex_unlock(&uuid_mutex);
309 close_bdev_excl(bdev);
310 mutex_lock(&uuid_mutex);
311 }
Chris Masondfe25022008-05-13 13:46:40 -0400312 kfree(device->name);
313 kfree(device);
314 goto again;
315 }
316 }
317 mutex_unlock(&uuid_mutex);
318 return 0;
319}
Chris Masona0af4692008-05-13 16:03:06 -0400320
Chris Mason8a4b83c2008-03-24 15:02:07 -0400321int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
322{
323 struct list_head *head = &fs_devices->devices;
324 struct list_head *cur;
325 struct btrfs_device *device;
326
327 mutex_lock(&uuid_mutex);
328 list_for_each(cur, head) {
329 device = list_entry(cur, struct btrfs_device, dev_list);
330 if (device->bdev) {
331 close_bdev_excl(device->bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400332 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400333 }
334 device->bdev = NULL;
Chris Masondfe25022008-05-13 13:46:40 -0400335 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400336 }
Chris Masona0af4692008-05-13 16:03:06 -0400337 fs_devices->mounted = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400338 mutex_unlock(&uuid_mutex);
339 return 0;
340}
341
342int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
343 int flags, void *holder)
344{
345 struct block_device *bdev;
346 struct list_head *head = &fs_devices->devices;
347 struct list_head *cur;
348 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400349 struct block_device *latest_bdev = NULL;
350 struct buffer_head *bh;
351 struct btrfs_super_block *disk_super;
352 u64 latest_devid = 0;
353 u64 latest_transid = 0;
354 u64 transid;
355 u64 devid;
356 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400357
358 mutex_lock(&uuid_mutex);
Chris Masona0af4692008-05-13 16:03:06 -0400359 if (fs_devices->mounted)
360 goto out;
361
Chris Mason8a4b83c2008-03-24 15:02:07 -0400362 list_for_each(cur, head) {
363 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Masonc1c4d912008-05-08 15:05:58 -0400364 if (device->bdev)
365 continue;
366
Chris Masondfe25022008-05-13 13:46:40 -0400367 if (!device->name)
368 continue;
369
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 bdev = open_bdev_excl(device->name, flags, holder);
Chris Masone17cade2008-04-15 15:41:47 -0400371
Chris Mason8a4b83c2008-03-24 15:02:07 -0400372 if (IS_ERR(bdev)) {
373 printk("open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400374 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400375 }
Chris Masona061fc82008-05-07 11:43:44 -0400376 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400377
378 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
379 if (!bh)
380 goto error_close;
381
382 disk_super = (struct btrfs_super_block *)bh->b_data;
383 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
384 sizeof(disk_super->magic)))
385 goto error_brelse;
386
387 devid = le64_to_cpu(disk_super->dev_item.devid);
388 if (devid != device->devid)
389 goto error_brelse;
390
391 transid = btrfs_super_generation(disk_super);
Chris Mason6af5ac32008-05-16 13:14:57 -0400392 if (!latest_transid || transid > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400393 latest_devid = devid;
394 latest_transid = transid;
395 latest_bdev = bdev;
396 }
397
Chris Mason8a4b83c2008-03-24 15:02:07 -0400398 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400399 device->in_fs_metadata = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400400 fs_devices->open_devices++;
401 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400402
Chris Masona0af4692008-05-13 16:03:06 -0400403error_brelse:
404 brelse(bh);
405error_close:
406 close_bdev_excl(bdev);
407error:
408 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400409 }
Chris Masona0af4692008-05-13 16:03:06 -0400410 if (fs_devices->open_devices == 0) {
411 ret = -EIO;
412 goto out;
413 }
414 fs_devices->mounted = 1;
415 fs_devices->latest_bdev = latest_bdev;
416 fs_devices->latest_devid = latest_devid;
417 fs_devices->latest_trans = latest_transid;
418out:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400419 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400420 return ret;
421}
422
423int btrfs_scan_one_device(const char *path, int flags, void *holder,
424 struct btrfs_fs_devices **fs_devices_ret)
425{
426 struct btrfs_super_block *disk_super;
427 struct block_device *bdev;
428 struct buffer_head *bh;
429 int ret;
430 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400431 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400432
433 mutex_lock(&uuid_mutex);
434
Chris Mason8a4b83c2008-03-24 15:02:07 -0400435 bdev = open_bdev_excl(path, flags, holder);
436
437 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400438 ret = PTR_ERR(bdev);
439 goto error;
440 }
441
442 ret = set_blocksize(bdev, 4096);
443 if (ret)
444 goto error_close;
445 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
446 if (!bh) {
447 ret = -EIO;
448 goto error_close;
449 }
450 disk_super = (struct btrfs_super_block *)bh->b_data;
451 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
452 sizeof(disk_super->magic))) {
Yane58ca022008-04-01 11:21:34 -0400453 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400454 goto error_brelse;
455 }
456 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400457 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400458 if (disk_super->label[0])
459 printk("device label %s ", disk_super->label);
460 else {
461 /* FIXME, make a readl uuid parser */
462 printk("device fsid %llx-%llx ",
463 *(unsigned long long *)disk_super->fsid,
464 *(unsigned long long *)(disk_super->fsid + 8));
465 }
466 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400467 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
468
469error_brelse:
470 brelse(bh);
471error_close:
472 close_bdev_excl(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400473error:
474 mutex_unlock(&uuid_mutex);
475 return ret;
476}
Chris Mason0b86a832008-03-24 15:01:56 -0400477
478/*
479 * this uses a pretty simple search, the expectation is that it is
480 * called very infrequently and that a given device has a small number
481 * of extents
482 */
Chris Masona1b32a52008-09-05 16:09:51 -0400483static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
484 struct btrfs_device *device,
485 struct btrfs_path *path,
486 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400487{
488 struct btrfs_key key;
489 struct btrfs_root *root = device->dev_root;
490 struct btrfs_dev_extent *dev_extent = NULL;
491 u64 hole_size = 0;
492 u64 last_byte = 0;
493 u64 search_start = 0;
494 u64 search_end = device->total_bytes;
495 int ret;
496 int slot = 0;
497 int start_found;
498 struct extent_buffer *l;
499
500 start_found = 0;
501 path->reada = 2;
502
503 /* FIXME use last free of some kind */
504
Chris Mason8a4b83c2008-03-24 15:02:07 -0400505 /* we don't want to overwrite the superblock on the drive,
506 * so we make sure to start at an offset of at least 1MB
507 */
508 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400509
510 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
511 search_start = max(root->fs_info->alloc_start, search_start);
512
Chris Mason0b86a832008-03-24 15:01:56 -0400513 key.objectid = device->devid;
514 key.offset = search_start;
515 key.type = BTRFS_DEV_EXTENT_KEY;
516 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
517 if (ret < 0)
518 goto error;
519 ret = btrfs_previous_item(root, path, 0, key.type);
520 if (ret < 0)
521 goto error;
522 l = path->nodes[0];
523 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
524 while (1) {
525 l = path->nodes[0];
526 slot = path->slots[0];
527 if (slot >= btrfs_header_nritems(l)) {
528 ret = btrfs_next_leaf(root, path);
529 if (ret == 0)
530 continue;
531 if (ret < 0)
532 goto error;
533no_more_items:
534 if (!start_found) {
535 if (search_start >= search_end) {
536 ret = -ENOSPC;
537 goto error;
538 }
539 *start = search_start;
540 start_found = 1;
541 goto check_pending;
542 }
543 *start = last_byte > search_start ?
544 last_byte : search_start;
545 if (search_end <= *start) {
546 ret = -ENOSPC;
547 goto error;
548 }
549 goto check_pending;
550 }
551 btrfs_item_key_to_cpu(l, &key, slot);
552
553 if (key.objectid < device->devid)
554 goto next;
555
556 if (key.objectid > device->devid)
557 goto no_more_items;
558
559 if (key.offset >= search_start && key.offset > last_byte &&
560 start_found) {
561 if (last_byte < search_start)
562 last_byte = search_start;
563 hole_size = key.offset - last_byte;
564 if (key.offset > last_byte &&
565 hole_size >= num_bytes) {
566 *start = last_byte;
567 goto check_pending;
568 }
569 }
570 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
571 goto next;
572 }
573
574 start_found = 1;
575 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
576 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
577next:
578 path->slots[0]++;
579 cond_resched();
580 }
581check_pending:
582 /* we have to make sure we didn't find an extent that has already
583 * been allocated by the map tree or the original allocation
584 */
585 btrfs_release_path(root, path);
586 BUG_ON(*start < search_start);
587
Chris Mason6324fbf2008-03-24 15:01:59 -0400588 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400589 ret = -ENOSPC;
590 goto error;
591 }
592 /* check for pending inserts here */
593 return 0;
594
595error:
596 btrfs_release_path(root, path);
597 return ret;
598}
599
Chris Mason8f18cf12008-04-25 16:53:30 -0400600int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
601 struct btrfs_device *device,
602 u64 start)
603{
604 int ret;
605 struct btrfs_path *path;
606 struct btrfs_root *root = device->dev_root;
607 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400608 struct btrfs_key found_key;
609 struct extent_buffer *leaf = NULL;
610 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400611
612 path = btrfs_alloc_path();
613 if (!path)
614 return -ENOMEM;
615
616 key.objectid = device->devid;
617 key.offset = start;
618 key.type = BTRFS_DEV_EXTENT_KEY;
619
620 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400621 if (ret > 0) {
622 ret = btrfs_previous_item(root, path, key.objectid,
623 BTRFS_DEV_EXTENT_KEY);
624 BUG_ON(ret);
625 leaf = path->nodes[0];
626 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
627 extent = btrfs_item_ptr(leaf, path->slots[0],
628 struct btrfs_dev_extent);
629 BUG_ON(found_key.offset > start || found_key.offset +
630 btrfs_dev_extent_length(leaf, extent) < start);
631 ret = 0;
632 } else if (ret == 0) {
633 leaf = path->nodes[0];
634 extent = btrfs_item_ptr(leaf, path->slots[0],
635 struct btrfs_dev_extent);
636 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400637 BUG_ON(ret);
638
Chris Masondfe25022008-05-13 13:46:40 -0400639 if (device->bytes_used > 0)
640 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400641 ret = btrfs_del_item(trans, root, path);
642 BUG_ON(ret);
643
644 btrfs_free_path(path);
645 return ret;
646}
647
Chris Masona1b32a52008-09-05 16:09:51 -0400648int noinline btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400649 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400650 u64 chunk_tree, u64 chunk_objectid,
651 u64 chunk_offset,
652 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400653{
654 int ret;
655 struct btrfs_path *path;
656 struct btrfs_root *root = device->dev_root;
657 struct btrfs_dev_extent *extent;
658 struct extent_buffer *leaf;
659 struct btrfs_key key;
660
Chris Masondfe25022008-05-13 13:46:40 -0400661 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400662 path = btrfs_alloc_path();
663 if (!path)
664 return -ENOMEM;
665
666 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
Chris Mason6324fbf2008-03-24 15:01:59 -0400667 if (ret) {
Chris Mason0b86a832008-03-24 15:01:56 -0400668 goto err;
Chris Mason6324fbf2008-03-24 15:01:59 -0400669 }
Chris Mason0b86a832008-03-24 15:01:56 -0400670
671 key.objectid = device->devid;
672 key.offset = *start;
673 key.type = BTRFS_DEV_EXTENT_KEY;
674 ret = btrfs_insert_empty_item(trans, root, path, &key,
675 sizeof(*extent));
676 BUG_ON(ret);
677
678 leaf = path->nodes[0];
679 extent = btrfs_item_ptr(leaf, path->slots[0],
680 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400681 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
682 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
683 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
684
685 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
686 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
687 BTRFS_UUID_SIZE);
688
Chris Mason0b86a832008-03-24 15:01:56 -0400689 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
690 btrfs_mark_buffer_dirty(leaf);
691err:
692 btrfs_free_path(path);
693 return ret;
694}
695
Chris Masona1b32a52008-09-05 16:09:51 -0400696static noinline int find_next_chunk(struct btrfs_root *root,
697 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400698{
699 struct btrfs_path *path;
700 int ret;
701 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400702 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400703 struct btrfs_key found_key;
704
705 path = btrfs_alloc_path();
706 BUG_ON(!path);
707
Chris Masone17cade2008-04-15 15:41:47 -0400708 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400709 key.offset = (u64)-1;
710 key.type = BTRFS_CHUNK_ITEM_KEY;
711
712 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
713 if (ret < 0)
714 goto error;
715
716 BUG_ON(ret == 0);
717
718 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
719 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400720 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400721 } else {
722 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
723 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400724 if (found_key.objectid != objectid)
725 *offset = 0;
726 else {
727 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
728 struct btrfs_chunk);
729 *offset = found_key.offset +
730 btrfs_chunk_length(path->nodes[0], chunk);
731 }
Chris Mason0b86a832008-03-24 15:01:56 -0400732 }
733 ret = 0;
734error:
735 btrfs_free_path(path);
736 return ret;
737}
738
Chris Masona1b32a52008-09-05 16:09:51 -0400739static noinline int find_next_devid(struct btrfs_root *root,
740 struct btrfs_path *path, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400741{
742 int ret;
743 struct btrfs_key key;
744 struct btrfs_key found_key;
745
746 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
747 key.type = BTRFS_DEV_ITEM_KEY;
748 key.offset = (u64)-1;
749
750 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
751 if (ret < 0)
752 goto error;
753
754 BUG_ON(ret == 0);
755
756 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
757 BTRFS_DEV_ITEM_KEY);
758 if (ret) {
759 *objectid = 1;
760 } else {
761 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
762 path->slots[0]);
763 *objectid = found_key.offset + 1;
764 }
765 ret = 0;
766error:
767 btrfs_release_path(root, path);
768 return ret;
769}
770
771/*
772 * the device information is stored in the chunk root
773 * the btrfs_device struct should be fully filled in
774 */
775int btrfs_add_device(struct btrfs_trans_handle *trans,
776 struct btrfs_root *root,
777 struct btrfs_device *device)
778{
779 int ret;
780 struct btrfs_path *path;
781 struct btrfs_dev_item *dev_item;
782 struct extent_buffer *leaf;
783 struct btrfs_key key;
784 unsigned long ptr;
Chris Mason006a58a2008-05-02 14:43:15 -0400785 u64 free_devid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400786
787 root = root->fs_info->chunk_root;
788
789 path = btrfs_alloc_path();
790 if (!path)
791 return -ENOMEM;
792
793 ret = find_next_devid(root, path, &free_devid);
794 if (ret)
795 goto out;
796
797 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
798 key.type = BTRFS_DEV_ITEM_KEY;
799 key.offset = free_devid;
800
801 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400802 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400803 if (ret)
804 goto out;
805
806 leaf = path->nodes[0];
807 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
808
Chris Mason8a4b83c2008-03-24 15:02:07 -0400809 device->devid = free_devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400810 btrfs_set_device_id(leaf, dev_item, device->devid);
811 btrfs_set_device_type(leaf, dev_item, device->type);
812 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
813 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
814 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400815 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
816 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400817 btrfs_set_device_group(leaf, dev_item, 0);
818 btrfs_set_device_seek_speed(leaf, dev_item, 0);
819 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400820
Chris Mason0b86a832008-03-24 15:01:56 -0400821 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400822 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400823 btrfs_mark_buffer_dirty(leaf);
824 ret = 0;
825
826out:
827 btrfs_free_path(path);
828 return ret;
829}
Chris Mason8f18cf12008-04-25 16:53:30 -0400830
Chris Masona061fc82008-05-07 11:43:44 -0400831static int btrfs_rm_dev_item(struct btrfs_root *root,
832 struct btrfs_device *device)
833{
834 int ret;
835 struct btrfs_path *path;
836 struct block_device *bdev = device->bdev;
837 struct btrfs_device *next_dev;
838 struct btrfs_key key;
839 u64 total_bytes;
840 struct btrfs_fs_devices *fs_devices;
841 struct btrfs_trans_handle *trans;
842
843 root = root->fs_info->chunk_root;
844
845 path = btrfs_alloc_path();
846 if (!path)
847 return -ENOMEM;
848
849 trans = btrfs_start_transaction(root, 1);
850 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
851 key.type = BTRFS_DEV_ITEM_KEY;
852 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -0400853 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400854
855 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
856 if (ret < 0)
857 goto out;
858
859 if (ret > 0) {
860 ret = -ENOENT;
861 goto out;
862 }
863
864 ret = btrfs_del_item(trans, root, path);
865 if (ret)
866 goto out;
867
868 /*
869 * at this point, the device is zero sized. We want to
870 * remove it from the devices list and zero out the old super
871 */
872 list_del_init(&device->dev_list);
873 list_del_init(&device->dev_alloc_list);
874 fs_devices = root->fs_info->fs_devices;
875
876 next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
877 dev_list);
Chris Masona061fc82008-05-07 11:43:44 -0400878 if (bdev == root->fs_info->sb->s_bdev)
879 root->fs_info->sb->s_bdev = next_dev->bdev;
880 if (bdev == fs_devices->latest_bdev)
881 fs_devices->latest_bdev = next_dev->bdev;
882
Chris Masona061fc82008-05-07 11:43:44 -0400883 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
884 btrfs_set_super_num_devices(&root->fs_info->super_copy,
885 total_bytes - 1);
886out:
887 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -0400888 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400889 btrfs_commit_transaction(trans, root);
890 return ret;
891}
892
893int btrfs_rm_device(struct btrfs_root *root, char *device_path)
894{
895 struct btrfs_device *device;
896 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400897 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -0400898 struct btrfs_super_block *disk_super;
899 u64 all_avail;
900 u64 devid;
901 int ret = 0;
902
Chris Masona061fc82008-05-07 11:43:44 -0400903 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -0400904 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -0400905
906 all_avail = root->fs_info->avail_data_alloc_bits |
907 root->fs_info->avail_system_alloc_bits |
908 root->fs_info->avail_metadata_alloc_bits;
909
910 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Chris Masondfe25022008-05-13 13:46:40 -0400911 btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
Chris Masona061fc82008-05-07 11:43:44 -0400912 printk("btrfs: unable to go below four devices on raid10\n");
913 ret = -EINVAL;
914 goto out;
915 }
916
917 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masondfe25022008-05-13 13:46:40 -0400918 btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
Chris Masona061fc82008-05-07 11:43:44 -0400919 printk("btrfs: unable to go below two devices on raid1\n");
920 ret = -EINVAL;
921 goto out;
922 }
923
Chris Masondfe25022008-05-13 13:46:40 -0400924 if (strcmp(device_path, "missing") == 0) {
925 struct list_head *cur;
926 struct list_head *devices;
927 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -0400928
Chris Masondfe25022008-05-13 13:46:40 -0400929 device = NULL;
930 devices = &root->fs_info->fs_devices->devices;
931 list_for_each(cur, devices) {
932 tmp = list_entry(cur, struct btrfs_device, dev_list);
933 if (tmp->in_fs_metadata && !tmp->bdev) {
934 device = tmp;
935 break;
936 }
937 }
938 bdev = NULL;
939 bh = NULL;
940 disk_super = NULL;
941 if (!device) {
942 printk("btrfs: no missing devices found to remove\n");
943 goto out;
944 }
Chris Masona061fc82008-05-07 11:43:44 -0400945
Chris Masondfe25022008-05-13 13:46:40 -0400946 } else {
947 bdev = open_bdev_excl(device_path, 0,
948 root->fs_info->bdev_holder);
949 if (IS_ERR(bdev)) {
950 ret = PTR_ERR(bdev);
951 goto out;
952 }
953
954 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
955 if (!bh) {
956 ret = -EIO;
957 goto error_close;
958 }
959 disk_super = (struct btrfs_super_block *)bh->b_data;
960 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
961 sizeof(disk_super->magic))) {
962 ret = -ENOENT;
963 goto error_brelse;
964 }
965 if (memcmp(disk_super->fsid, root->fs_info->fsid,
966 BTRFS_FSID_SIZE)) {
967 ret = -ENOENT;
968 goto error_brelse;
969 }
970 devid = le64_to_cpu(disk_super->dev_item.devid);
971 device = btrfs_find_device(root, devid, NULL);
972 if (!device) {
973 ret = -ENOENT;
974 goto error_brelse;
975 }
976
977 }
Chris Masona061fc82008-05-07 11:43:44 -0400978 root->fs_info->fs_devices->num_devices--;
Chris Mason0ef3e662008-05-24 14:04:53 -0400979 root->fs_info->fs_devices->open_devices--;
Chris Masona061fc82008-05-07 11:43:44 -0400980
981 ret = btrfs_shrink_device(device, 0);
982 if (ret)
983 goto error_brelse;
984
985
986 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
987 if (ret)
988 goto error_brelse;
989
Chris Masondfe25022008-05-13 13:46:40 -0400990 if (bh) {
991 /* make sure this device isn't detected as part of
992 * the FS anymore
993 */
994 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
995 set_buffer_dirty(bh);
996 sync_dirty_buffer(bh);
Chris Masona061fc82008-05-07 11:43:44 -0400997
Chris Masondfe25022008-05-13 13:46:40 -0400998 brelse(bh);
999 }
Chris Masona061fc82008-05-07 11:43:44 -04001000
Chris Masondfe25022008-05-13 13:46:40 -04001001 if (device->bdev) {
1002 /* one close for the device struct or super_block */
1003 close_bdev_excl(device->bdev);
1004 }
1005 if (bdev) {
1006 /* one close for us */
1007 close_bdev_excl(bdev);
1008 }
Chris Masona061fc82008-05-07 11:43:44 -04001009 kfree(device->name);
1010 kfree(device);
1011 ret = 0;
1012 goto out;
1013
1014error_brelse:
1015 brelse(bh);
1016error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001017 if (bdev)
1018 close_bdev_excl(bdev);
Chris Masona061fc82008-05-07 11:43:44 -04001019out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001020 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001021 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001022 return ret;
1023}
1024
Chris Mason788f20e2008-04-28 15:29:42 -04001025int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1026{
1027 struct btrfs_trans_handle *trans;
1028 struct btrfs_device *device;
1029 struct block_device *bdev;
1030 struct list_head *cur;
1031 struct list_head *devices;
1032 u64 total_bytes;
1033 int ret = 0;
1034
1035
1036 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
1037 if (!bdev) {
1038 return -EIO;
1039 }
Chris Masona2135012008-06-25 16:01:30 -04001040
Chris Mason7d9eb122008-07-08 14:19:17 -04001041 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001042
Chris Mason788f20e2008-04-28 15:29:42 -04001043 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04001044 lock_chunks(root);
Chris Mason788f20e2008-04-28 15:29:42 -04001045 devices = &root->fs_info->fs_devices->devices;
1046 list_for_each(cur, devices) {
1047 device = list_entry(cur, struct btrfs_device, dev_list);
1048 if (device->bdev == bdev) {
1049 ret = -EEXIST;
1050 goto out;
1051 }
1052 }
1053
1054 device = kzalloc(sizeof(*device), GFP_NOFS);
1055 if (!device) {
1056 /* we can safely leave the fs_devices entry around */
1057 ret = -ENOMEM;
1058 goto out_close_bdev;
1059 }
1060
1061 device->barriers = 1;
Chris Mason8b712842008-06-11 16:50:36 -04001062 device->work.func = pending_bios_fn;
Chris Mason788f20e2008-04-28 15:29:42 -04001063 generate_random_uuid(device->uuid);
1064 spin_lock_init(&device->io_lock);
1065 device->name = kstrdup(device_path, GFP_NOFS);
1066 if (!device->name) {
1067 kfree(device);
1068 goto out_close_bdev;
1069 }
1070 device->io_width = root->sectorsize;
1071 device->io_align = root->sectorsize;
1072 device->sector_size = root->sectorsize;
1073 device->total_bytes = i_size_read(bdev->bd_inode);
1074 device->dev_root = root->fs_info->dev_root;
1075 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001076 device->in_fs_metadata = 1;
Chris Mason788f20e2008-04-28 15:29:42 -04001077
1078 ret = btrfs_add_device(trans, root, device);
1079 if (ret)
1080 goto out_close_bdev;
1081
1082 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1083 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1084 total_bytes + device->total_bytes);
1085
1086 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1087 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1088 total_bytes + 1);
1089
1090 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1091 list_add(&device->dev_alloc_list,
1092 &root->fs_info->fs_devices->alloc_list);
1093 root->fs_info->fs_devices->num_devices++;
Chris Masona0af4692008-05-13 16:03:06 -04001094 root->fs_info->fs_devices->open_devices++;
Chris Mason788f20e2008-04-28 15:29:42 -04001095out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001096 unlock_chunks(root);
Chris Mason788f20e2008-04-28 15:29:42 -04001097 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04001098 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001099
Chris Mason788f20e2008-04-28 15:29:42 -04001100 return ret;
1101
1102out_close_bdev:
1103 close_bdev_excl(bdev);
1104 goto out;
1105}
1106
Chris Masona1b32a52008-09-05 16:09:51 -04001107int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
1108 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001109{
1110 int ret;
1111 struct btrfs_path *path;
1112 struct btrfs_root *root;
1113 struct btrfs_dev_item *dev_item;
1114 struct extent_buffer *leaf;
1115 struct btrfs_key key;
1116
1117 root = device->dev_root->fs_info->chunk_root;
1118
1119 path = btrfs_alloc_path();
1120 if (!path)
1121 return -ENOMEM;
1122
1123 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1124 key.type = BTRFS_DEV_ITEM_KEY;
1125 key.offset = device->devid;
1126
1127 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1128 if (ret < 0)
1129 goto out;
1130
1131 if (ret > 0) {
1132 ret = -ENOENT;
1133 goto out;
1134 }
1135
1136 leaf = path->nodes[0];
1137 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1138
1139 btrfs_set_device_id(leaf, dev_item, device->devid);
1140 btrfs_set_device_type(leaf, dev_item, device->type);
1141 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1142 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1143 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001144 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1145 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1146 btrfs_mark_buffer_dirty(leaf);
1147
1148out:
1149 btrfs_free_path(path);
1150 return ret;
1151}
1152
Chris Mason7d9eb122008-07-08 14:19:17 -04001153static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001154 struct btrfs_device *device, u64 new_size)
1155{
1156 struct btrfs_super_block *super_copy =
1157 &device->dev_root->fs_info->super_copy;
1158 u64 old_total = btrfs_super_total_bytes(super_copy);
1159 u64 diff = new_size - device->total_bytes;
1160
1161 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1162 return btrfs_update_device(trans, device);
1163}
1164
Chris Mason7d9eb122008-07-08 14:19:17 -04001165int btrfs_grow_device(struct btrfs_trans_handle *trans,
1166 struct btrfs_device *device, u64 new_size)
1167{
1168 int ret;
1169 lock_chunks(device->dev_root);
1170 ret = __btrfs_grow_device(trans, device, new_size);
1171 unlock_chunks(device->dev_root);
1172 return ret;
1173}
1174
Chris Mason8f18cf12008-04-25 16:53:30 -04001175static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root,
1177 u64 chunk_tree, u64 chunk_objectid,
1178 u64 chunk_offset)
1179{
1180 int ret;
1181 struct btrfs_path *path;
1182 struct btrfs_key key;
1183
1184 root = root->fs_info->chunk_root;
1185 path = btrfs_alloc_path();
1186 if (!path)
1187 return -ENOMEM;
1188
1189 key.objectid = chunk_objectid;
1190 key.offset = chunk_offset;
1191 key.type = BTRFS_CHUNK_ITEM_KEY;
1192
1193 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1194 BUG_ON(ret);
1195
1196 ret = btrfs_del_item(trans, root, path);
1197 BUG_ON(ret);
1198
1199 btrfs_free_path(path);
1200 return 0;
1201}
1202
1203int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1204 chunk_offset)
1205{
1206 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1207 struct btrfs_disk_key *disk_key;
1208 struct btrfs_chunk *chunk;
1209 u8 *ptr;
1210 int ret = 0;
1211 u32 num_stripes;
1212 u32 array_size;
1213 u32 len = 0;
1214 u32 cur;
1215 struct btrfs_key key;
1216
1217 array_size = btrfs_super_sys_array_size(super_copy);
1218
1219 ptr = super_copy->sys_chunk_array;
1220 cur = 0;
1221
1222 while (cur < array_size) {
1223 disk_key = (struct btrfs_disk_key *)ptr;
1224 btrfs_disk_key_to_cpu(&key, disk_key);
1225
1226 len = sizeof(*disk_key);
1227
1228 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1229 chunk = (struct btrfs_chunk *)(ptr + len);
1230 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1231 len += btrfs_chunk_item_size(num_stripes);
1232 } else {
1233 ret = -EIO;
1234 break;
1235 }
1236 if (key.objectid == chunk_objectid &&
1237 key.offset == chunk_offset) {
1238 memmove(ptr, ptr + len, array_size - (cur + len));
1239 array_size -= len;
1240 btrfs_set_super_sys_array_size(super_copy, array_size);
1241 } else {
1242 ptr += len;
1243 cur += len;
1244 }
1245 }
1246 return ret;
1247}
1248
1249
1250int btrfs_relocate_chunk(struct btrfs_root *root,
1251 u64 chunk_tree, u64 chunk_objectid,
1252 u64 chunk_offset)
1253{
1254 struct extent_map_tree *em_tree;
1255 struct btrfs_root *extent_root;
1256 struct btrfs_trans_handle *trans;
1257 struct extent_map *em;
1258 struct map_lookup *map;
1259 int ret;
1260 int i;
1261
Chris Mason323da792008-05-09 11:46:48 -04001262 printk("btrfs relocating chunk %llu\n",
1263 (unsigned long long)chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001264 root = root->fs_info->chunk_root;
1265 extent_root = root->fs_info->extent_root;
1266 em_tree = &root->fs_info->mapping_tree.map_tree;
1267
1268 /* step one, relocate all the extents inside this chunk */
1269 ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
1270 BUG_ON(ret);
1271
1272 trans = btrfs_start_transaction(root, 1);
1273 BUG_ON(!trans);
1274
Chris Mason7d9eb122008-07-08 14:19:17 -04001275 lock_chunks(root);
1276
Chris Mason8f18cf12008-04-25 16:53:30 -04001277 /*
1278 * step two, delete the device extents and the
1279 * chunk tree entries
1280 */
1281 spin_lock(&em_tree->lock);
1282 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1283 spin_unlock(&em_tree->lock);
1284
Chris Masona061fc82008-05-07 11:43:44 -04001285 BUG_ON(em->start > chunk_offset ||
1286 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001287 map = (struct map_lookup *)em->bdev;
1288
1289 for (i = 0; i < map->num_stripes; i++) {
1290 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1291 map->stripes[i].physical);
1292 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001293
Chris Masondfe25022008-05-13 13:46:40 -04001294 if (map->stripes[i].dev) {
1295 ret = btrfs_update_device(trans, map->stripes[i].dev);
1296 BUG_ON(ret);
1297 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001298 }
1299 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1300 chunk_offset);
1301
1302 BUG_ON(ret);
1303
1304 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1305 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1306 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001307 }
1308
Chris Mason8f18cf12008-04-25 16:53:30 -04001309 spin_lock(&em_tree->lock);
1310 remove_extent_mapping(em_tree, em);
1311 kfree(map);
1312 em->bdev = NULL;
1313
1314 /* once for the tree */
1315 free_extent_map(em);
1316 spin_unlock(&em_tree->lock);
1317
Chris Mason8f18cf12008-04-25 16:53:30 -04001318 /* once for us */
1319 free_extent_map(em);
1320
Chris Mason7d9eb122008-07-08 14:19:17 -04001321 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001322 btrfs_end_transaction(trans, root);
1323 return 0;
1324}
1325
Chris Masonec44a352008-04-28 15:29:52 -04001326static u64 div_factor(u64 num, int factor)
1327{
1328 if (factor == 10)
1329 return num;
1330 num *= factor;
1331 do_div(num, 10);
1332 return num;
1333}
1334
1335
1336int btrfs_balance(struct btrfs_root *dev_root)
1337{
1338 int ret;
1339 struct list_head *cur;
1340 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1341 struct btrfs_device *device;
1342 u64 old_size;
1343 u64 size_to_free;
1344 struct btrfs_path *path;
1345 struct btrfs_key key;
1346 struct btrfs_chunk *chunk;
1347 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1348 struct btrfs_trans_handle *trans;
1349 struct btrfs_key found_key;
1350
1351
Chris Mason7d9eb122008-07-08 14:19:17 -04001352 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001353 dev_root = dev_root->fs_info->dev_root;
1354
Chris Masonec44a352008-04-28 15:29:52 -04001355 /* step one make some room on all the devices */
1356 list_for_each(cur, devices) {
1357 device = list_entry(cur, struct btrfs_device, dev_list);
1358 old_size = device->total_bytes;
1359 size_to_free = div_factor(old_size, 1);
1360 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1361 if (device->total_bytes - device->bytes_used > size_to_free)
1362 continue;
1363
1364 ret = btrfs_shrink_device(device, old_size - size_to_free);
1365 BUG_ON(ret);
1366
1367 trans = btrfs_start_transaction(dev_root, 1);
1368 BUG_ON(!trans);
1369
1370 ret = btrfs_grow_device(trans, device, old_size);
1371 BUG_ON(ret);
1372
1373 btrfs_end_transaction(trans, dev_root);
1374 }
1375
1376 /* step two, relocate all the chunks */
1377 path = btrfs_alloc_path();
1378 BUG_ON(!path);
1379
1380 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1381 key.offset = (u64)-1;
1382 key.type = BTRFS_CHUNK_ITEM_KEY;
1383
1384 while(1) {
1385 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1386 if (ret < 0)
1387 goto error;
1388
1389 /*
1390 * this shouldn't happen, it means the last relocate
1391 * failed
1392 */
1393 if (ret == 0)
1394 break;
1395
1396 ret = btrfs_previous_item(chunk_root, path, 0,
1397 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001398 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001399 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001400
Chris Masonec44a352008-04-28 15:29:52 -04001401 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1402 path->slots[0]);
1403 if (found_key.objectid != key.objectid)
1404 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001405
Chris Masonec44a352008-04-28 15:29:52 -04001406 chunk = btrfs_item_ptr(path->nodes[0],
1407 path->slots[0],
1408 struct btrfs_chunk);
1409 key.offset = found_key.offset;
1410 /* chunk zero is special */
1411 if (key.offset == 0)
1412 break;
1413
Chris Mason7d9eb122008-07-08 14:19:17 -04001414 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001415 ret = btrfs_relocate_chunk(chunk_root,
1416 chunk_root->root_key.objectid,
1417 found_key.objectid,
1418 found_key.offset);
1419 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001420 }
1421 ret = 0;
1422error:
1423 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001424 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001425 return ret;
1426}
1427
Chris Mason8f18cf12008-04-25 16:53:30 -04001428/*
1429 * shrinking a device means finding all of the device extents past
1430 * the new size, and then following the back refs to the chunks.
1431 * The chunk relocation code actually frees the device extent
1432 */
1433int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1434{
1435 struct btrfs_trans_handle *trans;
1436 struct btrfs_root *root = device->dev_root;
1437 struct btrfs_dev_extent *dev_extent = NULL;
1438 struct btrfs_path *path;
1439 u64 length;
1440 u64 chunk_tree;
1441 u64 chunk_objectid;
1442 u64 chunk_offset;
1443 int ret;
1444 int slot;
1445 struct extent_buffer *l;
1446 struct btrfs_key key;
1447 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1448 u64 old_total = btrfs_super_total_bytes(super_copy);
1449 u64 diff = device->total_bytes - new_size;
1450
1451
1452 path = btrfs_alloc_path();
1453 if (!path)
1454 return -ENOMEM;
1455
1456 trans = btrfs_start_transaction(root, 1);
1457 if (!trans) {
1458 ret = -ENOMEM;
1459 goto done;
1460 }
1461
1462 path->reada = 2;
1463
Chris Mason7d9eb122008-07-08 14:19:17 -04001464 lock_chunks(root);
1465
Chris Mason8f18cf12008-04-25 16:53:30 -04001466 device->total_bytes = new_size;
1467 ret = btrfs_update_device(trans, device);
1468 if (ret) {
Chris Mason7d9eb122008-07-08 14:19:17 -04001469 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001470 btrfs_end_transaction(trans, root);
1471 goto done;
1472 }
1473 WARN_ON(diff > old_total);
1474 btrfs_set_super_total_bytes(super_copy, old_total - diff);
Chris Mason7d9eb122008-07-08 14:19:17 -04001475 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001476 btrfs_end_transaction(trans, root);
1477
1478 key.objectid = device->devid;
1479 key.offset = (u64)-1;
1480 key.type = BTRFS_DEV_EXTENT_KEY;
1481
1482 while (1) {
1483 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1484 if (ret < 0)
1485 goto done;
1486
1487 ret = btrfs_previous_item(root, path, 0, key.type);
1488 if (ret < 0)
1489 goto done;
1490 if (ret) {
1491 ret = 0;
1492 goto done;
1493 }
1494
1495 l = path->nodes[0];
1496 slot = path->slots[0];
1497 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1498
1499 if (key.objectid != device->devid)
1500 goto done;
1501
1502 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1503 length = btrfs_dev_extent_length(l, dev_extent);
1504
1505 if (key.offset + length <= new_size)
1506 goto done;
1507
1508 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1509 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1510 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1511 btrfs_release_path(root, path);
1512
1513 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1514 chunk_offset);
1515 if (ret)
1516 goto done;
1517 }
1518
1519done:
1520 btrfs_free_path(path);
1521 return ret;
1522}
1523
Chris Mason0b86a832008-03-24 15:01:56 -04001524int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1525 struct btrfs_root *root,
1526 struct btrfs_key *key,
1527 struct btrfs_chunk *chunk, int item_size)
1528{
1529 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1530 struct btrfs_disk_key disk_key;
1531 u32 array_size;
1532 u8 *ptr;
1533
1534 array_size = btrfs_super_sys_array_size(super_copy);
1535 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1536 return -EFBIG;
1537
1538 ptr = super_copy->sys_chunk_array + array_size;
1539 btrfs_cpu_key_to_disk(&disk_key, key);
1540 memcpy(ptr, &disk_key, sizeof(disk_key));
1541 ptr += sizeof(disk_key);
1542 memcpy(ptr, chunk, item_size);
1543 item_size += sizeof(disk_key);
1544 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1545 return 0;
1546}
1547
Chris Masona1b32a52008-09-05 16:09:51 -04001548static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
1549 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04001550{
1551 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1552 return calc_size;
1553 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1554 return calc_size * (num_stripes / sub_stripes);
1555 else
1556 return calc_size * num_stripes;
1557}
1558
1559
Chris Mason0b86a832008-03-24 15:01:56 -04001560int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1561 struct btrfs_root *extent_root, u64 *start,
Chris Mason6324fbf2008-03-24 15:01:59 -04001562 u64 *num_bytes, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001563{
1564 u64 dev_offset;
Chris Mason593060d2008-03-25 16:50:33 -04001565 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001566 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001567 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -04001568 struct btrfs_stripe *stripes;
1569 struct btrfs_device *device = NULL;
1570 struct btrfs_chunk *chunk;
Chris Mason6324fbf2008-03-24 15:01:59 -04001571 struct list_head private_devs;
Chris Masonb3075712008-04-22 09:22:07 -04001572 struct list_head *dev_list;
Chris Mason6324fbf2008-03-24 15:01:59 -04001573 struct list_head *cur;
Chris Mason0b86a832008-03-24 15:01:56 -04001574 struct extent_map_tree *em_tree;
1575 struct map_lookup *map;
1576 struct extent_map *em;
Chris Masona40a90a2008-04-18 11:55:51 -04001577 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001578 u64 physical;
1579 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001580 u64 max_chunk_size = calc_size;
1581 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001582 u64 avail;
1583 u64 max_avail = 0;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001584 u64 percent_max;
Chris Mason6324fbf2008-03-24 15:01:59 -04001585 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001586 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001587 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001588 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001589 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001590 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001591 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001592 struct btrfs_key key;
1593
Chris Masonec44a352008-04-28 15:29:52 -04001594 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1595 (type & BTRFS_BLOCK_GROUP_DUP)) {
1596 WARN_ON(1);
1597 type &= ~BTRFS_BLOCK_GROUP_DUP;
1598 }
Chris Masonb3075712008-04-22 09:22:07 -04001599 dev_list = &extent_root->fs_info->fs_devices->alloc_list;
Chris Mason6324fbf2008-03-24 15:01:59 -04001600 if (list_empty(dev_list))
1601 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04001602
Chris Masona40a90a2008-04-18 11:55:51 -04001603 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001604 num_stripes = extent_root->fs_info->fs_devices->open_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04001605 min_stripes = 2;
1606 }
1607 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04001608 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001609 min_stripes = 2;
1610 }
Chris Mason8790d502008-04-03 16:29:03 -04001611 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1612 num_stripes = min_t(u64, 2,
Chris Mason0ef3e662008-05-24 14:04:53 -04001613 extent_root->fs_info->fs_devices->open_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001614 if (num_stripes < 2)
1615 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04001616 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04001617 }
Chris Mason321aecc2008-04-16 10:49:51 -04001618 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001619 num_stripes = extent_root->fs_info->fs_devices->open_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04001620 if (num_stripes < 4)
1621 return -ENOSPC;
1622 num_stripes &= ~(u32)1;
1623 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001624 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04001625 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04001626
1627 if (type & BTRFS_BLOCK_GROUP_DATA) {
1628 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001629 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001630 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1631 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001632 min_stripe_size = 32 * 1024 * 1024;
1633 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1634 calc_size = 8 * 1024 * 1024;
1635 max_chunk_size = calc_size * 2;
1636 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001637 }
1638
Chris Mason8f18cf12008-04-25 16:53:30 -04001639 path = btrfs_alloc_path();
1640 if (!path)
1641 return -ENOMEM;
1642
Chris Mason9b3f68b2008-04-18 10:29:51 -04001643 /* we don't want a chunk larger than 10% of the FS */
1644 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
1645 max_chunk_size = min(percent_max, max_chunk_size);
1646
Chris Masona40a90a2008-04-18 11:55:51 -04001647again:
Chris Mason9b3f68b2008-04-18 10:29:51 -04001648 if (calc_size * num_stripes > max_chunk_size) {
1649 calc_size = max_chunk_size;
1650 do_div(calc_size, num_stripes);
1651 do_div(calc_size, stripe_len);
1652 calc_size *= stripe_len;
1653 }
1654 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04001655 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001656
Chris Mason9b3f68b2008-04-18 10:29:51 -04001657 do_div(calc_size, stripe_len);
1658 calc_size *= stripe_len;
1659
Chris Mason6324fbf2008-03-24 15:01:59 -04001660 INIT_LIST_HEAD(&private_devs);
1661 cur = dev_list->next;
1662 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04001663
1664 if (type & BTRFS_BLOCK_GROUP_DUP)
1665 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001666 else
1667 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04001668
Chris Masonad5bd912008-04-21 08:28:10 -04001669 /* we add 1MB because we never use the first 1MB of the device */
1670 min_free += 1024 * 1024;
1671
Chris Mason6324fbf2008-03-24 15:01:59 -04001672 /* build a private list of devices we will allocate from */
1673 while(index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04001674 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Chris Mason611f0e02008-04-03 16:29:03 -04001675
Chris Masondfe25022008-05-13 13:46:40 -04001676 if (device->total_bytes > device->bytes_used)
1677 avail = device->total_bytes - device->bytes_used;
1678 else
1679 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001680 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04001681
Chris Masondfe25022008-05-13 13:46:40 -04001682 if (device->in_fs_metadata && avail >= min_free) {
Chris Mason8f18cf12008-04-25 16:53:30 -04001683 u64 ignored_start = 0;
1684 ret = find_free_dev_extent(trans, device, path,
1685 min_free,
1686 &ignored_start);
1687 if (ret == 0) {
1688 list_move_tail(&device->dev_alloc_list,
1689 &private_devs);
Chris Mason611f0e02008-04-03 16:29:03 -04001690 index++;
Chris Mason8f18cf12008-04-25 16:53:30 -04001691 if (type & BTRFS_BLOCK_GROUP_DUP)
1692 index++;
1693 }
Chris Masondfe25022008-05-13 13:46:40 -04001694 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04001695 max_avail = avail;
Chris Mason6324fbf2008-03-24 15:01:59 -04001696 if (cur == dev_list)
1697 break;
1698 }
1699 if (index < num_stripes) {
1700 list_splice(&private_devs, dev_list);
Chris Masona40a90a2008-04-18 11:55:51 -04001701 if (index >= min_stripes) {
1702 num_stripes = index;
1703 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1704 num_stripes /= sub_stripes;
1705 num_stripes *= sub_stripes;
1706 }
1707 looped = 1;
1708 goto again;
1709 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001710 if (!looped && max_avail > 0) {
1711 looped = 1;
1712 calc_size = max_avail;
1713 goto again;
1714 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001715 btrfs_free_path(path);
Chris Mason6324fbf2008-03-24 15:01:59 -04001716 return -ENOSPC;
1717 }
Chris Masone17cade2008-04-15 15:41:47 -04001718 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1719 key.type = BTRFS_CHUNK_ITEM_KEY;
1720 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1721 &key.offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001722 if (ret) {
1723 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001724 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001725 }
Chris Mason0b86a832008-03-24 15:01:56 -04001726
Chris Mason0b86a832008-03-24 15:01:56 -04001727 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
Chris Mason8f18cf12008-04-25 16:53:30 -04001728 if (!chunk) {
1729 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001730 return -ENOMEM;
Chris Mason8f18cf12008-04-25 16:53:30 -04001731 }
Chris Mason0b86a832008-03-24 15:01:56 -04001732
Chris Mason593060d2008-03-25 16:50:33 -04001733 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1734 if (!map) {
1735 kfree(chunk);
Chris Mason8f18cf12008-04-25 16:53:30 -04001736 btrfs_free_path(path);
Chris Mason593060d2008-03-25 16:50:33 -04001737 return -ENOMEM;
1738 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001739 btrfs_free_path(path);
1740 path = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04001741
Chris Mason0b86a832008-03-24 15:01:56 -04001742 stripes = &chunk->stripe;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001743 *num_bytes = chunk_bytes_by_type(type, calc_size,
1744 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04001745
Chris Mason6324fbf2008-03-24 15:01:59 -04001746 index = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001747 while(index < num_stripes) {
Chris Masone17cade2008-04-15 15:41:47 -04001748 struct btrfs_stripe *stripe;
Chris Mason6324fbf2008-03-24 15:01:59 -04001749 BUG_ON(list_empty(&private_devs));
1750 cur = private_devs.next;
Chris Masonb3075712008-04-22 09:22:07 -04001751 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Chris Mason611f0e02008-04-03 16:29:03 -04001752
1753 /* loop over this device again if we're doing a dup group */
1754 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1755 (index == num_stripes - 1))
Chris Masonb3075712008-04-22 09:22:07 -04001756 list_move_tail(&device->dev_alloc_list, dev_list);
Chris Mason0b86a832008-03-24 15:01:56 -04001757
1758 ret = btrfs_alloc_dev_extent(trans, device,
Chris Masone17cade2008-04-15 15:41:47 -04001759 info->chunk_root->root_key.objectid,
1760 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1761 calc_size, &dev_offset);
Chris Mason0b86a832008-03-24 15:01:56 -04001762 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001763 device->bytes_used += calc_size;
1764 ret = btrfs_update_device(trans, device);
1765 BUG_ON(ret);
1766
Chris Mason593060d2008-03-25 16:50:33 -04001767 map->stripes[index].dev = device;
1768 map->stripes[index].physical = dev_offset;
Chris Masone17cade2008-04-15 15:41:47 -04001769 stripe = stripes + index;
1770 btrfs_set_stack_stripe_devid(stripe, device->devid);
1771 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1772 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001773 physical = dev_offset;
1774 index++;
1775 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001776 BUG_ON(!list_empty(&private_devs));
Chris Mason0b86a832008-03-24 15:01:56 -04001777
Chris Masone17cade2008-04-15 15:41:47 -04001778 /* key was set above */
1779 btrfs_set_stack_chunk_length(chunk, *num_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001780 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
Chris Mason593060d2008-03-25 16:50:33 -04001781 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -04001782 btrfs_set_stack_chunk_type(chunk, type);
1783 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001784 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1785 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -04001786 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
Chris Mason321aecc2008-04-16 10:49:51 -04001787 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001788 map->sector_size = extent_root->sectorsize;
1789 map->stripe_len = stripe_len;
1790 map->io_align = stripe_len;
1791 map->io_width = stripe_len;
1792 map->type = type;
1793 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001794 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04001795
1796 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1797 btrfs_chunk_item_size(num_stripes));
1798 BUG_ON(ret);
Chris Masone17cade2008-04-15 15:41:47 -04001799 *start = key.offset;;
Chris Mason0b86a832008-03-24 15:01:56 -04001800
1801 em = alloc_extent_map(GFP_NOFS);
1802 if (!em)
1803 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001804 em->bdev = (struct block_device *)map;
Chris Masone17cade2008-04-15 15:41:47 -04001805 em->start = key.offset;
1806 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04001807 em->block_start = 0;
1808
Chris Mason8f18cf12008-04-25 16:53:30 -04001809 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1810 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1811 chunk, btrfs_chunk_item_size(num_stripes));
1812 BUG_ON(ret);
1813 }
Chris Mason0b86a832008-03-24 15:01:56 -04001814 kfree(chunk);
1815
1816 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
1817 spin_lock(&em_tree->lock);
1818 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04001819 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04001820 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001821 free_extent_map(em);
1822 return ret;
1823}
1824
1825void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1826{
1827 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
1828}
1829
1830void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
1831{
1832 struct extent_map *em;
1833
1834 while(1) {
1835 spin_lock(&tree->map_tree.lock);
1836 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
1837 if (em)
1838 remove_extent_mapping(&tree->map_tree, em);
1839 spin_unlock(&tree->map_tree.lock);
1840 if (!em)
1841 break;
1842 kfree(em->bdev);
1843 /* once for us */
1844 free_extent_map(em);
1845 /* once for the tree */
1846 free_extent_map(em);
1847 }
1848}
1849
Chris Masonf1885912008-04-09 16:28:12 -04001850int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1851{
1852 struct extent_map *em;
1853 struct map_lookup *map;
1854 struct extent_map_tree *em_tree = &map_tree->map_tree;
1855 int ret;
1856
1857 spin_lock(&em_tree->lock);
1858 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04001859 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04001860 BUG_ON(!em);
1861
1862 BUG_ON(em->start > logical || em->start + em->len < logical);
1863 map = (struct map_lookup *)em->bdev;
1864 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1865 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001866 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1867 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001868 else
1869 ret = 1;
1870 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04001871 return ret;
1872}
1873
Chris Masondfe25022008-05-13 13:46:40 -04001874static int find_live_mirror(struct map_lookup *map, int first, int num,
1875 int optimal)
1876{
1877 int i;
1878 if (map->stripes[optimal].dev->bdev)
1879 return optimal;
1880 for (i = first; i < first + num; i++) {
1881 if (map->stripes[i].dev->bdev)
1882 return i;
1883 }
1884 /* we couldn't find one that doesn't fail. Just return something
1885 * and the io error handling code will clean up eventually
1886 */
1887 return optimal;
1888}
1889
Chris Masonf2d8d742008-04-21 10:03:05 -04001890static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1891 u64 logical, u64 *length,
1892 struct btrfs_multi_bio **multi_ret,
1893 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04001894{
1895 struct extent_map *em;
1896 struct map_lookup *map;
1897 struct extent_map_tree *em_tree = &map_tree->map_tree;
1898 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04001899 u64 stripe_offset;
1900 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04001901 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04001902 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04001903 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04001904 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04001905 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04001906 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04001907 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001908
Chris Masoncea9e442008-04-09 16:28:12 -04001909 if (multi_ret && !(rw & (1 << BIO_RW))) {
1910 stripes_allocated = 1;
1911 }
1912again:
1913 if (multi_ret) {
1914 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1915 GFP_NOFS);
1916 if (!multi)
1917 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04001918
1919 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001920 }
Chris Mason0b86a832008-03-24 15:01:56 -04001921
1922 spin_lock(&em_tree->lock);
1923 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04001924 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04001925
1926 if (!em && unplug_page)
1927 return 0;
1928
Chris Mason3b951512008-04-17 11:29:12 -04001929 if (!em) {
Chris Masona061fc82008-05-07 11:43:44 -04001930 printk("unable to find logical %Lu len %Lu\n", logical, *length);
Chris Masonf2d8d742008-04-21 10:03:05 -04001931 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04001932 }
Chris Mason0b86a832008-03-24 15:01:56 -04001933
1934 BUG_ON(em->start > logical || em->start + em->len < logical);
1935 map = (struct map_lookup *)em->bdev;
1936 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04001937
Chris Masonf1885912008-04-09 16:28:12 -04001938 if (mirror_num > map->num_stripes)
1939 mirror_num = 0;
1940
Chris Masoncea9e442008-04-09 16:28:12 -04001941 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04001942 if (rw & (1 << BIO_RW)) {
1943 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1944 BTRFS_BLOCK_GROUP_DUP)) {
1945 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04001946 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001947 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1948 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04001949 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001950 }
1951 }
1952 if (multi_ret && rw == WRITE &&
1953 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04001954 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04001955 free_extent_map(em);
1956 kfree(multi);
1957 goto again;
1958 }
Chris Mason593060d2008-03-25 16:50:33 -04001959 stripe_nr = offset;
1960 /*
1961 * stripe_nr counts the total number of stripes we have to stride
1962 * to get to this block
1963 */
1964 do_div(stripe_nr, map->stripe_len);
1965
1966 stripe_offset = stripe_nr * map->stripe_len;
1967 BUG_ON(offset < stripe_offset);
1968
1969 /* stripe_offset is the offset of this block in its stripe*/
1970 stripe_offset = offset - stripe_offset;
1971
Chris Masoncea9e442008-04-09 16:28:12 -04001972 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001973 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04001974 BTRFS_BLOCK_GROUP_DUP)) {
1975 /* we limit the length of each bio to what fits in a stripe */
1976 *length = min_t(u64, em->len - offset,
1977 map->stripe_len - stripe_offset);
1978 } else {
1979 *length = em->len - offset;
1980 }
Chris Masonf2d8d742008-04-21 10:03:05 -04001981
1982 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04001983 goto out;
1984
Chris Masonf2d8d742008-04-21 10:03:05 -04001985 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04001986 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001987 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04001988 if (unplug_page || (rw & (1 << BIO_RW)))
1989 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04001990 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04001991 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04001992 else {
1993 stripe_index = find_live_mirror(map, 0,
1994 map->num_stripes,
1995 current->pid % map->num_stripes);
1996 }
Chris Mason2fff7342008-04-29 14:12:09 -04001997
Chris Mason611f0e02008-04-03 16:29:03 -04001998 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04001999 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04002000 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002001 else if (mirror_num)
2002 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002003
Chris Mason321aecc2008-04-16 10:49:51 -04002004 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2005 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002006
2007 stripe_index = do_div(stripe_nr, factor);
2008 stripe_index *= map->sub_stripes;
2009
Chris Masonf2d8d742008-04-21 10:03:05 -04002010 if (unplug_page || (rw & (1 << BIO_RW)))
2011 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002012 else if (mirror_num)
2013 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002014 else {
2015 stripe_index = find_live_mirror(map, stripe_index,
2016 map->sub_stripes, stripe_index +
2017 current->pid % map->sub_stripes);
2018 }
Chris Mason8790d502008-04-03 16:29:03 -04002019 } else {
2020 /*
2021 * after this do_div call, stripe_nr is the number of stripes
2022 * on this device we have to walk to find the data, and
2023 * stripe_index is the number of our device in the stripe array
2024 */
2025 stripe_index = do_div(stripe_nr, map->num_stripes);
2026 }
Chris Mason593060d2008-03-25 16:50:33 -04002027 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002028
Chris Masonf2d8d742008-04-21 10:03:05 -04002029 for (i = 0; i < num_stripes; i++) {
2030 if (unplug_page) {
2031 struct btrfs_device *device;
2032 struct backing_dev_info *bdi;
2033
2034 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002035 if (device->bdev) {
2036 bdi = blk_get_backing_dev_info(device->bdev);
2037 if (bdi->unplug_io_fn) {
2038 bdi->unplug_io_fn(bdi, unplug_page);
2039 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002040 }
2041 } else {
2042 multi->stripes[i].physical =
2043 map->stripes[stripe_index].physical +
2044 stripe_offset + stripe_nr * map->stripe_len;
2045 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2046 }
Chris Masoncea9e442008-04-09 16:28:12 -04002047 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002048 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002049 if (multi_ret) {
2050 *multi_ret = multi;
2051 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002052 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002053 }
Chris Masoncea9e442008-04-09 16:28:12 -04002054out:
Chris Mason0b86a832008-03-24 15:01:56 -04002055 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002056 return 0;
2057}
2058
Chris Masonf2d8d742008-04-21 10:03:05 -04002059int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2060 u64 logical, u64 *length,
2061 struct btrfs_multi_bio **multi_ret, int mirror_num)
2062{
2063 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2064 mirror_num, NULL);
2065}
2066
2067int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2068 u64 logical, struct page *page)
2069{
2070 u64 length = PAGE_CACHE_SIZE;
2071 return __btrfs_map_block(map_tree, READ, logical, &length,
2072 NULL, 0, page);
2073}
2074
2075
Chris Mason8790d502008-04-03 16:29:03 -04002076#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2077static void end_bio_multi_stripe(struct bio *bio, int err)
2078#else
2079static int end_bio_multi_stripe(struct bio *bio,
2080 unsigned int bytes_done, int err)
2081#endif
2082{
Chris Masoncea9e442008-04-09 16:28:12 -04002083 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002084 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002085
2086#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2087 if (bio->bi_size)
2088 return 1;
2089#endif
2090 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002091 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002092
Chris Mason7d2b4da2008-08-05 10:13:57 -04002093 if (bio == multi->orig_bio)
2094 is_orig_bio = 1;
2095
Chris Masoncea9e442008-04-09 16:28:12 -04002096 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002097 if (!is_orig_bio) {
2098 bio_put(bio);
2099 bio = multi->orig_bio;
2100 }
Chris Mason8790d502008-04-03 16:29:03 -04002101 bio->bi_private = multi->private;
2102 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002103 /* only send an error to the higher layers if it is
2104 * beyond the tolerance of the multi-bio
2105 */
Chris Mason1259ab72008-05-12 13:39:03 -04002106 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002107 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002108 } else if (err) {
2109 /*
2110 * this bio is actually up to date, we didn't
2111 * go over the max number of errors
2112 */
2113 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002114 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002115 }
Chris Mason8790d502008-04-03 16:29:03 -04002116 kfree(multi);
2117
Miguel73f61b22008-04-11 15:50:59 -04002118#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2119 bio_endio(bio, bio->bi_size, err);
2120#else
Chris Mason8790d502008-04-03 16:29:03 -04002121 bio_endio(bio, err);
Miguel73f61b22008-04-11 15:50:59 -04002122#endif
Chris Mason7d2b4da2008-08-05 10:13:57 -04002123 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002124 bio_put(bio);
2125 }
2126#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2127 return 0;
2128#endif
2129}
2130
Chris Mason8b712842008-06-11 16:50:36 -04002131struct async_sched {
2132 struct bio *bio;
2133 int rw;
2134 struct btrfs_fs_info *info;
2135 struct btrfs_work work;
2136};
2137
2138/*
2139 * see run_scheduled_bios for a description of why bios are collected for
2140 * async submit.
2141 *
2142 * This will add one bio to the pending list for a device and make sure
2143 * the work struct is scheduled.
2144 */
Chris Masona1b32a52008-09-05 16:09:51 -04002145static int noinline schedule_bio(struct btrfs_root *root,
2146 struct btrfs_device *device,
2147 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002148{
2149 int should_queue = 1;
2150
2151 /* don't bother with additional async steps for reads, right now */
2152 if (!(rw & (1 << BIO_RW))) {
Chris Mason492bb6d2008-07-31 16:29:02 -04002153 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002154 submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04002155 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002156 return 0;
2157 }
2158
2159 /*
Chris Mason0986fe92008-08-15 15:34:15 -04002160 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002161 * higher layers. Otherwise, the async bio makes it appear we have
2162 * made progress against dirty pages when we've really just put it
2163 * on a queue for later
2164 */
Chris Mason0986fe92008-08-15 15:34:15 -04002165 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04002166 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002167 bio->bi_next = NULL;
2168 bio->bi_rw |= rw;
2169
2170 spin_lock(&device->io_lock);
2171
2172 if (device->pending_bio_tail)
2173 device->pending_bio_tail->bi_next = bio;
2174
2175 device->pending_bio_tail = bio;
2176 if (!device->pending_bios)
2177 device->pending_bios = bio;
2178 if (device->running_pending)
2179 should_queue = 0;
2180
2181 spin_unlock(&device->io_lock);
2182
2183 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002184 btrfs_queue_worker(&root->fs_info->submit_workers,
2185 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002186 return 0;
2187}
2188
Chris Masonf1885912008-04-09 16:28:12 -04002189int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002190 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002191{
2192 struct btrfs_mapping_tree *map_tree;
2193 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002194 struct bio *first_bio = bio;
Chris Mason0b86a832008-03-24 15:01:56 -04002195 u64 logical = bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002196 u64 length = 0;
2197 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002198 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002199 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002200 int dev_nr = 0;
2201 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002202
Chris Masonf2d8d742008-04-21 10:03:05 -04002203 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002204 map_tree = &root->fs_info->mapping_tree;
2205 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002206
Chris Masonf1885912008-04-09 16:28:12 -04002207 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2208 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002209 BUG_ON(ret);
2210
2211 total_devs = multi->num_stripes;
2212 if (map_length < length) {
2213 printk("mapping failed logical %Lu bio len %Lu "
2214 "len %Lu\n", logical, length, map_length);
2215 BUG();
2216 }
2217 multi->end_io = first_bio->bi_end_io;
2218 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002219 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04002220 atomic_set(&multi->stripes_pending, multi->num_stripes);
2221
Chris Mason8790d502008-04-03 16:29:03 -04002222 while(dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002223 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002224 if (dev_nr < total_devs - 1) {
2225 bio = bio_clone(first_bio, GFP_NOFS);
2226 BUG_ON(!bio);
2227 } else {
2228 bio = first_bio;
2229 }
2230 bio->bi_private = multi;
2231 bio->bi_end_io = end_bio_multi_stripe;
2232 }
Chris Masoncea9e442008-04-09 16:28:12 -04002233 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2234 dev = multi->stripes[dev_nr].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002235 if (dev && dev->bdev) {
2236 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002237 if (async_submit)
2238 schedule_bio(root, dev, rw, bio);
2239 else
2240 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002241 } else {
2242 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2243 bio->bi_sector = logical >> 9;
2244#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2245 bio_endio(bio, bio->bi_size, -EIO);
2246#else
2247 bio_endio(bio, -EIO);
2248#endif
2249 }
Chris Mason8790d502008-04-03 16:29:03 -04002250 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002251 }
Chris Masoncea9e442008-04-09 16:28:12 -04002252 if (total_devs == 1)
2253 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002254 return 0;
2255}
2256
Chris Masona4437552008-04-18 10:29:38 -04002257struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2258 u8 *uuid)
Chris Mason0b86a832008-03-24 15:01:56 -04002259{
Chris Mason8a4b83c2008-03-24 15:02:07 -04002260 struct list_head *head = &root->fs_info->fs_devices->devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002261
Chris Masona4437552008-04-18 10:29:38 -04002262 return __find_device(head, devid, uuid);
Chris Mason0b86a832008-03-24 15:01:56 -04002263}
2264
Chris Masondfe25022008-05-13 13:46:40 -04002265static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2266 u64 devid, u8 *dev_uuid)
2267{
2268 struct btrfs_device *device;
2269 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2270
2271 device = kzalloc(sizeof(*device), GFP_NOFS);
2272 list_add(&device->dev_list,
2273 &fs_devices->devices);
2274 list_add(&device->dev_alloc_list,
2275 &fs_devices->alloc_list);
2276 device->barriers = 1;
2277 device->dev_root = root->fs_info->dev_root;
2278 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04002279 device->work.func = pending_bios_fn;
Chris Masondfe25022008-05-13 13:46:40 -04002280 fs_devices->num_devices++;
2281 spin_lock_init(&device->io_lock);
2282 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2283 return device;
2284}
2285
2286
Chris Mason0b86a832008-03-24 15:01:56 -04002287static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2288 struct extent_buffer *leaf,
2289 struct btrfs_chunk *chunk)
2290{
2291 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2292 struct map_lookup *map;
2293 struct extent_map *em;
2294 u64 logical;
2295 u64 length;
2296 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04002297 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04002298 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002299 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04002300 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04002301
Chris Masone17cade2008-04-15 15:41:47 -04002302 logical = key->offset;
2303 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04002304
Chris Mason0b86a832008-03-24 15:01:56 -04002305 spin_lock(&map_tree->map_tree.lock);
2306 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04002307 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002308
2309 /* already mapped? */
2310 if (em && em->start <= logical && em->start + em->len > logical) {
2311 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002312 return 0;
2313 } else if (em) {
2314 free_extent_map(em);
2315 }
Chris Mason0b86a832008-03-24 15:01:56 -04002316
2317 map = kzalloc(sizeof(*map), GFP_NOFS);
2318 if (!map)
2319 return -ENOMEM;
2320
2321 em = alloc_extent_map(GFP_NOFS);
2322 if (!em)
2323 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04002324 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2325 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04002326 if (!map) {
2327 free_extent_map(em);
2328 return -ENOMEM;
2329 }
2330
2331 em->bdev = (struct block_device *)map;
2332 em->start = logical;
2333 em->len = length;
2334 em->block_start = 0;
2335
Chris Mason593060d2008-03-25 16:50:33 -04002336 map->num_stripes = num_stripes;
2337 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2338 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2339 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2340 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2341 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04002342 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04002343 for (i = 0; i < num_stripes; i++) {
2344 map->stripes[i].physical =
2345 btrfs_stripe_offset_nr(leaf, chunk, i);
2346 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04002347 read_extent_buffer(leaf, uuid, (unsigned long)
2348 btrfs_stripe_dev_uuid_nr(chunk, i),
2349 BTRFS_UUID_SIZE);
2350 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04002351
2352 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04002353 kfree(map);
2354 free_extent_map(em);
2355 return -EIO;
2356 }
Chris Masondfe25022008-05-13 13:46:40 -04002357 if (!map->stripes[i].dev) {
2358 map->stripes[i].dev =
2359 add_missing_dev(root, devid, uuid);
2360 if (!map->stripes[i].dev) {
2361 kfree(map);
2362 free_extent_map(em);
2363 return -EIO;
2364 }
2365 }
2366 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002367 }
2368
2369 spin_lock(&map_tree->map_tree.lock);
2370 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002371 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04002372 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002373 free_extent_map(em);
2374
2375 return 0;
2376}
2377
2378static int fill_device_from_item(struct extent_buffer *leaf,
2379 struct btrfs_dev_item *dev_item,
2380 struct btrfs_device *device)
2381{
2382 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04002383
2384 device->devid = btrfs_device_id(leaf, dev_item);
2385 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2386 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2387 device->type = btrfs_device_type(leaf, dev_item);
2388 device->io_align = btrfs_device_io_align(leaf, dev_item);
2389 device->io_width = btrfs_device_io_width(leaf, dev_item);
2390 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04002391
2392 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04002393 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002394
Chris Mason0b86a832008-03-24 15:01:56 -04002395 return 0;
2396}
2397
Chris Mason0d81ba52008-03-24 15:02:07 -04002398static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04002399 struct extent_buffer *leaf,
2400 struct btrfs_dev_item *dev_item)
2401{
2402 struct btrfs_device *device;
2403 u64 devid;
2404 int ret;
Chris Masona4437552008-04-18 10:29:38 -04002405 u8 dev_uuid[BTRFS_UUID_SIZE];
2406
Chris Mason0b86a832008-03-24 15:01:56 -04002407 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04002408 read_extent_buffer(leaf, dev_uuid,
2409 (unsigned long)btrfs_device_uuid(dev_item),
2410 BTRFS_UUID_SIZE);
2411 device = btrfs_find_device(root, devid, dev_uuid);
Chris Mason6324fbf2008-03-24 15:01:59 -04002412 if (!device) {
Chris Masondfe25022008-05-13 13:46:40 -04002413 printk("warning devid %Lu missing\n", devid);
2414 device = add_missing_dev(root, devid, dev_uuid);
Chris Mason6324fbf2008-03-24 15:01:59 -04002415 if (!device)
2416 return -ENOMEM;
Chris Mason6324fbf2008-03-24 15:01:59 -04002417 }
Chris Mason0b86a832008-03-24 15:01:56 -04002418
2419 fill_device_from_item(leaf, dev_item, device);
2420 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04002421 device->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002422 ret = 0;
2423#if 0
2424 ret = btrfs_open_device(device);
2425 if (ret) {
2426 kfree(device);
2427 }
2428#endif
2429 return ret;
2430}
2431
Chris Mason0d81ba52008-03-24 15:02:07 -04002432int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
2433{
2434 struct btrfs_dev_item *dev_item;
2435
2436 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
2437 dev_item);
2438 return read_one_dev(root, buf, dev_item);
2439}
2440
Chris Mason0b86a832008-03-24 15:01:56 -04002441int btrfs_read_sys_array(struct btrfs_root *root)
2442{
2443 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04002444 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04002445 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04002446 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04002447 u8 *ptr;
2448 unsigned long sb_ptr;
2449 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002450 u32 num_stripes;
2451 u32 array_size;
2452 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002453 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04002454 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04002455
Chris Masona061fc82008-05-07 11:43:44 -04002456 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
2457 BTRFS_SUPER_INFO_SIZE);
2458 if (!sb)
2459 return -ENOMEM;
2460 btrfs_set_buffer_uptodate(sb);
2461 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002462 array_size = btrfs_super_sys_array_size(super_copy);
2463
Chris Mason0b86a832008-03-24 15:01:56 -04002464 ptr = super_copy->sys_chunk_array;
2465 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
2466 cur = 0;
2467
2468 while (cur < array_size) {
2469 disk_key = (struct btrfs_disk_key *)ptr;
2470 btrfs_disk_key_to_cpu(&key, disk_key);
2471
Chris Masona061fc82008-05-07 11:43:44 -04002472 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04002473 sb_ptr += len;
2474 cur += len;
2475
Chris Mason0d81ba52008-03-24 15:02:07 -04002476 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04002477 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04002478 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04002479 if (ret)
2480 break;
Chris Mason0b86a832008-03-24 15:01:56 -04002481 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
2482 len = btrfs_chunk_item_size(num_stripes);
2483 } else {
Chris Mason84eed902008-04-25 09:04:37 -04002484 ret = -EIO;
2485 break;
Chris Mason0b86a832008-03-24 15:01:56 -04002486 }
2487 ptr += len;
2488 sb_ptr += len;
2489 cur += len;
2490 }
Chris Masona061fc82008-05-07 11:43:44 -04002491 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04002492 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04002493}
2494
2495int btrfs_read_chunk_tree(struct btrfs_root *root)
2496{
2497 struct btrfs_path *path;
2498 struct extent_buffer *leaf;
2499 struct btrfs_key key;
2500 struct btrfs_key found_key;
2501 int ret;
2502 int slot;
2503
2504 root = root->fs_info->chunk_root;
2505
2506 path = btrfs_alloc_path();
2507 if (!path)
2508 return -ENOMEM;
2509
2510 /* first we search for all of the device items, and then we
2511 * read in all of the chunk items. This way we can create chunk
2512 * mappings that reference all of the devices that are afound
2513 */
2514 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2515 key.offset = 0;
2516 key.type = 0;
2517again:
2518 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2519 while(1) {
2520 leaf = path->nodes[0];
2521 slot = path->slots[0];
2522 if (slot >= btrfs_header_nritems(leaf)) {
2523 ret = btrfs_next_leaf(root, path);
2524 if (ret == 0)
2525 continue;
2526 if (ret < 0)
2527 goto error;
2528 break;
2529 }
2530 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2531 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2532 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
2533 break;
2534 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
2535 struct btrfs_dev_item *dev_item;
2536 dev_item = btrfs_item_ptr(leaf, slot,
2537 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04002538 ret = read_one_dev(root, leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04002539 BUG_ON(ret);
2540 }
2541 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
2542 struct btrfs_chunk *chunk;
2543 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2544 ret = read_one_chunk(root, &found_key, leaf, chunk);
2545 }
2546 path->slots[0]++;
2547 }
2548 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2549 key.objectid = 0;
2550 btrfs_release_path(root, path);
2551 goto again;
2552 }
2553
2554 btrfs_free_path(path);
2555 ret = 0;
2556error:
2557 return ret;
2558}