blob: 5e6ee7a6f73831d574ade1c09353b3a1b82441e3 [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 Masona4437552008-04-18 10:29:38 -040097static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
98 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
113static struct btrfs_fs_devices *find_fsid(u8 *fsid)
114{
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 */
137int run_scheduled_bios(struct btrfs_device *device)
138{
139 struct bio *pending;
140 struct backing_dev_info *bdi;
141 struct bio *tail;
142 struct bio *cur;
143 int again = 0;
144 unsigned long num_run = 0;
145
146 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
147loop:
148 spin_lock(&device->io_lock);
149
150 /* take all the bios off the list at once and process them
151 * later on (without the lock held). But, remember the
152 * tail and other pointers so the bios can be properly reinserted
153 * into the list if we hit congestion
154 */
155 pending = device->pending_bios;
156 tail = device->pending_bio_tail;
157 WARN_ON(pending && !tail);
158 device->pending_bios = NULL;
159 device->pending_bio_tail = NULL;
160
161 /*
162 * if pending was null this time around, no bios need processing
163 * at all and we can stop. Otherwise it'll loop back up again
164 * and do an additional check so no bios are missed.
165 *
166 * device->running_pending is used to synchronize with the
167 * schedule_bio code.
168 */
169 if (pending) {
170 again = 1;
171 device->running_pending = 1;
172 } else {
173 again = 0;
174 device->running_pending = 0;
175 }
176 spin_unlock(&device->io_lock);
177
178 while(pending) {
179 cur = pending;
180 pending = pending->bi_next;
181 cur->bi_next = NULL;
182 atomic_dec(&device->dev_root->fs_info->nr_async_submits);
183 submit_bio(cur->bi_rw, cur);
184 num_run++;
185
186 /*
187 * we made progress, there is more work to do and the bdi
188 * is now congested. Back off and let other work structs
189 * run instead
190 */
191 if (pending && num_run && bdi_write_congested(bdi)) {
192 struct bio *old_head;
193
194 spin_lock(&device->io_lock);
195 old_head = device->pending_bios;
196 device->pending_bios = pending;
197 if (device->pending_bio_tail)
198 tail->bi_next = old_head;
199 else
200 device->pending_bio_tail = tail;
201
202 spin_unlock(&device->io_lock);
203 btrfs_requeue_work(&device->work);
204 goto done;
205 }
206 }
207 if (again)
208 goto loop;
209done:
210 return 0;
211}
212
213void pending_bios_fn(struct btrfs_work *work)
214{
215 struct btrfs_device *device;
216
217 device = container_of(work, struct btrfs_device, work);
218 run_scheduled_bios(device);
219}
220
Chris Mason8a4b83c2008-03-24 15:02:07 -0400221static int device_list_add(const char *path,
222 struct btrfs_super_block *disk_super,
223 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
224{
225 struct btrfs_device *device;
226 struct btrfs_fs_devices *fs_devices;
227 u64 found_transid = btrfs_super_generation(disk_super);
228
229 fs_devices = find_fsid(disk_super->fsid);
230 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400231 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400232 if (!fs_devices)
233 return -ENOMEM;
234 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400235 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400236 list_add(&fs_devices->list, &fs_uuids);
237 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
238 fs_devices->latest_devid = devid;
239 fs_devices->latest_trans = found_transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400240 device = NULL;
241 } else {
Chris Masona4437552008-04-18 10:29:38 -0400242 device = __find_device(&fs_devices->devices, devid,
243 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400244 }
245 if (!device) {
246 device = kzalloc(sizeof(*device), GFP_NOFS);
247 if (!device) {
248 /* we can safely leave the fs_devices entry around */
249 return -ENOMEM;
250 }
251 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400252 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400253 memcpy(device->uuid, disk_super->dev_item.uuid,
254 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400255 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400256 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400257 device->name = kstrdup(path, GFP_NOFS);
258 if (!device->name) {
259 kfree(device);
260 return -ENOMEM;
261 }
262 list_add(&device->dev_list, &fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400263 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400264 fs_devices->num_devices++;
265 }
266
267 if (found_transid > fs_devices->latest_trans) {
268 fs_devices->latest_devid = devid;
269 fs_devices->latest_trans = found_transid;
270 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400271 *fs_devices_ret = fs_devices;
272 return 0;
273}
274
Chris Masondfe25022008-05-13 13:46:40 -0400275int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
276{
277 struct list_head *head = &fs_devices->devices;
278 struct list_head *cur;
279 struct btrfs_device *device;
280
281 mutex_lock(&uuid_mutex);
282again:
283 list_for_each(cur, head) {
284 device = list_entry(cur, struct btrfs_device, dev_list);
285 if (!device->in_fs_metadata) {
Chris Masona74a4b92008-06-25 16:01:31 -0400286 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400287 list_del(&device->dev_list);
288 list_del(&device->dev_alloc_list);
289 fs_devices->num_devices--;
Chris Masona74a4b92008-06-25 16:01:31 -0400290 if (device->bdev) {
291 bdev = device->bdev;
292 fs_devices->open_devices--;
293 mutex_unlock(&uuid_mutex);
294 close_bdev_excl(bdev);
295 mutex_lock(&uuid_mutex);
296 }
Chris Masondfe25022008-05-13 13:46:40 -0400297 kfree(device->name);
298 kfree(device);
299 goto again;
300 }
301 }
302 mutex_unlock(&uuid_mutex);
303 return 0;
304}
Chris Masona0af4692008-05-13 16:03:06 -0400305
Chris Mason8a4b83c2008-03-24 15:02:07 -0400306int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
307{
308 struct list_head *head = &fs_devices->devices;
309 struct list_head *cur;
310 struct btrfs_device *device;
311
312 mutex_lock(&uuid_mutex);
313 list_for_each(cur, head) {
314 device = list_entry(cur, struct btrfs_device, dev_list);
315 if (device->bdev) {
316 close_bdev_excl(device->bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400317 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400318 }
319 device->bdev = NULL;
Chris Masondfe25022008-05-13 13:46:40 -0400320 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400321 }
Chris Masona0af4692008-05-13 16:03:06 -0400322 fs_devices->mounted = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400323 mutex_unlock(&uuid_mutex);
324 return 0;
325}
326
327int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
328 int flags, void *holder)
329{
330 struct block_device *bdev;
331 struct list_head *head = &fs_devices->devices;
332 struct list_head *cur;
333 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400334 struct block_device *latest_bdev = NULL;
335 struct buffer_head *bh;
336 struct btrfs_super_block *disk_super;
337 u64 latest_devid = 0;
338 u64 latest_transid = 0;
339 u64 transid;
340 u64 devid;
341 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400342
343 mutex_lock(&uuid_mutex);
Chris Masona0af4692008-05-13 16:03:06 -0400344 if (fs_devices->mounted)
345 goto out;
346
Chris Mason8a4b83c2008-03-24 15:02:07 -0400347 list_for_each(cur, head) {
348 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Masonc1c4d912008-05-08 15:05:58 -0400349 if (device->bdev)
350 continue;
351
Chris Masondfe25022008-05-13 13:46:40 -0400352 if (!device->name)
353 continue;
354
Chris Mason8a4b83c2008-03-24 15:02:07 -0400355 bdev = open_bdev_excl(device->name, flags, holder);
Chris Masone17cade2008-04-15 15:41:47 -0400356
Chris Mason8a4b83c2008-03-24 15:02:07 -0400357 if (IS_ERR(bdev)) {
358 printk("open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400359 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400360 }
Chris Masona061fc82008-05-07 11:43:44 -0400361 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400362
363 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
364 if (!bh)
365 goto error_close;
366
367 disk_super = (struct btrfs_super_block *)bh->b_data;
368 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
369 sizeof(disk_super->magic)))
370 goto error_brelse;
371
372 devid = le64_to_cpu(disk_super->dev_item.devid);
373 if (devid != device->devid)
374 goto error_brelse;
375
376 transid = btrfs_super_generation(disk_super);
Chris Mason6af5ac32008-05-16 13:14:57 -0400377 if (!latest_transid || transid > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400378 latest_devid = devid;
379 latest_transid = transid;
380 latest_bdev = bdev;
381 }
382
Chris Mason8a4b83c2008-03-24 15:02:07 -0400383 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400384 device->in_fs_metadata = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400385 fs_devices->open_devices++;
386 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400387
Chris Masona0af4692008-05-13 16:03:06 -0400388error_brelse:
389 brelse(bh);
390error_close:
391 close_bdev_excl(bdev);
392error:
393 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400394 }
Chris Masona0af4692008-05-13 16:03:06 -0400395 if (fs_devices->open_devices == 0) {
396 ret = -EIO;
397 goto out;
398 }
399 fs_devices->mounted = 1;
400 fs_devices->latest_bdev = latest_bdev;
401 fs_devices->latest_devid = latest_devid;
402 fs_devices->latest_trans = latest_transid;
403out:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400404 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405 return ret;
406}
407
408int btrfs_scan_one_device(const char *path, int flags, void *holder,
409 struct btrfs_fs_devices **fs_devices_ret)
410{
411 struct btrfs_super_block *disk_super;
412 struct block_device *bdev;
413 struct buffer_head *bh;
414 int ret;
415 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400416 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400417
418 mutex_lock(&uuid_mutex);
419
Chris Mason8a4b83c2008-03-24 15:02:07 -0400420 bdev = open_bdev_excl(path, flags, holder);
421
422 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400423 ret = PTR_ERR(bdev);
424 goto error;
425 }
426
427 ret = set_blocksize(bdev, 4096);
428 if (ret)
429 goto error_close;
430 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
431 if (!bh) {
432 ret = -EIO;
433 goto error_close;
434 }
435 disk_super = (struct btrfs_super_block *)bh->b_data;
436 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
437 sizeof(disk_super->magic))) {
Yane58ca022008-04-01 11:21:34 -0400438 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400439 goto error_brelse;
440 }
441 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400442 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400443 if (disk_super->label[0])
444 printk("device label %s ", disk_super->label);
445 else {
446 /* FIXME, make a readl uuid parser */
447 printk("device fsid %llx-%llx ",
448 *(unsigned long long *)disk_super->fsid,
449 *(unsigned long long *)(disk_super->fsid + 8));
450 }
451 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400452 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
453
454error_brelse:
455 brelse(bh);
456error_close:
457 close_bdev_excl(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400458error:
459 mutex_unlock(&uuid_mutex);
460 return ret;
461}
Chris Mason0b86a832008-03-24 15:01:56 -0400462
463/*
464 * this uses a pretty simple search, the expectation is that it is
465 * called very infrequently and that a given device has a small number
466 * of extents
467 */
468static int find_free_dev_extent(struct btrfs_trans_handle *trans,
469 struct btrfs_device *device,
470 struct btrfs_path *path,
471 u64 num_bytes, u64 *start)
472{
473 struct btrfs_key key;
474 struct btrfs_root *root = device->dev_root;
475 struct btrfs_dev_extent *dev_extent = NULL;
476 u64 hole_size = 0;
477 u64 last_byte = 0;
478 u64 search_start = 0;
479 u64 search_end = device->total_bytes;
480 int ret;
481 int slot = 0;
482 int start_found;
483 struct extent_buffer *l;
484
485 start_found = 0;
486 path->reada = 2;
487
488 /* FIXME use last free of some kind */
489
Chris Mason8a4b83c2008-03-24 15:02:07 -0400490 /* we don't want to overwrite the superblock on the drive,
491 * so we make sure to start at an offset of at least 1MB
492 */
493 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400494
495 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
496 search_start = max(root->fs_info->alloc_start, search_start);
497
Chris Mason0b86a832008-03-24 15:01:56 -0400498 key.objectid = device->devid;
499 key.offset = search_start;
500 key.type = BTRFS_DEV_EXTENT_KEY;
501 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
502 if (ret < 0)
503 goto error;
504 ret = btrfs_previous_item(root, path, 0, key.type);
505 if (ret < 0)
506 goto error;
507 l = path->nodes[0];
508 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
509 while (1) {
510 l = path->nodes[0];
511 slot = path->slots[0];
512 if (slot >= btrfs_header_nritems(l)) {
513 ret = btrfs_next_leaf(root, path);
514 if (ret == 0)
515 continue;
516 if (ret < 0)
517 goto error;
518no_more_items:
519 if (!start_found) {
520 if (search_start >= search_end) {
521 ret = -ENOSPC;
522 goto error;
523 }
524 *start = search_start;
525 start_found = 1;
526 goto check_pending;
527 }
528 *start = last_byte > search_start ?
529 last_byte : search_start;
530 if (search_end <= *start) {
531 ret = -ENOSPC;
532 goto error;
533 }
534 goto check_pending;
535 }
536 btrfs_item_key_to_cpu(l, &key, slot);
537
538 if (key.objectid < device->devid)
539 goto next;
540
541 if (key.objectid > device->devid)
542 goto no_more_items;
543
544 if (key.offset >= search_start && key.offset > last_byte &&
545 start_found) {
546 if (last_byte < search_start)
547 last_byte = search_start;
548 hole_size = key.offset - last_byte;
549 if (key.offset > last_byte &&
550 hole_size >= num_bytes) {
551 *start = last_byte;
552 goto check_pending;
553 }
554 }
555 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
556 goto next;
557 }
558
559 start_found = 1;
560 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
561 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
562next:
563 path->slots[0]++;
564 cond_resched();
565 }
566check_pending:
567 /* we have to make sure we didn't find an extent that has already
568 * been allocated by the map tree or the original allocation
569 */
570 btrfs_release_path(root, path);
571 BUG_ON(*start < search_start);
572
Chris Mason6324fbf2008-03-24 15:01:59 -0400573 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400574 ret = -ENOSPC;
575 goto error;
576 }
577 /* check for pending inserts here */
578 return 0;
579
580error:
581 btrfs_release_path(root, path);
582 return ret;
583}
584
Chris Mason8f18cf12008-04-25 16:53:30 -0400585int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
586 struct btrfs_device *device,
587 u64 start)
588{
589 int ret;
590 struct btrfs_path *path;
591 struct btrfs_root *root = device->dev_root;
592 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400593 struct btrfs_key found_key;
594 struct extent_buffer *leaf = NULL;
595 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400596
597 path = btrfs_alloc_path();
598 if (!path)
599 return -ENOMEM;
600
601 key.objectid = device->devid;
602 key.offset = start;
603 key.type = BTRFS_DEV_EXTENT_KEY;
604
605 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400606 if (ret > 0) {
607 ret = btrfs_previous_item(root, path, key.objectid,
608 BTRFS_DEV_EXTENT_KEY);
609 BUG_ON(ret);
610 leaf = path->nodes[0];
611 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
612 extent = btrfs_item_ptr(leaf, path->slots[0],
613 struct btrfs_dev_extent);
614 BUG_ON(found_key.offset > start || found_key.offset +
615 btrfs_dev_extent_length(leaf, extent) < start);
616 ret = 0;
617 } else if (ret == 0) {
618 leaf = path->nodes[0];
619 extent = btrfs_item_ptr(leaf, path->slots[0],
620 struct btrfs_dev_extent);
621 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400622 BUG_ON(ret);
623
Chris Masondfe25022008-05-13 13:46:40 -0400624 if (device->bytes_used > 0)
625 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400626 ret = btrfs_del_item(trans, root, path);
627 BUG_ON(ret);
628
629 btrfs_free_path(path);
630 return ret;
631}
632
Chris Mason0b86a832008-03-24 15:01:56 -0400633int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
634 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400635 u64 chunk_tree, u64 chunk_objectid,
636 u64 chunk_offset,
637 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400638{
639 int ret;
640 struct btrfs_path *path;
641 struct btrfs_root *root = device->dev_root;
642 struct btrfs_dev_extent *extent;
643 struct extent_buffer *leaf;
644 struct btrfs_key key;
645
Chris Masondfe25022008-05-13 13:46:40 -0400646 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400647 path = btrfs_alloc_path();
648 if (!path)
649 return -ENOMEM;
650
651 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
Chris Mason6324fbf2008-03-24 15:01:59 -0400652 if (ret) {
Chris Mason0b86a832008-03-24 15:01:56 -0400653 goto err;
Chris Mason6324fbf2008-03-24 15:01:59 -0400654 }
Chris Mason0b86a832008-03-24 15:01:56 -0400655
656 key.objectid = device->devid;
657 key.offset = *start;
658 key.type = BTRFS_DEV_EXTENT_KEY;
659 ret = btrfs_insert_empty_item(trans, root, path, &key,
660 sizeof(*extent));
661 BUG_ON(ret);
662
663 leaf = path->nodes[0];
664 extent = btrfs_item_ptr(leaf, path->slots[0],
665 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400666 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
667 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
668 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
669
670 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
671 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
672 BTRFS_UUID_SIZE);
673
Chris Mason0b86a832008-03-24 15:01:56 -0400674 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
675 btrfs_mark_buffer_dirty(leaf);
676err:
677 btrfs_free_path(path);
678 return ret;
679}
680
Chris Masone17cade2008-04-15 15:41:47 -0400681static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400682{
683 struct btrfs_path *path;
684 int ret;
685 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400686 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400687 struct btrfs_key found_key;
688
689 path = btrfs_alloc_path();
690 BUG_ON(!path);
691
Chris Masone17cade2008-04-15 15:41:47 -0400692 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400693 key.offset = (u64)-1;
694 key.type = BTRFS_CHUNK_ITEM_KEY;
695
696 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
697 if (ret < 0)
698 goto error;
699
700 BUG_ON(ret == 0);
701
702 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
703 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400704 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400705 } else {
706 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
707 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400708 if (found_key.objectid != objectid)
709 *offset = 0;
710 else {
711 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
712 struct btrfs_chunk);
713 *offset = found_key.offset +
714 btrfs_chunk_length(path->nodes[0], chunk);
715 }
Chris Mason0b86a832008-03-24 15:01:56 -0400716 }
717 ret = 0;
718error:
719 btrfs_free_path(path);
720 return ret;
721}
722
Chris Mason0b86a832008-03-24 15:01:56 -0400723static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
724 u64 *objectid)
725{
726 int ret;
727 struct btrfs_key key;
728 struct btrfs_key found_key;
729
730 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
731 key.type = BTRFS_DEV_ITEM_KEY;
732 key.offset = (u64)-1;
733
734 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
735 if (ret < 0)
736 goto error;
737
738 BUG_ON(ret == 0);
739
740 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
741 BTRFS_DEV_ITEM_KEY);
742 if (ret) {
743 *objectid = 1;
744 } else {
745 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
746 path->slots[0]);
747 *objectid = found_key.offset + 1;
748 }
749 ret = 0;
750error:
751 btrfs_release_path(root, path);
752 return ret;
753}
754
755/*
756 * the device information is stored in the chunk root
757 * the btrfs_device struct should be fully filled in
758 */
759int btrfs_add_device(struct btrfs_trans_handle *trans,
760 struct btrfs_root *root,
761 struct btrfs_device *device)
762{
763 int ret;
764 struct btrfs_path *path;
765 struct btrfs_dev_item *dev_item;
766 struct extent_buffer *leaf;
767 struct btrfs_key key;
768 unsigned long ptr;
Chris Mason006a58a2008-05-02 14:43:15 -0400769 u64 free_devid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400770
771 root = root->fs_info->chunk_root;
772
773 path = btrfs_alloc_path();
774 if (!path)
775 return -ENOMEM;
776
777 ret = find_next_devid(root, path, &free_devid);
778 if (ret)
779 goto out;
780
781 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
782 key.type = BTRFS_DEV_ITEM_KEY;
783 key.offset = free_devid;
784
785 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400786 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400787 if (ret)
788 goto out;
789
790 leaf = path->nodes[0];
791 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
792
Chris Mason8a4b83c2008-03-24 15:02:07 -0400793 device->devid = free_devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400794 btrfs_set_device_id(leaf, dev_item, device->devid);
795 btrfs_set_device_type(leaf, dev_item, device->type);
796 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
797 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
798 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400799 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
800 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400801 btrfs_set_device_group(leaf, dev_item, 0);
802 btrfs_set_device_seek_speed(leaf, dev_item, 0);
803 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400804
Chris Mason0b86a832008-03-24 15:01:56 -0400805 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400806 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400807 btrfs_mark_buffer_dirty(leaf);
808 ret = 0;
809
810out:
811 btrfs_free_path(path);
812 return ret;
813}
Chris Mason8f18cf12008-04-25 16:53:30 -0400814
Chris Masona061fc82008-05-07 11:43:44 -0400815static int btrfs_rm_dev_item(struct btrfs_root *root,
816 struct btrfs_device *device)
817{
818 int ret;
819 struct btrfs_path *path;
820 struct block_device *bdev = device->bdev;
821 struct btrfs_device *next_dev;
822 struct btrfs_key key;
823 u64 total_bytes;
824 struct btrfs_fs_devices *fs_devices;
825 struct btrfs_trans_handle *trans;
826
827 root = root->fs_info->chunk_root;
828
829 path = btrfs_alloc_path();
830 if (!path)
831 return -ENOMEM;
832
833 trans = btrfs_start_transaction(root, 1);
834 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
835 key.type = BTRFS_DEV_ITEM_KEY;
836 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -0400837 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400838
839 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
840 if (ret < 0)
841 goto out;
842
843 if (ret > 0) {
844 ret = -ENOENT;
845 goto out;
846 }
847
848 ret = btrfs_del_item(trans, root, path);
849 if (ret)
850 goto out;
851
852 /*
853 * at this point, the device is zero sized. We want to
854 * remove it from the devices list and zero out the old super
855 */
856 list_del_init(&device->dev_list);
857 list_del_init(&device->dev_alloc_list);
858 fs_devices = root->fs_info->fs_devices;
859
860 next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
861 dev_list);
Chris Masona061fc82008-05-07 11:43:44 -0400862 if (bdev == root->fs_info->sb->s_bdev)
863 root->fs_info->sb->s_bdev = next_dev->bdev;
864 if (bdev == fs_devices->latest_bdev)
865 fs_devices->latest_bdev = next_dev->bdev;
866
Chris Masona061fc82008-05-07 11:43:44 -0400867 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
868 btrfs_set_super_num_devices(&root->fs_info->super_copy,
869 total_bytes - 1);
870out:
871 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -0400872 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -0400873 btrfs_commit_transaction(trans, root);
874 return ret;
875}
876
877int btrfs_rm_device(struct btrfs_root *root, char *device_path)
878{
879 struct btrfs_device *device;
880 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400881 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -0400882 struct btrfs_super_block *disk_super;
883 u64 all_avail;
884 u64 devid;
885 int ret = 0;
886
Chris Masona061fc82008-05-07 11:43:44 -0400887 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -0400888 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -0400889
890 all_avail = root->fs_info->avail_data_alloc_bits |
891 root->fs_info->avail_system_alloc_bits |
892 root->fs_info->avail_metadata_alloc_bits;
893
894 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Chris Masondfe25022008-05-13 13:46:40 -0400895 btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
Chris Masona061fc82008-05-07 11:43:44 -0400896 printk("btrfs: unable to go below four devices on raid10\n");
897 ret = -EINVAL;
898 goto out;
899 }
900
901 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masondfe25022008-05-13 13:46:40 -0400902 btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
Chris Masona061fc82008-05-07 11:43:44 -0400903 printk("btrfs: unable to go below two devices on raid1\n");
904 ret = -EINVAL;
905 goto out;
906 }
907
Chris Masondfe25022008-05-13 13:46:40 -0400908 if (strcmp(device_path, "missing") == 0) {
909 struct list_head *cur;
910 struct list_head *devices;
911 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -0400912
Chris Masondfe25022008-05-13 13:46:40 -0400913 device = NULL;
914 devices = &root->fs_info->fs_devices->devices;
915 list_for_each(cur, devices) {
916 tmp = list_entry(cur, struct btrfs_device, dev_list);
917 if (tmp->in_fs_metadata && !tmp->bdev) {
918 device = tmp;
919 break;
920 }
921 }
922 bdev = NULL;
923 bh = NULL;
924 disk_super = NULL;
925 if (!device) {
926 printk("btrfs: no missing devices found to remove\n");
927 goto out;
928 }
Chris Masona061fc82008-05-07 11:43:44 -0400929
Chris Masondfe25022008-05-13 13:46:40 -0400930 } else {
931 bdev = open_bdev_excl(device_path, 0,
932 root->fs_info->bdev_holder);
933 if (IS_ERR(bdev)) {
934 ret = PTR_ERR(bdev);
935 goto out;
936 }
937
938 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
939 if (!bh) {
940 ret = -EIO;
941 goto error_close;
942 }
943 disk_super = (struct btrfs_super_block *)bh->b_data;
944 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
945 sizeof(disk_super->magic))) {
946 ret = -ENOENT;
947 goto error_brelse;
948 }
949 if (memcmp(disk_super->fsid, root->fs_info->fsid,
950 BTRFS_FSID_SIZE)) {
951 ret = -ENOENT;
952 goto error_brelse;
953 }
954 devid = le64_to_cpu(disk_super->dev_item.devid);
955 device = btrfs_find_device(root, devid, NULL);
956 if (!device) {
957 ret = -ENOENT;
958 goto error_brelse;
959 }
960
961 }
Chris Masona061fc82008-05-07 11:43:44 -0400962 root->fs_info->fs_devices->num_devices--;
Chris Mason0ef3e662008-05-24 14:04:53 -0400963 root->fs_info->fs_devices->open_devices--;
Chris Masona061fc82008-05-07 11:43:44 -0400964
965 ret = btrfs_shrink_device(device, 0);
966 if (ret)
967 goto error_brelse;
968
969
970 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
971 if (ret)
972 goto error_brelse;
973
Chris Masondfe25022008-05-13 13:46:40 -0400974 if (bh) {
975 /* make sure this device isn't detected as part of
976 * the FS anymore
977 */
978 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
979 set_buffer_dirty(bh);
980 sync_dirty_buffer(bh);
Chris Masona061fc82008-05-07 11:43:44 -0400981
Chris Masondfe25022008-05-13 13:46:40 -0400982 brelse(bh);
983 }
Chris Masona061fc82008-05-07 11:43:44 -0400984
Chris Masondfe25022008-05-13 13:46:40 -0400985 if (device->bdev) {
986 /* one close for the device struct or super_block */
987 close_bdev_excl(device->bdev);
988 }
989 if (bdev) {
990 /* one close for us */
991 close_bdev_excl(bdev);
992 }
Chris Masona061fc82008-05-07 11:43:44 -0400993 kfree(device->name);
994 kfree(device);
995 ret = 0;
996 goto out;
997
998error_brelse:
999 brelse(bh);
1000error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001001 if (bdev)
1002 close_bdev_excl(bdev);
Chris Masona061fc82008-05-07 11:43:44 -04001003out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001004 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001005 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001006 return ret;
1007}
1008
Chris Mason788f20e2008-04-28 15:29:42 -04001009int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1010{
1011 struct btrfs_trans_handle *trans;
1012 struct btrfs_device *device;
1013 struct block_device *bdev;
1014 struct list_head *cur;
1015 struct list_head *devices;
1016 u64 total_bytes;
1017 int ret = 0;
1018
1019
1020 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
1021 if (!bdev) {
1022 return -EIO;
1023 }
Chris Masona2135012008-06-25 16:01:30 -04001024
Chris Mason7d9eb122008-07-08 14:19:17 -04001025 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001026
Chris Mason788f20e2008-04-28 15:29:42 -04001027 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04001028 lock_chunks(root);
Chris Mason788f20e2008-04-28 15:29:42 -04001029 devices = &root->fs_info->fs_devices->devices;
1030 list_for_each(cur, devices) {
1031 device = list_entry(cur, struct btrfs_device, dev_list);
1032 if (device->bdev == bdev) {
1033 ret = -EEXIST;
1034 goto out;
1035 }
1036 }
1037
1038 device = kzalloc(sizeof(*device), GFP_NOFS);
1039 if (!device) {
1040 /* we can safely leave the fs_devices entry around */
1041 ret = -ENOMEM;
1042 goto out_close_bdev;
1043 }
1044
1045 device->barriers = 1;
Chris Mason8b712842008-06-11 16:50:36 -04001046 device->work.func = pending_bios_fn;
Chris Mason788f20e2008-04-28 15:29:42 -04001047 generate_random_uuid(device->uuid);
1048 spin_lock_init(&device->io_lock);
1049 device->name = kstrdup(device_path, GFP_NOFS);
1050 if (!device->name) {
1051 kfree(device);
1052 goto out_close_bdev;
1053 }
1054 device->io_width = root->sectorsize;
1055 device->io_align = root->sectorsize;
1056 device->sector_size = root->sectorsize;
1057 device->total_bytes = i_size_read(bdev->bd_inode);
1058 device->dev_root = root->fs_info->dev_root;
1059 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001060 device->in_fs_metadata = 1;
Chris Mason788f20e2008-04-28 15:29:42 -04001061
1062 ret = btrfs_add_device(trans, root, device);
1063 if (ret)
1064 goto out_close_bdev;
1065
1066 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1067 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1068 total_bytes + device->total_bytes);
1069
1070 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1071 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1072 total_bytes + 1);
1073
1074 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1075 list_add(&device->dev_alloc_list,
1076 &root->fs_info->fs_devices->alloc_list);
1077 root->fs_info->fs_devices->num_devices++;
Chris Masona0af4692008-05-13 16:03:06 -04001078 root->fs_info->fs_devices->open_devices++;
Chris Mason788f20e2008-04-28 15:29:42 -04001079out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001080 unlock_chunks(root);
Chris Mason788f20e2008-04-28 15:29:42 -04001081 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04001082 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001083
Chris Mason788f20e2008-04-28 15:29:42 -04001084 return ret;
1085
1086out_close_bdev:
1087 close_bdev_excl(bdev);
1088 goto out;
1089}
1090
Chris Mason0b86a832008-03-24 15:01:56 -04001091int btrfs_update_device(struct btrfs_trans_handle *trans,
1092 struct btrfs_device *device)
1093{
1094 int ret;
1095 struct btrfs_path *path;
1096 struct btrfs_root *root;
1097 struct btrfs_dev_item *dev_item;
1098 struct extent_buffer *leaf;
1099 struct btrfs_key key;
1100
1101 root = device->dev_root->fs_info->chunk_root;
1102
1103 path = btrfs_alloc_path();
1104 if (!path)
1105 return -ENOMEM;
1106
1107 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1108 key.type = BTRFS_DEV_ITEM_KEY;
1109 key.offset = device->devid;
1110
1111 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1112 if (ret < 0)
1113 goto out;
1114
1115 if (ret > 0) {
1116 ret = -ENOENT;
1117 goto out;
1118 }
1119
1120 leaf = path->nodes[0];
1121 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1122
1123 btrfs_set_device_id(leaf, dev_item, device->devid);
1124 btrfs_set_device_type(leaf, dev_item, device->type);
1125 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1126 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1127 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001128 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1129 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1130 btrfs_mark_buffer_dirty(leaf);
1131
1132out:
1133 btrfs_free_path(path);
1134 return ret;
1135}
1136
Chris Mason7d9eb122008-07-08 14:19:17 -04001137static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001138 struct btrfs_device *device, u64 new_size)
1139{
1140 struct btrfs_super_block *super_copy =
1141 &device->dev_root->fs_info->super_copy;
1142 u64 old_total = btrfs_super_total_bytes(super_copy);
1143 u64 diff = new_size - device->total_bytes;
1144
1145 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1146 return btrfs_update_device(trans, device);
1147}
1148
Chris Mason7d9eb122008-07-08 14:19:17 -04001149int btrfs_grow_device(struct btrfs_trans_handle *trans,
1150 struct btrfs_device *device, u64 new_size)
1151{
1152 int ret;
1153 lock_chunks(device->dev_root);
1154 ret = __btrfs_grow_device(trans, device, new_size);
1155 unlock_chunks(device->dev_root);
1156 return ret;
1157}
1158
Chris Mason8f18cf12008-04-25 16:53:30 -04001159static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1160 struct btrfs_root *root,
1161 u64 chunk_tree, u64 chunk_objectid,
1162 u64 chunk_offset)
1163{
1164 int ret;
1165 struct btrfs_path *path;
1166 struct btrfs_key key;
1167
1168 root = root->fs_info->chunk_root;
1169 path = btrfs_alloc_path();
1170 if (!path)
1171 return -ENOMEM;
1172
1173 key.objectid = chunk_objectid;
1174 key.offset = chunk_offset;
1175 key.type = BTRFS_CHUNK_ITEM_KEY;
1176
1177 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1178 BUG_ON(ret);
1179
1180 ret = btrfs_del_item(trans, root, path);
1181 BUG_ON(ret);
1182
1183 btrfs_free_path(path);
1184 return 0;
1185}
1186
1187int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1188 chunk_offset)
1189{
1190 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1191 struct btrfs_disk_key *disk_key;
1192 struct btrfs_chunk *chunk;
1193 u8 *ptr;
1194 int ret = 0;
1195 u32 num_stripes;
1196 u32 array_size;
1197 u32 len = 0;
1198 u32 cur;
1199 struct btrfs_key key;
1200
1201 array_size = btrfs_super_sys_array_size(super_copy);
1202
1203 ptr = super_copy->sys_chunk_array;
1204 cur = 0;
1205
1206 while (cur < array_size) {
1207 disk_key = (struct btrfs_disk_key *)ptr;
1208 btrfs_disk_key_to_cpu(&key, disk_key);
1209
1210 len = sizeof(*disk_key);
1211
1212 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1213 chunk = (struct btrfs_chunk *)(ptr + len);
1214 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1215 len += btrfs_chunk_item_size(num_stripes);
1216 } else {
1217 ret = -EIO;
1218 break;
1219 }
1220 if (key.objectid == chunk_objectid &&
1221 key.offset == chunk_offset) {
1222 memmove(ptr, ptr + len, array_size - (cur + len));
1223 array_size -= len;
1224 btrfs_set_super_sys_array_size(super_copy, array_size);
1225 } else {
1226 ptr += len;
1227 cur += len;
1228 }
1229 }
1230 return ret;
1231}
1232
1233
1234int btrfs_relocate_chunk(struct btrfs_root *root,
1235 u64 chunk_tree, u64 chunk_objectid,
1236 u64 chunk_offset)
1237{
1238 struct extent_map_tree *em_tree;
1239 struct btrfs_root *extent_root;
1240 struct btrfs_trans_handle *trans;
1241 struct extent_map *em;
1242 struct map_lookup *map;
1243 int ret;
1244 int i;
1245
Chris Mason323da792008-05-09 11:46:48 -04001246 printk("btrfs relocating chunk %llu\n",
1247 (unsigned long long)chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001248 root = root->fs_info->chunk_root;
1249 extent_root = root->fs_info->extent_root;
1250 em_tree = &root->fs_info->mapping_tree.map_tree;
1251
1252 /* step one, relocate all the extents inside this chunk */
1253 ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
1254 BUG_ON(ret);
1255
1256 trans = btrfs_start_transaction(root, 1);
1257 BUG_ON(!trans);
1258
Chris Mason7d9eb122008-07-08 14:19:17 -04001259 lock_chunks(root);
1260
Chris Mason8f18cf12008-04-25 16:53:30 -04001261 /*
1262 * step two, delete the device extents and the
1263 * chunk tree entries
1264 */
1265 spin_lock(&em_tree->lock);
1266 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1267 spin_unlock(&em_tree->lock);
1268
Chris Masona061fc82008-05-07 11:43:44 -04001269 BUG_ON(em->start > chunk_offset ||
1270 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001271 map = (struct map_lookup *)em->bdev;
1272
1273 for (i = 0; i < map->num_stripes; i++) {
1274 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1275 map->stripes[i].physical);
1276 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001277
Chris Masondfe25022008-05-13 13:46:40 -04001278 if (map->stripes[i].dev) {
1279 ret = btrfs_update_device(trans, map->stripes[i].dev);
1280 BUG_ON(ret);
1281 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001282 }
1283 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1284 chunk_offset);
1285
1286 BUG_ON(ret);
1287
1288 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1289 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1290 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001291 }
1292
Chris Mason8f18cf12008-04-25 16:53:30 -04001293 spin_lock(&em_tree->lock);
1294 remove_extent_mapping(em_tree, em);
1295 kfree(map);
1296 em->bdev = NULL;
1297
1298 /* once for the tree */
1299 free_extent_map(em);
1300 spin_unlock(&em_tree->lock);
1301
Chris Mason8f18cf12008-04-25 16:53:30 -04001302 /* once for us */
1303 free_extent_map(em);
1304
Chris Mason7d9eb122008-07-08 14:19:17 -04001305 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001306 btrfs_end_transaction(trans, root);
1307 return 0;
1308}
1309
Chris Masonec44a352008-04-28 15:29:52 -04001310static u64 div_factor(u64 num, int factor)
1311{
1312 if (factor == 10)
1313 return num;
1314 num *= factor;
1315 do_div(num, 10);
1316 return num;
1317}
1318
1319
1320int btrfs_balance(struct btrfs_root *dev_root)
1321{
1322 int ret;
1323 struct list_head *cur;
1324 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1325 struct btrfs_device *device;
1326 u64 old_size;
1327 u64 size_to_free;
1328 struct btrfs_path *path;
1329 struct btrfs_key key;
1330 struct btrfs_chunk *chunk;
1331 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1332 struct btrfs_trans_handle *trans;
1333 struct btrfs_key found_key;
1334
1335
Chris Mason7d9eb122008-07-08 14:19:17 -04001336 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001337 dev_root = dev_root->fs_info->dev_root;
1338
Chris Masonec44a352008-04-28 15:29:52 -04001339 /* step one make some room on all the devices */
1340 list_for_each(cur, devices) {
1341 device = list_entry(cur, struct btrfs_device, dev_list);
1342 old_size = device->total_bytes;
1343 size_to_free = div_factor(old_size, 1);
1344 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1345 if (device->total_bytes - device->bytes_used > size_to_free)
1346 continue;
1347
1348 ret = btrfs_shrink_device(device, old_size - size_to_free);
1349 BUG_ON(ret);
1350
1351 trans = btrfs_start_transaction(dev_root, 1);
1352 BUG_ON(!trans);
1353
1354 ret = btrfs_grow_device(trans, device, old_size);
1355 BUG_ON(ret);
1356
1357 btrfs_end_transaction(trans, dev_root);
1358 }
1359
1360 /* step two, relocate all the chunks */
1361 path = btrfs_alloc_path();
1362 BUG_ON(!path);
1363
1364 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1365 key.offset = (u64)-1;
1366 key.type = BTRFS_CHUNK_ITEM_KEY;
1367
1368 while(1) {
1369 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1370 if (ret < 0)
1371 goto error;
1372
1373 /*
1374 * this shouldn't happen, it means the last relocate
1375 * failed
1376 */
1377 if (ret == 0)
1378 break;
1379
1380 ret = btrfs_previous_item(chunk_root, path, 0,
1381 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001382 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001383 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001384
Chris Masonec44a352008-04-28 15:29:52 -04001385 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1386 path->slots[0]);
1387 if (found_key.objectid != key.objectid)
1388 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001389
Chris Masonec44a352008-04-28 15:29:52 -04001390 chunk = btrfs_item_ptr(path->nodes[0],
1391 path->slots[0],
1392 struct btrfs_chunk);
1393 key.offset = found_key.offset;
1394 /* chunk zero is special */
1395 if (key.offset == 0)
1396 break;
1397
Chris Mason7d9eb122008-07-08 14:19:17 -04001398 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001399 ret = btrfs_relocate_chunk(chunk_root,
1400 chunk_root->root_key.objectid,
1401 found_key.objectid,
1402 found_key.offset);
1403 BUG_ON(ret);
Chris Masonec44a352008-04-28 15:29:52 -04001404 }
1405 ret = 0;
1406error:
1407 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001408 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001409 return ret;
1410}
1411
Chris Mason8f18cf12008-04-25 16:53:30 -04001412/*
1413 * shrinking a device means finding all of the device extents past
1414 * the new size, and then following the back refs to the chunks.
1415 * The chunk relocation code actually frees the device extent
1416 */
1417int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1418{
1419 struct btrfs_trans_handle *trans;
1420 struct btrfs_root *root = device->dev_root;
1421 struct btrfs_dev_extent *dev_extent = NULL;
1422 struct btrfs_path *path;
1423 u64 length;
1424 u64 chunk_tree;
1425 u64 chunk_objectid;
1426 u64 chunk_offset;
1427 int ret;
1428 int slot;
1429 struct extent_buffer *l;
1430 struct btrfs_key key;
1431 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1432 u64 old_total = btrfs_super_total_bytes(super_copy);
1433 u64 diff = device->total_bytes - new_size;
1434
1435
1436 path = btrfs_alloc_path();
1437 if (!path)
1438 return -ENOMEM;
1439
1440 trans = btrfs_start_transaction(root, 1);
1441 if (!trans) {
1442 ret = -ENOMEM;
1443 goto done;
1444 }
1445
1446 path->reada = 2;
1447
Chris Mason7d9eb122008-07-08 14:19:17 -04001448 lock_chunks(root);
1449
Chris Mason8f18cf12008-04-25 16:53:30 -04001450 device->total_bytes = new_size;
1451 ret = btrfs_update_device(trans, device);
1452 if (ret) {
Chris Mason7d9eb122008-07-08 14:19:17 -04001453 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001454 btrfs_end_transaction(trans, root);
1455 goto done;
1456 }
1457 WARN_ON(diff > old_total);
1458 btrfs_set_super_total_bytes(super_copy, old_total - diff);
Chris Mason7d9eb122008-07-08 14:19:17 -04001459 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001460 btrfs_end_transaction(trans, root);
1461
1462 key.objectid = device->devid;
1463 key.offset = (u64)-1;
1464 key.type = BTRFS_DEV_EXTENT_KEY;
1465
1466 while (1) {
1467 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1468 if (ret < 0)
1469 goto done;
1470
1471 ret = btrfs_previous_item(root, path, 0, key.type);
1472 if (ret < 0)
1473 goto done;
1474 if (ret) {
1475 ret = 0;
1476 goto done;
1477 }
1478
1479 l = path->nodes[0];
1480 slot = path->slots[0];
1481 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1482
1483 if (key.objectid != device->devid)
1484 goto done;
1485
1486 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1487 length = btrfs_dev_extent_length(l, dev_extent);
1488
1489 if (key.offset + length <= new_size)
1490 goto done;
1491
1492 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1493 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1494 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1495 btrfs_release_path(root, path);
1496
1497 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1498 chunk_offset);
1499 if (ret)
1500 goto done;
1501 }
1502
1503done:
1504 btrfs_free_path(path);
1505 return ret;
1506}
1507
Chris Mason0b86a832008-03-24 15:01:56 -04001508int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1509 struct btrfs_root *root,
1510 struct btrfs_key *key,
1511 struct btrfs_chunk *chunk, int item_size)
1512{
1513 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1514 struct btrfs_disk_key disk_key;
1515 u32 array_size;
1516 u8 *ptr;
1517
1518 array_size = btrfs_super_sys_array_size(super_copy);
1519 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1520 return -EFBIG;
1521
1522 ptr = super_copy->sys_chunk_array + array_size;
1523 btrfs_cpu_key_to_disk(&disk_key, key);
1524 memcpy(ptr, &disk_key, sizeof(disk_key));
1525 ptr += sizeof(disk_key);
1526 memcpy(ptr, chunk, item_size);
1527 item_size += sizeof(disk_key);
1528 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1529 return 0;
1530}
1531
Chris Mason9b3f68b2008-04-18 10:29:51 -04001532static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
1533 int sub_stripes)
1534{
1535 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1536 return calc_size;
1537 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1538 return calc_size * (num_stripes / sub_stripes);
1539 else
1540 return calc_size * num_stripes;
1541}
1542
1543
Chris Mason0b86a832008-03-24 15:01:56 -04001544int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1545 struct btrfs_root *extent_root, u64 *start,
Chris Mason6324fbf2008-03-24 15:01:59 -04001546 u64 *num_bytes, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001547{
1548 u64 dev_offset;
Chris Mason593060d2008-03-25 16:50:33 -04001549 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001550 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001551 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -04001552 struct btrfs_stripe *stripes;
1553 struct btrfs_device *device = NULL;
1554 struct btrfs_chunk *chunk;
Chris Mason6324fbf2008-03-24 15:01:59 -04001555 struct list_head private_devs;
Chris Masonb3075712008-04-22 09:22:07 -04001556 struct list_head *dev_list;
Chris Mason6324fbf2008-03-24 15:01:59 -04001557 struct list_head *cur;
Chris Mason0b86a832008-03-24 15:01:56 -04001558 struct extent_map_tree *em_tree;
1559 struct map_lookup *map;
1560 struct extent_map *em;
Chris Masona40a90a2008-04-18 11:55:51 -04001561 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001562 u64 physical;
1563 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001564 u64 max_chunk_size = calc_size;
1565 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001566 u64 avail;
1567 u64 max_avail = 0;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001568 u64 percent_max;
Chris Mason6324fbf2008-03-24 15:01:59 -04001569 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001570 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001571 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001572 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001573 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001574 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001575 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001576 struct btrfs_key key;
1577
Chris Masonec44a352008-04-28 15:29:52 -04001578 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1579 (type & BTRFS_BLOCK_GROUP_DUP)) {
1580 WARN_ON(1);
1581 type &= ~BTRFS_BLOCK_GROUP_DUP;
1582 }
Chris Masonb3075712008-04-22 09:22:07 -04001583 dev_list = &extent_root->fs_info->fs_devices->alloc_list;
Chris Mason6324fbf2008-03-24 15:01:59 -04001584 if (list_empty(dev_list))
1585 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04001586
Chris Masona40a90a2008-04-18 11:55:51 -04001587 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001588 num_stripes = extent_root->fs_info->fs_devices->open_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04001589 min_stripes = 2;
1590 }
1591 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04001592 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001593 min_stripes = 2;
1594 }
Chris Mason8790d502008-04-03 16:29:03 -04001595 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1596 num_stripes = min_t(u64, 2,
Chris Mason0ef3e662008-05-24 14:04:53 -04001597 extent_root->fs_info->fs_devices->open_devices);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001598 if (num_stripes < 2)
1599 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04001600 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04001601 }
Chris Mason321aecc2008-04-16 10:49:51 -04001602 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001603 num_stripes = extent_root->fs_info->fs_devices->open_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04001604 if (num_stripes < 4)
1605 return -ENOSPC;
1606 num_stripes &= ~(u32)1;
1607 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001608 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04001609 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04001610
1611 if (type & BTRFS_BLOCK_GROUP_DATA) {
1612 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001613 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001614 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1615 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001616 min_stripe_size = 32 * 1024 * 1024;
1617 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1618 calc_size = 8 * 1024 * 1024;
1619 max_chunk_size = calc_size * 2;
1620 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001621 }
1622
Chris Mason8f18cf12008-04-25 16:53:30 -04001623 path = btrfs_alloc_path();
1624 if (!path)
1625 return -ENOMEM;
1626
Chris Mason9b3f68b2008-04-18 10:29:51 -04001627 /* we don't want a chunk larger than 10% of the FS */
1628 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
1629 max_chunk_size = min(percent_max, max_chunk_size);
1630
Chris Masona40a90a2008-04-18 11:55:51 -04001631again:
Chris Mason9b3f68b2008-04-18 10:29:51 -04001632 if (calc_size * num_stripes > max_chunk_size) {
1633 calc_size = max_chunk_size;
1634 do_div(calc_size, num_stripes);
1635 do_div(calc_size, stripe_len);
1636 calc_size *= stripe_len;
1637 }
1638 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04001639 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001640
Chris Mason9b3f68b2008-04-18 10:29:51 -04001641 do_div(calc_size, stripe_len);
1642 calc_size *= stripe_len;
1643
Chris Mason6324fbf2008-03-24 15:01:59 -04001644 INIT_LIST_HEAD(&private_devs);
1645 cur = dev_list->next;
1646 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04001647
1648 if (type & BTRFS_BLOCK_GROUP_DUP)
1649 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001650 else
1651 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04001652
Chris Masonad5bd912008-04-21 08:28:10 -04001653 /* we add 1MB because we never use the first 1MB of the device */
1654 min_free += 1024 * 1024;
1655
Chris Mason6324fbf2008-03-24 15:01:59 -04001656 /* build a private list of devices we will allocate from */
1657 while(index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04001658 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Chris Mason611f0e02008-04-03 16:29:03 -04001659
Chris Masondfe25022008-05-13 13:46:40 -04001660 if (device->total_bytes > device->bytes_used)
1661 avail = device->total_bytes - device->bytes_used;
1662 else
1663 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001664 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04001665
Chris Masondfe25022008-05-13 13:46:40 -04001666 if (device->in_fs_metadata && avail >= min_free) {
Chris Mason8f18cf12008-04-25 16:53:30 -04001667 u64 ignored_start = 0;
1668 ret = find_free_dev_extent(trans, device, path,
1669 min_free,
1670 &ignored_start);
1671 if (ret == 0) {
1672 list_move_tail(&device->dev_alloc_list,
1673 &private_devs);
Chris Mason611f0e02008-04-03 16:29:03 -04001674 index++;
Chris Mason8f18cf12008-04-25 16:53:30 -04001675 if (type & BTRFS_BLOCK_GROUP_DUP)
1676 index++;
1677 }
Chris Masondfe25022008-05-13 13:46:40 -04001678 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04001679 max_avail = avail;
Chris Mason6324fbf2008-03-24 15:01:59 -04001680 if (cur == dev_list)
1681 break;
1682 }
1683 if (index < num_stripes) {
1684 list_splice(&private_devs, dev_list);
Chris Masona40a90a2008-04-18 11:55:51 -04001685 if (index >= min_stripes) {
1686 num_stripes = index;
1687 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1688 num_stripes /= sub_stripes;
1689 num_stripes *= sub_stripes;
1690 }
1691 looped = 1;
1692 goto again;
1693 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001694 if (!looped && max_avail > 0) {
1695 looped = 1;
1696 calc_size = max_avail;
1697 goto again;
1698 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001699 btrfs_free_path(path);
Chris Mason6324fbf2008-03-24 15:01:59 -04001700 return -ENOSPC;
1701 }
Chris Masone17cade2008-04-15 15:41:47 -04001702 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1703 key.type = BTRFS_CHUNK_ITEM_KEY;
1704 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1705 &key.offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001706 if (ret) {
1707 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001708 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001709 }
Chris Mason0b86a832008-03-24 15:01:56 -04001710
Chris Mason0b86a832008-03-24 15:01:56 -04001711 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
Chris Mason8f18cf12008-04-25 16:53:30 -04001712 if (!chunk) {
1713 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001714 return -ENOMEM;
Chris Mason8f18cf12008-04-25 16:53:30 -04001715 }
Chris Mason0b86a832008-03-24 15:01:56 -04001716
Chris Mason593060d2008-03-25 16:50:33 -04001717 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1718 if (!map) {
1719 kfree(chunk);
Chris Mason8f18cf12008-04-25 16:53:30 -04001720 btrfs_free_path(path);
Chris Mason593060d2008-03-25 16:50:33 -04001721 return -ENOMEM;
1722 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001723 btrfs_free_path(path);
1724 path = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04001725
Chris Mason0b86a832008-03-24 15:01:56 -04001726 stripes = &chunk->stripe;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001727 *num_bytes = chunk_bytes_by_type(type, calc_size,
1728 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04001729
Chris Mason6324fbf2008-03-24 15:01:59 -04001730 index = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001731 while(index < num_stripes) {
Chris Masone17cade2008-04-15 15:41:47 -04001732 struct btrfs_stripe *stripe;
Chris Mason6324fbf2008-03-24 15:01:59 -04001733 BUG_ON(list_empty(&private_devs));
1734 cur = private_devs.next;
Chris Masonb3075712008-04-22 09:22:07 -04001735 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Chris Mason611f0e02008-04-03 16:29:03 -04001736
1737 /* loop over this device again if we're doing a dup group */
1738 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1739 (index == num_stripes - 1))
Chris Masonb3075712008-04-22 09:22:07 -04001740 list_move_tail(&device->dev_alloc_list, dev_list);
Chris Mason0b86a832008-03-24 15:01:56 -04001741
1742 ret = btrfs_alloc_dev_extent(trans, device,
Chris Masone17cade2008-04-15 15:41:47 -04001743 info->chunk_root->root_key.objectid,
1744 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1745 calc_size, &dev_offset);
Chris Mason0b86a832008-03-24 15:01:56 -04001746 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001747 device->bytes_used += calc_size;
1748 ret = btrfs_update_device(trans, device);
1749 BUG_ON(ret);
1750
Chris Mason593060d2008-03-25 16:50:33 -04001751 map->stripes[index].dev = device;
1752 map->stripes[index].physical = dev_offset;
Chris Masone17cade2008-04-15 15:41:47 -04001753 stripe = stripes + index;
1754 btrfs_set_stack_stripe_devid(stripe, device->devid);
1755 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1756 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001757 physical = dev_offset;
1758 index++;
1759 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001760 BUG_ON(!list_empty(&private_devs));
Chris Mason0b86a832008-03-24 15:01:56 -04001761
Chris Masone17cade2008-04-15 15:41:47 -04001762 /* key was set above */
1763 btrfs_set_stack_chunk_length(chunk, *num_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001764 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
Chris Mason593060d2008-03-25 16:50:33 -04001765 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -04001766 btrfs_set_stack_chunk_type(chunk, type);
1767 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001768 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1769 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -04001770 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
Chris Mason321aecc2008-04-16 10:49:51 -04001771 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001772 map->sector_size = extent_root->sectorsize;
1773 map->stripe_len = stripe_len;
1774 map->io_align = stripe_len;
1775 map->io_width = stripe_len;
1776 map->type = type;
1777 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001778 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04001779
1780 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1781 btrfs_chunk_item_size(num_stripes));
1782 BUG_ON(ret);
Chris Masone17cade2008-04-15 15:41:47 -04001783 *start = key.offset;;
Chris Mason0b86a832008-03-24 15:01:56 -04001784
1785 em = alloc_extent_map(GFP_NOFS);
1786 if (!em)
1787 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001788 em->bdev = (struct block_device *)map;
Chris Masone17cade2008-04-15 15:41:47 -04001789 em->start = key.offset;
1790 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04001791 em->block_start = 0;
1792
Chris Mason8f18cf12008-04-25 16:53:30 -04001793 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1794 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1795 chunk, btrfs_chunk_item_size(num_stripes));
1796 BUG_ON(ret);
1797 }
Chris Mason0b86a832008-03-24 15:01:56 -04001798 kfree(chunk);
1799
1800 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
1801 spin_lock(&em_tree->lock);
1802 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04001803 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04001804 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001805 free_extent_map(em);
1806 return ret;
1807}
1808
1809void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1810{
1811 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
1812}
1813
1814void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
1815{
1816 struct extent_map *em;
1817
1818 while(1) {
1819 spin_lock(&tree->map_tree.lock);
1820 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
1821 if (em)
1822 remove_extent_mapping(&tree->map_tree, em);
1823 spin_unlock(&tree->map_tree.lock);
1824 if (!em)
1825 break;
1826 kfree(em->bdev);
1827 /* once for us */
1828 free_extent_map(em);
1829 /* once for the tree */
1830 free_extent_map(em);
1831 }
1832}
1833
Chris Masonf1885912008-04-09 16:28:12 -04001834int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1835{
1836 struct extent_map *em;
1837 struct map_lookup *map;
1838 struct extent_map_tree *em_tree = &map_tree->map_tree;
1839 int ret;
1840
1841 spin_lock(&em_tree->lock);
1842 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04001843 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04001844 BUG_ON(!em);
1845
1846 BUG_ON(em->start > logical || em->start + em->len < logical);
1847 map = (struct map_lookup *)em->bdev;
1848 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1849 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001850 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1851 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001852 else
1853 ret = 1;
1854 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04001855 return ret;
1856}
1857
Chris Masondfe25022008-05-13 13:46:40 -04001858static int find_live_mirror(struct map_lookup *map, int first, int num,
1859 int optimal)
1860{
1861 int i;
1862 if (map->stripes[optimal].dev->bdev)
1863 return optimal;
1864 for (i = first; i < first + num; i++) {
1865 if (map->stripes[i].dev->bdev)
1866 return i;
1867 }
1868 /* we couldn't find one that doesn't fail. Just return something
1869 * and the io error handling code will clean up eventually
1870 */
1871 return optimal;
1872}
1873
Chris Masonf2d8d742008-04-21 10:03:05 -04001874static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1875 u64 logical, u64 *length,
1876 struct btrfs_multi_bio **multi_ret,
1877 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04001878{
1879 struct extent_map *em;
1880 struct map_lookup *map;
1881 struct extent_map_tree *em_tree = &map_tree->map_tree;
1882 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04001883 u64 stripe_offset;
1884 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04001885 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04001886 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04001887 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04001888 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04001889 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04001890 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04001891 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001892
Chris Masoncea9e442008-04-09 16:28:12 -04001893 if (multi_ret && !(rw & (1 << BIO_RW))) {
1894 stripes_allocated = 1;
1895 }
1896again:
1897 if (multi_ret) {
1898 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1899 GFP_NOFS);
1900 if (!multi)
1901 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04001902
1903 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001904 }
Chris Mason0b86a832008-03-24 15:01:56 -04001905
1906 spin_lock(&em_tree->lock);
1907 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04001908 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04001909
1910 if (!em && unplug_page)
1911 return 0;
1912
Chris Mason3b951512008-04-17 11:29:12 -04001913 if (!em) {
Chris Masona061fc82008-05-07 11:43:44 -04001914 printk("unable to find logical %Lu len %Lu\n", logical, *length);
Chris Masonf2d8d742008-04-21 10:03:05 -04001915 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04001916 }
Chris Mason0b86a832008-03-24 15:01:56 -04001917
1918 BUG_ON(em->start > logical || em->start + em->len < logical);
1919 map = (struct map_lookup *)em->bdev;
1920 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04001921
Chris Masonf1885912008-04-09 16:28:12 -04001922 if (mirror_num > map->num_stripes)
1923 mirror_num = 0;
1924
Chris Masoncea9e442008-04-09 16:28:12 -04001925 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04001926 if (rw & (1 << BIO_RW)) {
1927 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1928 BTRFS_BLOCK_GROUP_DUP)) {
1929 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04001930 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001931 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1932 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04001933 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001934 }
1935 }
1936 if (multi_ret && rw == WRITE &&
1937 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04001938 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04001939 free_extent_map(em);
1940 kfree(multi);
1941 goto again;
1942 }
Chris Mason593060d2008-03-25 16:50:33 -04001943 stripe_nr = offset;
1944 /*
1945 * stripe_nr counts the total number of stripes we have to stride
1946 * to get to this block
1947 */
1948 do_div(stripe_nr, map->stripe_len);
1949
1950 stripe_offset = stripe_nr * map->stripe_len;
1951 BUG_ON(offset < stripe_offset);
1952
1953 /* stripe_offset is the offset of this block in its stripe*/
1954 stripe_offset = offset - stripe_offset;
1955
Chris Masoncea9e442008-04-09 16:28:12 -04001956 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001957 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04001958 BTRFS_BLOCK_GROUP_DUP)) {
1959 /* we limit the length of each bio to what fits in a stripe */
1960 *length = min_t(u64, em->len - offset,
1961 map->stripe_len - stripe_offset);
1962 } else {
1963 *length = em->len - offset;
1964 }
Chris Masonf2d8d742008-04-21 10:03:05 -04001965
1966 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04001967 goto out;
1968
Chris Masonf2d8d742008-04-21 10:03:05 -04001969 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04001970 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001971 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04001972 if (unplug_page || (rw & (1 << BIO_RW)))
1973 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04001974 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04001975 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04001976 else {
1977 stripe_index = find_live_mirror(map, 0,
1978 map->num_stripes,
1979 current->pid % map->num_stripes);
1980 }
Chris Mason2fff7342008-04-29 14:12:09 -04001981
Chris Mason611f0e02008-04-03 16:29:03 -04001982 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04001983 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04001984 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001985 else if (mirror_num)
1986 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04001987
Chris Mason321aecc2008-04-16 10:49:51 -04001988 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1989 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001990
1991 stripe_index = do_div(stripe_nr, factor);
1992 stripe_index *= map->sub_stripes;
1993
Chris Masonf2d8d742008-04-21 10:03:05 -04001994 if (unplug_page || (rw & (1 << BIO_RW)))
1995 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001996 else if (mirror_num)
1997 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04001998 else {
1999 stripe_index = find_live_mirror(map, stripe_index,
2000 map->sub_stripes, stripe_index +
2001 current->pid % map->sub_stripes);
2002 }
Chris Mason8790d502008-04-03 16:29:03 -04002003 } else {
2004 /*
2005 * after this do_div call, stripe_nr is the number of stripes
2006 * on this device we have to walk to find the data, and
2007 * stripe_index is the number of our device in the stripe array
2008 */
2009 stripe_index = do_div(stripe_nr, map->num_stripes);
2010 }
Chris Mason593060d2008-03-25 16:50:33 -04002011 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002012
Chris Masonf2d8d742008-04-21 10:03:05 -04002013 for (i = 0; i < num_stripes; i++) {
2014 if (unplug_page) {
2015 struct btrfs_device *device;
2016 struct backing_dev_info *bdi;
2017
2018 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002019 if (device->bdev) {
2020 bdi = blk_get_backing_dev_info(device->bdev);
2021 if (bdi->unplug_io_fn) {
2022 bdi->unplug_io_fn(bdi, unplug_page);
2023 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002024 }
2025 } else {
2026 multi->stripes[i].physical =
2027 map->stripes[stripe_index].physical +
2028 stripe_offset + stripe_nr * map->stripe_len;
2029 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2030 }
Chris Masoncea9e442008-04-09 16:28:12 -04002031 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002032 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002033 if (multi_ret) {
2034 *multi_ret = multi;
2035 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002036 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002037 }
Chris Masoncea9e442008-04-09 16:28:12 -04002038out:
Chris Mason0b86a832008-03-24 15:01:56 -04002039 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002040 return 0;
2041}
2042
Chris Masonf2d8d742008-04-21 10:03:05 -04002043int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2044 u64 logical, u64 *length,
2045 struct btrfs_multi_bio **multi_ret, int mirror_num)
2046{
2047 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2048 mirror_num, NULL);
2049}
2050
2051int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2052 u64 logical, struct page *page)
2053{
2054 u64 length = PAGE_CACHE_SIZE;
2055 return __btrfs_map_block(map_tree, READ, logical, &length,
2056 NULL, 0, page);
2057}
2058
2059
Chris Mason8790d502008-04-03 16:29:03 -04002060#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2061static void end_bio_multi_stripe(struct bio *bio, int err)
2062#else
2063static int end_bio_multi_stripe(struct bio *bio,
2064 unsigned int bytes_done, int err)
2065#endif
2066{
Chris Masoncea9e442008-04-09 16:28:12 -04002067 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason8790d502008-04-03 16:29:03 -04002068
2069#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2070 if (bio->bi_size)
2071 return 1;
2072#endif
2073 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002074 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002075
Chris Masoncea9e442008-04-09 16:28:12 -04002076 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason8790d502008-04-03 16:29:03 -04002077 bio->bi_private = multi->private;
2078 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002079 /* only send an error to the higher layers if it is
2080 * beyond the tolerance of the multi-bio
2081 */
Chris Mason1259ab72008-05-12 13:39:03 -04002082 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002083 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002084 } else if (err) {
2085 /*
2086 * this bio is actually up to date, we didn't
2087 * go over the max number of errors
2088 */
2089 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002090 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002091 }
Chris Mason8790d502008-04-03 16:29:03 -04002092 kfree(multi);
2093
Miguel73f61b22008-04-11 15:50:59 -04002094#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2095 bio_endio(bio, bio->bi_size, err);
2096#else
Chris Mason8790d502008-04-03 16:29:03 -04002097 bio_endio(bio, err);
Miguel73f61b22008-04-11 15:50:59 -04002098#endif
Chris Mason8790d502008-04-03 16:29:03 -04002099 } else {
2100 bio_put(bio);
2101 }
2102#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2103 return 0;
2104#endif
2105}
2106
Chris Mason8b712842008-06-11 16:50:36 -04002107struct async_sched {
2108 struct bio *bio;
2109 int rw;
2110 struct btrfs_fs_info *info;
2111 struct btrfs_work work;
2112};
2113
2114/*
2115 * see run_scheduled_bios for a description of why bios are collected for
2116 * async submit.
2117 *
2118 * This will add one bio to the pending list for a device and make sure
2119 * the work struct is scheduled.
2120 */
2121int schedule_bio(struct btrfs_root *root, struct btrfs_device *device,
2122 int rw, struct bio *bio)
2123{
2124 int should_queue = 1;
2125
2126 /* don't bother with additional async steps for reads, right now */
2127 if (!(rw & (1 << BIO_RW))) {
2128 submit_bio(rw, bio);
2129 return 0;
2130 }
2131
2132 /*
2133 * nr_async_sumbits allows us to reliably return congestion to the
2134 * higher layers. Otherwise, the async bio makes it appear we have
2135 * made progress against dirty pages when we've really just put it
2136 * on a queue for later
2137 */
2138 atomic_inc(&root->fs_info->nr_async_submits);
2139 bio->bi_next = NULL;
2140 bio->bi_rw |= rw;
2141
2142 spin_lock(&device->io_lock);
2143
2144 if (device->pending_bio_tail)
2145 device->pending_bio_tail->bi_next = bio;
2146
2147 device->pending_bio_tail = bio;
2148 if (!device->pending_bios)
2149 device->pending_bios = bio;
2150 if (device->running_pending)
2151 should_queue = 0;
2152
2153 spin_unlock(&device->io_lock);
2154
2155 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002156 btrfs_queue_worker(&root->fs_info->submit_workers,
2157 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002158 return 0;
2159}
2160
Chris Masonf1885912008-04-09 16:28:12 -04002161int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002162 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002163{
2164 struct btrfs_mapping_tree *map_tree;
2165 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002166 struct bio *first_bio = bio;
Chris Mason0b86a832008-03-24 15:01:56 -04002167 u64 logical = bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002168 u64 length = 0;
2169 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002170 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002171 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002172 int dev_nr = 0;
2173 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002174
Chris Masonf2d8d742008-04-21 10:03:05 -04002175 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002176 map_tree = &root->fs_info->mapping_tree;
2177 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04002178
Chris Masonf1885912008-04-09 16:28:12 -04002179 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2180 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04002181 BUG_ON(ret);
2182
2183 total_devs = multi->num_stripes;
2184 if (map_length < length) {
2185 printk("mapping failed logical %Lu bio len %Lu "
2186 "len %Lu\n", logical, length, map_length);
2187 BUG();
2188 }
2189 multi->end_io = first_bio->bi_end_io;
2190 multi->private = first_bio->bi_private;
2191 atomic_set(&multi->stripes_pending, multi->num_stripes);
2192
Chris Mason8790d502008-04-03 16:29:03 -04002193 while(dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04002194 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04002195 if (dev_nr < total_devs - 1) {
2196 bio = bio_clone(first_bio, GFP_NOFS);
2197 BUG_ON(!bio);
2198 } else {
2199 bio = first_bio;
2200 }
2201 bio->bi_private = multi;
2202 bio->bi_end_io = end_bio_multi_stripe;
2203 }
Chris Masoncea9e442008-04-09 16:28:12 -04002204 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2205 dev = multi->stripes[dev_nr].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002206 if (dev && dev->bdev) {
2207 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04002208 if (async_submit)
2209 schedule_bio(root, dev, rw, bio);
2210 else
2211 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04002212 } else {
2213 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2214 bio->bi_sector = logical >> 9;
2215#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2216 bio_endio(bio, bio->bi_size, -EIO);
2217#else
2218 bio_endio(bio, -EIO);
2219#endif
2220 }
Chris Mason8790d502008-04-03 16:29:03 -04002221 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04002222 }
Chris Masoncea9e442008-04-09 16:28:12 -04002223 if (total_devs == 1)
2224 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04002225 return 0;
2226}
2227
Chris Masona4437552008-04-18 10:29:38 -04002228struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2229 u8 *uuid)
Chris Mason0b86a832008-03-24 15:01:56 -04002230{
Chris Mason8a4b83c2008-03-24 15:02:07 -04002231 struct list_head *head = &root->fs_info->fs_devices->devices;
Chris Mason0b86a832008-03-24 15:01:56 -04002232
Chris Masona4437552008-04-18 10:29:38 -04002233 return __find_device(head, devid, uuid);
Chris Mason0b86a832008-03-24 15:01:56 -04002234}
2235
Chris Masondfe25022008-05-13 13:46:40 -04002236static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2237 u64 devid, u8 *dev_uuid)
2238{
2239 struct btrfs_device *device;
2240 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2241
2242 device = kzalloc(sizeof(*device), GFP_NOFS);
2243 list_add(&device->dev_list,
2244 &fs_devices->devices);
2245 list_add(&device->dev_alloc_list,
2246 &fs_devices->alloc_list);
2247 device->barriers = 1;
2248 device->dev_root = root->fs_info->dev_root;
2249 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04002250 device->work.func = pending_bios_fn;
Chris Masondfe25022008-05-13 13:46:40 -04002251 fs_devices->num_devices++;
2252 spin_lock_init(&device->io_lock);
2253 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2254 return device;
2255}
2256
2257
Chris Mason0b86a832008-03-24 15:01:56 -04002258static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2259 struct extent_buffer *leaf,
2260 struct btrfs_chunk *chunk)
2261{
2262 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2263 struct map_lookup *map;
2264 struct extent_map *em;
2265 u64 logical;
2266 u64 length;
2267 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04002268 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04002269 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002270 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04002271 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04002272
Chris Masone17cade2008-04-15 15:41:47 -04002273 logical = key->offset;
2274 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04002275
Chris Mason0b86a832008-03-24 15:01:56 -04002276 spin_lock(&map_tree->map_tree.lock);
2277 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04002278 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002279
2280 /* already mapped? */
2281 if (em && em->start <= logical && em->start + em->len > logical) {
2282 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002283 return 0;
2284 } else if (em) {
2285 free_extent_map(em);
2286 }
Chris Mason0b86a832008-03-24 15:01:56 -04002287
2288 map = kzalloc(sizeof(*map), GFP_NOFS);
2289 if (!map)
2290 return -ENOMEM;
2291
2292 em = alloc_extent_map(GFP_NOFS);
2293 if (!em)
2294 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04002295 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2296 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04002297 if (!map) {
2298 free_extent_map(em);
2299 return -ENOMEM;
2300 }
2301
2302 em->bdev = (struct block_device *)map;
2303 em->start = logical;
2304 em->len = length;
2305 em->block_start = 0;
2306
Chris Mason593060d2008-03-25 16:50:33 -04002307 map->num_stripes = num_stripes;
2308 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2309 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2310 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2311 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2312 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04002313 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04002314 for (i = 0; i < num_stripes; i++) {
2315 map->stripes[i].physical =
2316 btrfs_stripe_offset_nr(leaf, chunk, i);
2317 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04002318 read_extent_buffer(leaf, uuid, (unsigned long)
2319 btrfs_stripe_dev_uuid_nr(chunk, i),
2320 BTRFS_UUID_SIZE);
2321 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04002322
2323 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04002324 kfree(map);
2325 free_extent_map(em);
2326 return -EIO;
2327 }
Chris Masondfe25022008-05-13 13:46:40 -04002328 if (!map->stripes[i].dev) {
2329 map->stripes[i].dev =
2330 add_missing_dev(root, devid, uuid);
2331 if (!map->stripes[i].dev) {
2332 kfree(map);
2333 free_extent_map(em);
2334 return -EIO;
2335 }
2336 }
2337 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002338 }
2339
2340 spin_lock(&map_tree->map_tree.lock);
2341 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04002342 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04002343 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002344 free_extent_map(em);
2345
2346 return 0;
2347}
2348
2349static int fill_device_from_item(struct extent_buffer *leaf,
2350 struct btrfs_dev_item *dev_item,
2351 struct btrfs_device *device)
2352{
2353 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04002354
2355 device->devid = btrfs_device_id(leaf, dev_item);
2356 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2357 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2358 device->type = btrfs_device_type(leaf, dev_item);
2359 device->io_align = btrfs_device_io_align(leaf, dev_item);
2360 device->io_width = btrfs_device_io_width(leaf, dev_item);
2361 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04002362
2363 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04002364 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002365
Chris Mason0b86a832008-03-24 15:01:56 -04002366 return 0;
2367}
2368
Chris Mason0d81ba52008-03-24 15:02:07 -04002369static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04002370 struct extent_buffer *leaf,
2371 struct btrfs_dev_item *dev_item)
2372{
2373 struct btrfs_device *device;
2374 u64 devid;
2375 int ret;
Chris Masona4437552008-04-18 10:29:38 -04002376 u8 dev_uuid[BTRFS_UUID_SIZE];
2377
Chris Mason0b86a832008-03-24 15:01:56 -04002378 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04002379 read_extent_buffer(leaf, dev_uuid,
2380 (unsigned long)btrfs_device_uuid(dev_item),
2381 BTRFS_UUID_SIZE);
2382 device = btrfs_find_device(root, devid, dev_uuid);
Chris Mason6324fbf2008-03-24 15:01:59 -04002383 if (!device) {
Chris Masondfe25022008-05-13 13:46:40 -04002384 printk("warning devid %Lu missing\n", devid);
2385 device = add_missing_dev(root, devid, dev_uuid);
Chris Mason6324fbf2008-03-24 15:01:59 -04002386 if (!device)
2387 return -ENOMEM;
Chris Mason6324fbf2008-03-24 15:01:59 -04002388 }
Chris Mason0b86a832008-03-24 15:01:56 -04002389
2390 fill_device_from_item(leaf, dev_item, device);
2391 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04002392 device->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002393 ret = 0;
2394#if 0
2395 ret = btrfs_open_device(device);
2396 if (ret) {
2397 kfree(device);
2398 }
2399#endif
2400 return ret;
2401}
2402
Chris Mason0d81ba52008-03-24 15:02:07 -04002403int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
2404{
2405 struct btrfs_dev_item *dev_item;
2406
2407 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
2408 dev_item);
2409 return read_one_dev(root, buf, dev_item);
2410}
2411
Chris Mason0b86a832008-03-24 15:01:56 -04002412int btrfs_read_sys_array(struct btrfs_root *root)
2413{
2414 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04002415 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04002416 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04002417 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04002418 u8 *ptr;
2419 unsigned long sb_ptr;
2420 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002421 u32 num_stripes;
2422 u32 array_size;
2423 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002424 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04002425 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04002426
Chris Masona061fc82008-05-07 11:43:44 -04002427 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
2428 BTRFS_SUPER_INFO_SIZE);
2429 if (!sb)
2430 return -ENOMEM;
2431 btrfs_set_buffer_uptodate(sb);
2432 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04002433 array_size = btrfs_super_sys_array_size(super_copy);
2434
Chris Mason0b86a832008-03-24 15:01:56 -04002435 ptr = super_copy->sys_chunk_array;
2436 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
2437 cur = 0;
2438
2439 while (cur < array_size) {
2440 disk_key = (struct btrfs_disk_key *)ptr;
2441 btrfs_disk_key_to_cpu(&key, disk_key);
2442
Chris Masona061fc82008-05-07 11:43:44 -04002443 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04002444 sb_ptr += len;
2445 cur += len;
2446
Chris Mason0d81ba52008-03-24 15:02:07 -04002447 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04002448 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04002449 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04002450 if (ret)
2451 break;
Chris Mason0b86a832008-03-24 15:01:56 -04002452 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
2453 len = btrfs_chunk_item_size(num_stripes);
2454 } else {
Chris Mason84eed902008-04-25 09:04:37 -04002455 ret = -EIO;
2456 break;
Chris Mason0b86a832008-03-24 15:01:56 -04002457 }
2458 ptr += len;
2459 sb_ptr += len;
2460 cur += len;
2461 }
Chris Masona061fc82008-05-07 11:43:44 -04002462 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04002463 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04002464}
2465
2466int btrfs_read_chunk_tree(struct btrfs_root *root)
2467{
2468 struct btrfs_path *path;
2469 struct extent_buffer *leaf;
2470 struct btrfs_key key;
2471 struct btrfs_key found_key;
2472 int ret;
2473 int slot;
2474
2475 root = root->fs_info->chunk_root;
2476
2477 path = btrfs_alloc_path();
2478 if (!path)
2479 return -ENOMEM;
2480
2481 /* first we search for all of the device items, and then we
2482 * read in all of the chunk items. This way we can create chunk
2483 * mappings that reference all of the devices that are afound
2484 */
2485 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2486 key.offset = 0;
2487 key.type = 0;
2488again:
2489 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2490 while(1) {
2491 leaf = path->nodes[0];
2492 slot = path->slots[0];
2493 if (slot >= btrfs_header_nritems(leaf)) {
2494 ret = btrfs_next_leaf(root, path);
2495 if (ret == 0)
2496 continue;
2497 if (ret < 0)
2498 goto error;
2499 break;
2500 }
2501 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2502 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2503 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
2504 break;
2505 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
2506 struct btrfs_dev_item *dev_item;
2507 dev_item = btrfs_item_ptr(leaf, slot,
2508 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04002509 ret = read_one_dev(root, leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04002510 BUG_ON(ret);
2511 }
2512 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
2513 struct btrfs_chunk *chunk;
2514 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2515 ret = read_one_chunk(root, &found_key, leaf, chunk);
2516 }
2517 path->slots[0]++;
2518 }
2519 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2520 key.objectid = 0;
2521 btrfs_release_path(root, path);
2522 goto again;
2523 }
2524
2525 btrfs_free_path(path);
2526 ret = 0;
2527error:
2528 return ret;
2529}
2530