blob: 4fc92cf813d8e65a51c0da1d6078ad8c68f9284e [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 Mason593060d2008-03-25 16:50:33 -040021#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040022#include "ctree.h"
23#include "extent_map.h"
24#include "disk-io.h"
25#include "transaction.h"
26#include "print-tree.h"
27#include "volumes.h"
28
Chris Mason593060d2008-03-25 16:50:33 -040029struct map_lookup {
30 u64 type;
31 int io_align;
32 int io_width;
33 int stripe_len;
34 int sector_size;
35 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040036 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040037 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040038};
39
40#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040041 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040042
Chris Mason8a4b83c2008-03-24 15:02:07 -040043static DEFINE_MUTEX(uuid_mutex);
44static LIST_HEAD(fs_uuids);
45
46int btrfs_cleanup_fs_uuids(void)
47{
48 struct btrfs_fs_devices *fs_devices;
49 struct list_head *uuid_cur;
50 struct list_head *devices_cur;
51 struct btrfs_device *dev;
52
53 list_for_each(uuid_cur, &fs_uuids) {
54 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
55 list);
56 while(!list_empty(&fs_devices->devices)) {
57 devices_cur = fs_devices->devices.next;
58 dev = list_entry(devices_cur, struct btrfs_device,
59 dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -040060 if (dev->bdev) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040061 close_bdev_excl(dev->bdev);
62 }
63 list_del(&dev->dev_list);
64 kfree(dev);
65 }
66 }
67 return 0;
68}
69
Chris Masona4437552008-04-18 10:29:38 -040070static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
71 u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040072{
73 struct btrfs_device *dev;
74 struct list_head *cur;
75
76 list_for_each(cur, head) {
77 dev = list_entry(cur, struct btrfs_device, dev_list);
Chris Masona4437552008-04-18 10:29:38 -040078 if (dev->devid == devid &&
79 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040080 return dev;
Chris Masona4437552008-04-18 10:29:38 -040081 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040082 }
83 return NULL;
84}
85
86static struct btrfs_fs_devices *find_fsid(u8 *fsid)
87{
88 struct list_head *cur;
89 struct btrfs_fs_devices *fs_devices;
90
91 list_for_each(cur, &fs_uuids) {
92 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
93 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
94 return fs_devices;
95 }
96 return NULL;
97}
98
99static int device_list_add(const char *path,
100 struct btrfs_super_block *disk_super,
101 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
102{
103 struct btrfs_device *device;
104 struct btrfs_fs_devices *fs_devices;
105 u64 found_transid = btrfs_super_generation(disk_super);
106
107 fs_devices = find_fsid(disk_super->fsid);
108 if (!fs_devices) {
109 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
110 if (!fs_devices)
111 return -ENOMEM;
112 INIT_LIST_HEAD(&fs_devices->devices);
113 list_add(&fs_devices->list, &fs_uuids);
114 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
115 fs_devices->latest_devid = devid;
116 fs_devices->latest_trans = found_transid;
117 fs_devices->lowest_devid = (u64)-1;
118 fs_devices->num_devices = 0;
119 device = NULL;
120 } else {
Chris Masona4437552008-04-18 10:29:38 -0400121 device = __find_device(&fs_devices->devices, devid,
122 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400123 }
124 if (!device) {
125 device = kzalloc(sizeof(*device), GFP_NOFS);
126 if (!device) {
127 /* we can safely leave the fs_devices entry around */
128 return -ENOMEM;
129 }
130 device->devid = devid;
Chris Masona4437552008-04-18 10:29:38 -0400131 memcpy(device->uuid, disk_super->dev_item.uuid,
132 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400133 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400134 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400135 device->name = kstrdup(path, GFP_NOFS);
136 if (!device->name) {
137 kfree(device);
138 return -ENOMEM;
139 }
140 list_add(&device->dev_list, &fs_devices->devices);
141 fs_devices->num_devices++;
142 }
143
144 if (found_transid > fs_devices->latest_trans) {
145 fs_devices->latest_devid = devid;
146 fs_devices->latest_trans = found_transid;
147 }
148 if (fs_devices->lowest_devid > devid) {
149 fs_devices->lowest_devid = devid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400150 }
151 *fs_devices_ret = fs_devices;
152 return 0;
153}
154
155int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
156{
157 struct list_head *head = &fs_devices->devices;
158 struct list_head *cur;
159 struct btrfs_device *device;
160
161 mutex_lock(&uuid_mutex);
162 list_for_each(cur, head) {
163 device = list_entry(cur, struct btrfs_device, dev_list);
164 if (device->bdev) {
165 close_bdev_excl(device->bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400166 }
167 device->bdev = NULL;
168 }
169 mutex_unlock(&uuid_mutex);
170 return 0;
171}
172
173int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
174 int flags, void *holder)
175{
176 struct block_device *bdev;
177 struct list_head *head = &fs_devices->devices;
178 struct list_head *cur;
179 struct btrfs_device *device;
180 int ret;
181
182 mutex_lock(&uuid_mutex);
183 list_for_each(cur, head) {
184 device = list_entry(cur, struct btrfs_device, dev_list);
185 bdev = open_bdev_excl(device->name, flags, holder);
Chris Masone17cade2008-04-15 15:41:47 -0400186
Chris Mason8a4b83c2008-03-24 15:02:07 -0400187 if (IS_ERR(bdev)) {
188 printk("open %s failed\n", device->name);
189 ret = PTR_ERR(bdev);
190 goto fail;
191 }
192 if (device->devid == fs_devices->latest_devid)
193 fs_devices->latest_bdev = bdev;
194 if (device->devid == fs_devices->lowest_devid) {
195 fs_devices->lowest_bdev = bdev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400196 }
197 device->bdev = bdev;
198 }
199 mutex_unlock(&uuid_mutex);
200 return 0;
201fail:
202 mutex_unlock(&uuid_mutex);
203 btrfs_close_devices(fs_devices);
204 return ret;
205}
206
207int btrfs_scan_one_device(const char *path, int flags, void *holder,
208 struct btrfs_fs_devices **fs_devices_ret)
209{
210 struct btrfs_super_block *disk_super;
211 struct block_device *bdev;
212 struct buffer_head *bh;
213 int ret;
214 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400215 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400216
217 mutex_lock(&uuid_mutex);
218
Chris Mason8a4b83c2008-03-24 15:02:07 -0400219 bdev = open_bdev_excl(path, flags, holder);
220
221 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400222 ret = PTR_ERR(bdev);
223 goto error;
224 }
225
226 ret = set_blocksize(bdev, 4096);
227 if (ret)
228 goto error_close;
229 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
230 if (!bh) {
231 ret = -EIO;
232 goto error_close;
233 }
234 disk_super = (struct btrfs_super_block *)bh->b_data;
235 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
236 sizeof(disk_super->magic))) {
Yane58ca022008-04-01 11:21:34 -0400237 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400238 goto error_brelse;
239 }
240 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400241 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400242 if (disk_super->label[0])
243 printk("device label %s ", disk_super->label);
244 else {
245 /* FIXME, make a readl uuid parser */
246 printk("device fsid %llx-%llx ",
247 *(unsigned long long *)disk_super->fsid,
248 *(unsigned long long *)(disk_super->fsid + 8));
249 }
250 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400251 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
252
253error_brelse:
254 brelse(bh);
255error_close:
256 close_bdev_excl(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400257error:
258 mutex_unlock(&uuid_mutex);
259 return ret;
260}
Chris Mason0b86a832008-03-24 15:01:56 -0400261
262/*
263 * this uses a pretty simple search, the expectation is that it is
264 * called very infrequently and that a given device has a small number
265 * of extents
266 */
267static int find_free_dev_extent(struct btrfs_trans_handle *trans,
268 struct btrfs_device *device,
269 struct btrfs_path *path,
270 u64 num_bytes, u64 *start)
271{
272 struct btrfs_key key;
273 struct btrfs_root *root = device->dev_root;
274 struct btrfs_dev_extent *dev_extent = NULL;
275 u64 hole_size = 0;
276 u64 last_byte = 0;
277 u64 search_start = 0;
278 u64 search_end = device->total_bytes;
279 int ret;
280 int slot = 0;
281 int start_found;
282 struct extent_buffer *l;
283
284 start_found = 0;
285 path->reada = 2;
286
287 /* FIXME use last free of some kind */
288
Chris Mason8a4b83c2008-03-24 15:02:07 -0400289 /* we don't want to overwrite the superblock on the drive,
290 * so we make sure to start at an offset of at least 1MB
291 */
292 search_start = max((u64)1024 * 1024, search_start);
Chris Mason0b86a832008-03-24 15:01:56 -0400293 key.objectid = device->devid;
294 key.offset = search_start;
295 key.type = BTRFS_DEV_EXTENT_KEY;
296 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
297 if (ret < 0)
298 goto error;
299 ret = btrfs_previous_item(root, path, 0, key.type);
300 if (ret < 0)
301 goto error;
302 l = path->nodes[0];
303 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
304 while (1) {
305 l = path->nodes[0];
306 slot = path->slots[0];
307 if (slot >= btrfs_header_nritems(l)) {
308 ret = btrfs_next_leaf(root, path);
309 if (ret == 0)
310 continue;
311 if (ret < 0)
312 goto error;
313no_more_items:
314 if (!start_found) {
315 if (search_start >= search_end) {
316 ret = -ENOSPC;
317 goto error;
318 }
319 *start = search_start;
320 start_found = 1;
321 goto check_pending;
322 }
323 *start = last_byte > search_start ?
324 last_byte : search_start;
325 if (search_end <= *start) {
326 ret = -ENOSPC;
327 goto error;
328 }
329 goto check_pending;
330 }
331 btrfs_item_key_to_cpu(l, &key, slot);
332
333 if (key.objectid < device->devid)
334 goto next;
335
336 if (key.objectid > device->devid)
337 goto no_more_items;
338
339 if (key.offset >= search_start && key.offset > last_byte &&
340 start_found) {
341 if (last_byte < search_start)
342 last_byte = search_start;
343 hole_size = key.offset - last_byte;
344 if (key.offset > last_byte &&
345 hole_size >= num_bytes) {
346 *start = last_byte;
347 goto check_pending;
348 }
349 }
350 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
351 goto next;
352 }
353
354 start_found = 1;
355 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
356 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
357next:
358 path->slots[0]++;
359 cond_resched();
360 }
361check_pending:
362 /* we have to make sure we didn't find an extent that has already
363 * been allocated by the map tree or the original allocation
364 */
365 btrfs_release_path(root, path);
366 BUG_ON(*start < search_start);
367
Chris Mason6324fbf2008-03-24 15:01:59 -0400368 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400369 ret = -ENOSPC;
370 goto error;
371 }
372 /* check for pending inserts here */
373 return 0;
374
375error:
376 btrfs_release_path(root, path);
377 return ret;
378}
379
380int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
381 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400382 u64 chunk_tree, u64 chunk_objectid,
383 u64 chunk_offset,
384 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400385{
386 int ret;
387 struct btrfs_path *path;
388 struct btrfs_root *root = device->dev_root;
389 struct btrfs_dev_extent *extent;
390 struct extent_buffer *leaf;
391 struct btrfs_key key;
392
393 path = btrfs_alloc_path();
394 if (!path)
395 return -ENOMEM;
396
397 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
Chris Mason6324fbf2008-03-24 15:01:59 -0400398 if (ret) {
Chris Mason0b86a832008-03-24 15:01:56 -0400399 goto err;
Chris Mason6324fbf2008-03-24 15:01:59 -0400400 }
Chris Mason0b86a832008-03-24 15:01:56 -0400401
402 key.objectid = device->devid;
403 key.offset = *start;
404 key.type = BTRFS_DEV_EXTENT_KEY;
405 ret = btrfs_insert_empty_item(trans, root, path, &key,
406 sizeof(*extent));
407 BUG_ON(ret);
408
409 leaf = path->nodes[0];
410 extent = btrfs_item_ptr(leaf, path->slots[0],
411 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400412 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
413 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
414 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
415
416 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
417 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
418 BTRFS_UUID_SIZE);
419
Chris Mason0b86a832008-03-24 15:01:56 -0400420 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
421 btrfs_mark_buffer_dirty(leaf);
422err:
423 btrfs_free_path(path);
424 return ret;
425}
426
Chris Masone17cade2008-04-15 15:41:47 -0400427static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400428{
429 struct btrfs_path *path;
430 int ret;
431 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400432 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400433 struct btrfs_key found_key;
434
435 path = btrfs_alloc_path();
436 BUG_ON(!path);
437
Chris Masone17cade2008-04-15 15:41:47 -0400438 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400439 key.offset = (u64)-1;
440 key.type = BTRFS_CHUNK_ITEM_KEY;
441
442 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
443 if (ret < 0)
444 goto error;
445
446 BUG_ON(ret == 0);
447
448 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
449 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400450 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400451 } else {
452 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
453 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400454 if (found_key.objectid != objectid)
455 *offset = 0;
456 else {
457 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
458 struct btrfs_chunk);
459 *offset = found_key.offset +
460 btrfs_chunk_length(path->nodes[0], chunk);
461 }
Chris Mason0b86a832008-03-24 15:01:56 -0400462 }
463 ret = 0;
464error:
465 btrfs_free_path(path);
466 return ret;
467}
468
Chris Mason0b86a832008-03-24 15:01:56 -0400469static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
470 u64 *objectid)
471{
472 int ret;
473 struct btrfs_key key;
474 struct btrfs_key found_key;
475
476 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
477 key.type = BTRFS_DEV_ITEM_KEY;
478 key.offset = (u64)-1;
479
480 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
481 if (ret < 0)
482 goto error;
483
484 BUG_ON(ret == 0);
485
486 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
487 BTRFS_DEV_ITEM_KEY);
488 if (ret) {
489 *objectid = 1;
490 } else {
491 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
492 path->slots[0]);
493 *objectid = found_key.offset + 1;
494 }
495 ret = 0;
496error:
497 btrfs_release_path(root, path);
498 return ret;
499}
500
501/*
502 * the device information is stored in the chunk root
503 * the btrfs_device struct should be fully filled in
504 */
505int btrfs_add_device(struct btrfs_trans_handle *trans,
506 struct btrfs_root *root,
507 struct btrfs_device *device)
508{
509 int ret;
510 struct btrfs_path *path;
511 struct btrfs_dev_item *dev_item;
512 struct extent_buffer *leaf;
513 struct btrfs_key key;
514 unsigned long ptr;
515 u64 free_devid;
516
517 root = root->fs_info->chunk_root;
518
519 path = btrfs_alloc_path();
520 if (!path)
521 return -ENOMEM;
522
523 ret = find_next_devid(root, path, &free_devid);
524 if (ret)
525 goto out;
526
527 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
528 key.type = BTRFS_DEV_ITEM_KEY;
529 key.offset = free_devid;
530
531 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400532 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400533 if (ret)
534 goto out;
535
536 leaf = path->nodes[0];
537 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
538
Chris Mason8a4b83c2008-03-24 15:02:07 -0400539 device->devid = free_devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400540 btrfs_set_device_id(leaf, dev_item, device->devid);
541 btrfs_set_device_type(leaf, dev_item, device->type);
542 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
543 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
544 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400545 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
546 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400547 btrfs_set_device_group(leaf, dev_item, 0);
548 btrfs_set_device_seek_speed(leaf, dev_item, 0);
549 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400550
Chris Mason0b86a832008-03-24 15:01:56 -0400551 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400552 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400553 btrfs_mark_buffer_dirty(leaf);
554 ret = 0;
555
556out:
557 btrfs_free_path(path);
558 return ret;
559}
560int btrfs_update_device(struct btrfs_trans_handle *trans,
561 struct btrfs_device *device)
562{
563 int ret;
564 struct btrfs_path *path;
565 struct btrfs_root *root;
566 struct btrfs_dev_item *dev_item;
567 struct extent_buffer *leaf;
568 struct btrfs_key key;
569
570 root = device->dev_root->fs_info->chunk_root;
571
572 path = btrfs_alloc_path();
573 if (!path)
574 return -ENOMEM;
575
576 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
577 key.type = BTRFS_DEV_ITEM_KEY;
578 key.offset = device->devid;
579
580 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
581 if (ret < 0)
582 goto out;
583
584 if (ret > 0) {
585 ret = -ENOENT;
586 goto out;
587 }
588
589 leaf = path->nodes[0];
590 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
591
592 btrfs_set_device_id(leaf, dev_item, device->devid);
593 btrfs_set_device_type(leaf, dev_item, device->type);
594 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
595 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
596 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400597 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
598 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
599 btrfs_mark_buffer_dirty(leaf);
600
601out:
602 btrfs_free_path(path);
603 return ret;
604}
605
606int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
607 struct btrfs_root *root,
608 struct btrfs_key *key,
609 struct btrfs_chunk *chunk, int item_size)
610{
611 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
612 struct btrfs_disk_key disk_key;
613 u32 array_size;
614 u8 *ptr;
615
616 array_size = btrfs_super_sys_array_size(super_copy);
617 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
618 return -EFBIG;
619
620 ptr = super_copy->sys_chunk_array + array_size;
621 btrfs_cpu_key_to_disk(&disk_key, key);
622 memcpy(ptr, &disk_key, sizeof(disk_key));
623 ptr += sizeof(disk_key);
624 memcpy(ptr, chunk, item_size);
625 item_size += sizeof(disk_key);
626 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
627 return 0;
628}
629
Chris Mason9b3f68b2008-04-18 10:29:51 -0400630static u64 div_factor(u64 num, int factor)
631{
632 if (factor == 10)
633 return num;
634 num *= factor;
635 do_div(num, 10);
636 return num;
637}
638
639static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
640 int sub_stripes)
641{
642 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
643 return calc_size;
644 else if (type & BTRFS_BLOCK_GROUP_RAID10)
645 return calc_size * (num_stripes / sub_stripes);
646 else
647 return calc_size * num_stripes;
648}
649
650
Chris Mason0b86a832008-03-24 15:01:56 -0400651int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
652 struct btrfs_root *extent_root, u64 *start,
Chris Mason6324fbf2008-03-24 15:01:59 -0400653 u64 *num_bytes, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -0400654{
655 u64 dev_offset;
Chris Mason593060d2008-03-25 16:50:33 -0400656 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400657 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
658 struct btrfs_stripe *stripes;
659 struct btrfs_device *device = NULL;
660 struct btrfs_chunk *chunk;
Chris Mason6324fbf2008-03-24 15:01:59 -0400661 struct list_head private_devs;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400662 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
Chris Mason6324fbf2008-03-24 15:01:59 -0400663 struct list_head *cur;
Chris Mason0b86a832008-03-24 15:01:56 -0400664 struct extent_map_tree *em_tree;
665 struct map_lookup *map;
666 struct extent_map *em;
Chris Masona40a90a2008-04-18 11:55:51 -0400667 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400668 u64 physical;
669 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400670 u64 max_chunk_size = calc_size;
671 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -0400672 u64 avail;
673 u64 max_avail = 0;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400674 u64 percent_max;
Chris Mason6324fbf2008-03-24 15:01:59 -0400675 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -0400676 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -0400677 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -0400678 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400679 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -0400680 int index;
Chris Mason593060d2008-03-25 16:50:33 -0400681 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400682 struct btrfs_key key;
683
Chris Mason6324fbf2008-03-24 15:01:59 -0400684 if (list_empty(dev_list))
685 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -0400686
Chris Masona40a90a2008-04-18 11:55:51 -0400687 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Chris Mason593060d2008-03-25 16:50:33 -0400688 num_stripes = btrfs_super_num_devices(&info->super_copy);
Chris Masona40a90a2008-04-18 11:55:51 -0400689 min_stripes = 2;
690 }
691 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400692 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -0400693 min_stripes = 2;
694 }
Chris Mason8790d502008-04-03 16:29:03 -0400695 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
696 num_stripes = min_t(u64, 2,
697 btrfs_super_num_devices(&info->super_copy));
Chris Mason9b3f68b2008-04-18 10:29:51 -0400698 if (num_stripes < 2)
699 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -0400700 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -0400701 }
Chris Mason321aecc2008-04-16 10:49:51 -0400702 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
703 num_stripes = btrfs_super_num_devices(&info->super_copy);
704 if (num_stripes < 4)
705 return -ENOSPC;
706 num_stripes &= ~(u32)1;
707 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -0400708 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -0400709 }
Chris Mason9b3f68b2008-04-18 10:29:51 -0400710
711 if (type & BTRFS_BLOCK_GROUP_DATA) {
712 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -0400713 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400714 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
715 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -0400716 min_stripe_size = 32 * 1024 * 1024;
717 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
718 calc_size = 8 * 1024 * 1024;
719 max_chunk_size = calc_size * 2;
720 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400721 }
722
723 /* we don't want a chunk larger than 10% of the FS */
724 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
725 max_chunk_size = min(percent_max, max_chunk_size);
726
Chris Masona40a90a2008-04-18 11:55:51 -0400727again:
Chris Mason9b3f68b2008-04-18 10:29:51 -0400728 if (calc_size * num_stripes > max_chunk_size) {
729 calc_size = max_chunk_size;
730 do_div(calc_size, num_stripes);
731 do_div(calc_size, stripe_len);
732 calc_size *= stripe_len;
733 }
734 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -0400735 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -0400736
Chris Mason9b3f68b2008-04-18 10:29:51 -0400737 do_div(calc_size, stripe_len);
738 calc_size *= stripe_len;
739
Chris Mason6324fbf2008-03-24 15:01:59 -0400740 INIT_LIST_HEAD(&private_devs);
741 cur = dev_list->next;
742 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -0400743
744 if (type & BTRFS_BLOCK_GROUP_DUP)
745 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400746 else
747 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -0400748
Chris Masonad5bd912008-04-21 08:28:10 -0400749 /* we add 1MB because we never use the first 1MB of the device */
750 min_free += 1024 * 1024;
751
Chris Mason6324fbf2008-03-24 15:01:59 -0400752 /* build a private list of devices we will allocate from */
753 while(index < num_stripes) {
754 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Mason611f0e02008-04-03 16:29:03 -0400755
Chris Mason6324fbf2008-03-24 15:01:59 -0400756 avail = device->total_bytes - device->bytes_used;
757 cur = cur->next;
Chris Mason611f0e02008-04-03 16:29:03 -0400758 if (avail >= min_free) {
Chris Mason6324fbf2008-03-24 15:01:59 -0400759 list_move_tail(&device->dev_list, &private_devs);
760 index++;
Chris Mason611f0e02008-04-03 16:29:03 -0400761 if (type & BTRFS_BLOCK_GROUP_DUP)
762 index++;
Chris Masona40a90a2008-04-18 11:55:51 -0400763 } else if (avail > max_avail)
764 max_avail = avail;
Chris Mason6324fbf2008-03-24 15:01:59 -0400765 if (cur == dev_list)
766 break;
767 }
768 if (index < num_stripes) {
769 list_splice(&private_devs, dev_list);
Chris Masona40a90a2008-04-18 11:55:51 -0400770 if (index >= min_stripes) {
771 num_stripes = index;
772 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
773 num_stripes /= sub_stripes;
774 num_stripes *= sub_stripes;
775 }
776 looped = 1;
777 goto again;
778 }
Chris Mason6324fbf2008-03-24 15:01:59 -0400779 if (!looped && max_avail > 0) {
780 looped = 1;
781 calc_size = max_avail;
782 goto again;
783 }
784 return -ENOSPC;
785 }
Chris Masone17cade2008-04-15 15:41:47 -0400786 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
787 key.type = BTRFS_CHUNK_ITEM_KEY;
788 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
789 &key.offset);
Chris Mason0b86a832008-03-24 15:01:56 -0400790 if (ret)
791 return ret;
792
Chris Mason0b86a832008-03-24 15:01:56 -0400793 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
794 if (!chunk)
795 return -ENOMEM;
796
Chris Mason593060d2008-03-25 16:50:33 -0400797 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
798 if (!map) {
799 kfree(chunk);
800 return -ENOMEM;
801 }
802
Chris Mason0b86a832008-03-24 15:01:56 -0400803 stripes = &chunk->stripe;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400804 *num_bytes = chunk_bytes_by_type(type, calc_size,
805 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -0400806
Chris Mason8790d502008-04-03 16:29:03 -0400807
Chris Mason6324fbf2008-03-24 15:01:59 -0400808 index = 0;
Chris Masone17cade2008-04-15 15:41:47 -0400809printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -0400810 while(index < num_stripes) {
Chris Masone17cade2008-04-15 15:41:47 -0400811 struct btrfs_stripe *stripe;
Chris Mason6324fbf2008-03-24 15:01:59 -0400812 BUG_ON(list_empty(&private_devs));
813 cur = private_devs.next;
814 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Mason611f0e02008-04-03 16:29:03 -0400815
816 /* loop over this device again if we're doing a dup group */
817 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
818 (index == num_stripes - 1))
819 list_move_tail(&device->dev_list, dev_list);
Chris Mason0b86a832008-03-24 15:01:56 -0400820
821 ret = btrfs_alloc_dev_extent(trans, device,
Chris Masone17cade2008-04-15 15:41:47 -0400822 info->chunk_root->root_key.objectid,
823 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
824 calc_size, &dev_offset);
Chris Mason0b86a832008-03-24 15:01:56 -0400825 BUG_ON(ret);
Chris Masone17cade2008-04-15 15:41:47 -0400826printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.offset, calc_size, device->devid, type);
Chris Mason0b86a832008-03-24 15:01:56 -0400827 device->bytes_used += calc_size;
828 ret = btrfs_update_device(trans, device);
829 BUG_ON(ret);
830
Chris Mason593060d2008-03-25 16:50:33 -0400831 map->stripes[index].dev = device;
832 map->stripes[index].physical = dev_offset;
Chris Masone17cade2008-04-15 15:41:47 -0400833 stripe = stripes + index;
834 btrfs_set_stack_stripe_devid(stripe, device->devid);
835 btrfs_set_stack_stripe_offset(stripe, dev_offset);
836 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400837 physical = dev_offset;
838 index++;
839 }
Chris Mason6324fbf2008-03-24 15:01:59 -0400840 BUG_ON(!list_empty(&private_devs));
Chris Mason0b86a832008-03-24 15:01:56 -0400841
Chris Masone17cade2008-04-15 15:41:47 -0400842 /* key was set above */
843 btrfs_set_stack_chunk_length(chunk, *num_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -0400844 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
Chris Mason593060d2008-03-25 16:50:33 -0400845 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -0400846 btrfs_set_stack_chunk_type(chunk, type);
847 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -0400848 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
849 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -0400850 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
Chris Mason321aecc2008-04-16 10:49:51 -0400851 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
Chris Mason593060d2008-03-25 16:50:33 -0400852 map->sector_size = extent_root->sectorsize;
853 map->stripe_len = stripe_len;
854 map->io_align = stripe_len;
855 map->io_width = stripe_len;
856 map->type = type;
857 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -0400858 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -0400859
860 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
861 btrfs_chunk_item_size(num_stripes));
862 BUG_ON(ret);
Chris Masone17cade2008-04-15 15:41:47 -0400863 *start = key.offset;;
Chris Mason0b86a832008-03-24 15:01:56 -0400864
865 em = alloc_extent_map(GFP_NOFS);
866 if (!em)
867 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400868 em->bdev = (struct block_device *)map;
Chris Masone17cade2008-04-15 15:41:47 -0400869 em->start = key.offset;
870 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400871 em->block_start = 0;
872
Chris Mason0b86a832008-03-24 15:01:56 -0400873 kfree(chunk);
874
875 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
876 spin_lock(&em_tree->lock);
877 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -0400878 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -0400879 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400880 free_extent_map(em);
881 return ret;
882}
883
884void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
885{
886 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
887}
888
889void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
890{
891 struct extent_map *em;
892
893 while(1) {
894 spin_lock(&tree->map_tree.lock);
895 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
896 if (em)
897 remove_extent_mapping(&tree->map_tree, em);
898 spin_unlock(&tree->map_tree.lock);
899 if (!em)
900 break;
901 kfree(em->bdev);
902 /* once for us */
903 free_extent_map(em);
904 /* once for the tree */
905 free_extent_map(em);
906 }
907}
908
Chris Masonf1885912008-04-09 16:28:12 -0400909int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
910{
911 struct extent_map *em;
912 struct map_lookup *map;
913 struct extent_map_tree *em_tree = &map_tree->map_tree;
914 int ret;
915
916 spin_lock(&em_tree->lock);
917 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -0400918 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -0400919 BUG_ON(!em);
920
921 BUG_ON(em->start > logical || em->start + em->len < logical);
922 map = (struct map_lookup *)em->bdev;
923 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
924 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -0400925 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
926 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -0400927 else
928 ret = 1;
929 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -0400930 return ret;
931}
932
Chris Mason8790d502008-04-03 16:29:03 -0400933int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
Chris Masoncea9e442008-04-09 16:28:12 -0400934 u64 logical, u64 *length,
Chris Masonf1885912008-04-09 16:28:12 -0400935 struct btrfs_multi_bio **multi_ret, int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -0400936{
937 struct extent_map *em;
938 struct map_lookup *map;
939 struct extent_map_tree *em_tree = &map_tree->map_tree;
940 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -0400941 u64 stripe_offset;
942 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -0400943 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -0400944 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -0400945 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -0400946 int i;
947 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400948
Chris Masoncea9e442008-04-09 16:28:12 -0400949 if (multi_ret && !(rw & (1 << BIO_RW))) {
950 stripes_allocated = 1;
951 }
952again:
953 if (multi_ret) {
954 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
955 GFP_NOFS);
956 if (!multi)
957 return -ENOMEM;
958 }
Chris Mason0b86a832008-03-24 15:01:56 -0400959
960 spin_lock(&em_tree->lock);
961 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -0400962 spin_unlock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -0400963 if (!em) {
964 printk("unable to find logical %Lu\n", logical);
965 }
Chris Mason0b86a832008-03-24 15:01:56 -0400966 BUG_ON(!em);
967
968 BUG_ON(em->start > logical || em->start + em->len < logical);
969 map = (struct map_lookup *)em->bdev;
970 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -0400971
Chris Masonf1885912008-04-09 16:28:12 -0400972 if (mirror_num > map->num_stripes)
973 mirror_num = 0;
974
Chris Masoncea9e442008-04-09 16:28:12 -0400975 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -0400976 if (rw & (1 << BIO_RW)) {
977 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
978 BTRFS_BLOCK_GROUP_DUP)) {
979 stripes_required = map->num_stripes;
980 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
981 stripes_required = map->sub_stripes;
982 }
983 }
984 if (multi_ret && rw == WRITE &&
985 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -0400986 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -0400987 free_extent_map(em);
988 kfree(multi);
989 goto again;
990 }
Chris Mason593060d2008-03-25 16:50:33 -0400991 stripe_nr = offset;
992 /*
993 * stripe_nr counts the total number of stripes we have to stride
994 * to get to this block
995 */
996 do_div(stripe_nr, map->stripe_len);
997
998 stripe_offset = stripe_nr * map->stripe_len;
999 BUG_ON(offset < stripe_offset);
1000
1001 /* stripe_offset is the offset of this block in its stripe*/
1002 stripe_offset = offset - stripe_offset;
1003
Chris Masoncea9e442008-04-09 16:28:12 -04001004 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001005 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04001006 BTRFS_BLOCK_GROUP_DUP)) {
1007 /* we limit the length of each bio to what fits in a stripe */
1008 *length = min_t(u64, em->len - offset,
1009 map->stripe_len - stripe_offset);
1010 } else {
1011 *length = em->len - offset;
1012 }
1013 if (!multi_ret)
1014 goto out;
1015
1016 multi->num_stripes = 1;
1017 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001018 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Mason8790d502008-04-03 16:29:03 -04001019 if (rw & (1 << BIO_RW))
Chris Masoncea9e442008-04-09 16:28:12 -04001020 multi->num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001021 else if (mirror_num) {
1022 stripe_index = mirror_num - 1;
1023 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001024 int i;
1025 u64 least = (u64)-1;
1026 struct btrfs_device *cur;
Chris Mason593060d2008-03-25 16:50:33 -04001027
Chris Mason8790d502008-04-03 16:29:03 -04001028 for (i = 0; i < map->num_stripes; i++) {
1029 cur = map->stripes[i].dev;
1030 spin_lock(&cur->io_lock);
1031 if (cur->total_ios < least) {
1032 least = cur->total_ios;
1033 stripe_index = i;
1034 }
1035 spin_unlock(&cur->io_lock);
1036 }
Chris Mason8790d502008-04-03 16:29:03 -04001037 }
Chris Mason611f0e02008-04-03 16:29:03 -04001038 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04001039 if (rw & (1 << BIO_RW))
1040 multi->num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001041 else if (mirror_num)
1042 stripe_index = mirror_num - 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001043 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1044 int factor = map->num_stripes / map->sub_stripes;
1045 int orig_stripe_nr = stripe_nr;
1046
1047 stripe_index = do_div(stripe_nr, factor);
1048 stripe_index *= map->sub_stripes;
1049
1050 if (rw & (1 << BIO_RW))
1051 multi->num_stripes = map->sub_stripes;
1052 else if (mirror_num)
1053 stripe_index += mirror_num - 1;
1054 else
1055 stripe_index += orig_stripe_nr % map->sub_stripes;
Chris Mason8790d502008-04-03 16:29:03 -04001056 } else {
1057 /*
1058 * after this do_div call, stripe_nr is the number of stripes
1059 * on this device we have to walk to find the data, and
1060 * stripe_index is the number of our device in the stripe array
1061 */
1062 stripe_index = do_div(stripe_nr, map->num_stripes);
1063 }
Chris Mason593060d2008-03-25 16:50:33 -04001064 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001065
Chris Masoncea9e442008-04-09 16:28:12 -04001066 for (i = 0; i < multi->num_stripes; i++) {
1067 multi->stripes[i].physical =
1068 map->stripes[stripe_index].physical + stripe_offset +
1069 stripe_nr * map->stripe_len;
1070 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1071 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04001072 }
Chris Masoncea9e442008-04-09 16:28:12 -04001073 *multi_ret = multi;
1074out:
Chris Mason0b86a832008-03-24 15:01:56 -04001075 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04001076 return 0;
1077}
1078
Chris Mason8790d502008-04-03 16:29:03 -04001079#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1080static void end_bio_multi_stripe(struct bio *bio, int err)
1081#else
1082static int end_bio_multi_stripe(struct bio *bio,
1083 unsigned int bytes_done, int err)
1084#endif
1085{
Chris Masoncea9e442008-04-09 16:28:12 -04001086 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason8790d502008-04-03 16:29:03 -04001087
1088#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1089 if (bio->bi_size)
1090 return 1;
1091#endif
1092 if (err)
1093 multi->error = err;
1094
Chris Masoncea9e442008-04-09 16:28:12 -04001095 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason8790d502008-04-03 16:29:03 -04001096 bio->bi_private = multi->private;
1097 bio->bi_end_io = multi->end_io;
1098
1099 if (!err && multi->error)
1100 err = multi->error;
1101 kfree(multi);
1102
Miguel73f61b22008-04-11 15:50:59 -04001103#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1104 bio_endio(bio, bio->bi_size, err);
1105#else
Chris Mason8790d502008-04-03 16:29:03 -04001106 bio_endio(bio, err);
Miguel73f61b22008-04-11 15:50:59 -04001107#endif
Chris Mason8790d502008-04-03 16:29:03 -04001108 } else {
1109 bio_put(bio);
1110 }
1111#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1112 return 0;
1113#endif
1114}
1115
Chris Masonf1885912008-04-09 16:28:12 -04001116int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1117 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04001118{
1119 struct btrfs_mapping_tree *map_tree;
1120 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04001121 struct bio *first_bio = bio;
Chris Mason0b86a832008-03-24 15:01:56 -04001122 u64 logical = bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04001123 u64 length = 0;
1124 u64 map_length;
1125 struct bio_vec *bvec;
Chris Masoncea9e442008-04-09 16:28:12 -04001126 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001127 int i;
1128 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04001129 int dev_nr = 0;
1130 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001131
1132 bio_for_each_segment(bvec, bio, i) {
1133 length += bvec->bv_len;
1134 }
Chris Mason8790d502008-04-03 16:29:03 -04001135
Chris Mason0b86a832008-03-24 15:01:56 -04001136 map_tree = &root->fs_info->mapping_tree;
1137 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001138
Chris Masonf1885912008-04-09 16:28:12 -04001139 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
1140 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04001141 BUG_ON(ret);
1142
1143 total_devs = multi->num_stripes;
1144 if (map_length < length) {
1145 printk("mapping failed logical %Lu bio len %Lu "
1146 "len %Lu\n", logical, length, map_length);
1147 BUG();
1148 }
1149 multi->end_io = first_bio->bi_end_io;
1150 multi->private = first_bio->bi_private;
1151 atomic_set(&multi->stripes_pending, multi->num_stripes);
1152
Chris Mason8790d502008-04-03 16:29:03 -04001153 while(dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04001154 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04001155 if (dev_nr < total_devs - 1) {
1156 bio = bio_clone(first_bio, GFP_NOFS);
1157 BUG_ON(!bio);
1158 } else {
1159 bio = first_bio;
1160 }
1161 bio->bi_private = multi;
1162 bio->bi_end_io = end_bio_multi_stripe;
1163 }
Chris Masoncea9e442008-04-09 16:28:12 -04001164 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1165 dev = multi->stripes[dev_nr].dev;
Chris Mason8790d502008-04-03 16:29:03 -04001166 bio->bi_bdev = dev->bdev;
1167 spin_lock(&dev->io_lock);
1168 dev->total_ios++;
1169 spin_unlock(&dev->io_lock);
1170 submit_bio(rw, bio);
1171 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04001172 }
Chris Masoncea9e442008-04-09 16:28:12 -04001173 if (total_devs == 1)
1174 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04001175 return 0;
1176}
1177
Chris Masona4437552008-04-18 10:29:38 -04001178struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1179 u8 *uuid)
Chris Mason0b86a832008-03-24 15:01:56 -04001180{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001181 struct list_head *head = &root->fs_info->fs_devices->devices;
Chris Mason0b86a832008-03-24 15:01:56 -04001182
Chris Masona4437552008-04-18 10:29:38 -04001183 return __find_device(head, devid, uuid);
Chris Mason0b86a832008-03-24 15:01:56 -04001184}
1185
1186static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1187 struct extent_buffer *leaf,
1188 struct btrfs_chunk *chunk)
1189{
1190 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1191 struct map_lookup *map;
1192 struct extent_map *em;
1193 u64 logical;
1194 u64 length;
1195 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04001196 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04001197 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04001198 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04001199 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04001200
Chris Masone17cade2008-04-15 15:41:47 -04001201 logical = key->offset;
1202 length = btrfs_chunk_length(leaf, chunk);
Chris Mason0b86a832008-03-24 15:01:56 -04001203 spin_lock(&map_tree->map_tree.lock);
1204 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04001205 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04001206
1207 /* already mapped? */
1208 if (em && em->start <= logical && em->start + em->len > logical) {
1209 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04001210 return 0;
1211 } else if (em) {
1212 free_extent_map(em);
1213 }
Chris Mason0b86a832008-03-24 15:01:56 -04001214
1215 map = kzalloc(sizeof(*map), GFP_NOFS);
1216 if (!map)
1217 return -ENOMEM;
1218
1219 em = alloc_extent_map(GFP_NOFS);
1220 if (!em)
1221 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04001222 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1223 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04001224 if (!map) {
1225 free_extent_map(em);
1226 return -ENOMEM;
1227 }
1228
1229 em->bdev = (struct block_device *)map;
1230 em->start = logical;
1231 em->len = length;
1232 em->block_start = 0;
1233
Chris Mason593060d2008-03-25 16:50:33 -04001234 map->num_stripes = num_stripes;
1235 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1236 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1237 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1238 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1239 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04001240 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04001241 for (i = 0; i < num_stripes; i++) {
1242 map->stripes[i].physical =
1243 btrfs_stripe_offset_nr(leaf, chunk, i);
1244 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04001245 read_extent_buffer(leaf, uuid, (unsigned long)
1246 btrfs_stripe_dev_uuid_nr(chunk, i),
1247 BTRFS_UUID_SIZE);
1248 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
Chris Mason593060d2008-03-25 16:50:33 -04001249 if (!map->stripes[i].dev) {
1250 kfree(map);
1251 free_extent_map(em);
1252 return -EIO;
1253 }
Chris Mason0b86a832008-03-24 15:01:56 -04001254 }
1255
1256 spin_lock(&map_tree->map_tree.lock);
1257 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04001258 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04001259 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001260 free_extent_map(em);
1261
1262 return 0;
1263}
1264
1265static int fill_device_from_item(struct extent_buffer *leaf,
1266 struct btrfs_dev_item *dev_item,
1267 struct btrfs_device *device)
1268{
1269 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001270
1271 device->devid = btrfs_device_id(leaf, dev_item);
1272 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1273 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1274 device->type = btrfs_device_type(leaf, dev_item);
1275 device->io_align = btrfs_device_io_align(leaf, dev_item);
1276 device->io_width = btrfs_device_io_width(leaf, dev_item);
1277 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04001278
1279 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001280 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001281
Chris Mason0b86a832008-03-24 15:01:56 -04001282 return 0;
1283}
1284
Chris Mason0d81ba52008-03-24 15:02:07 -04001285static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04001286 struct extent_buffer *leaf,
1287 struct btrfs_dev_item *dev_item)
1288{
1289 struct btrfs_device *device;
1290 u64 devid;
1291 int ret;
Chris Masona4437552008-04-18 10:29:38 -04001292 u8 dev_uuid[BTRFS_UUID_SIZE];
1293
Chris Mason0b86a832008-03-24 15:01:56 -04001294 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04001295 read_extent_buffer(leaf, dev_uuid,
1296 (unsigned long)btrfs_device_uuid(dev_item),
1297 BTRFS_UUID_SIZE);
1298 device = btrfs_find_device(root, devid, dev_uuid);
Chris Mason6324fbf2008-03-24 15:01:59 -04001299 if (!device) {
Chris Mason8a4b83c2008-03-24 15:02:07 -04001300 printk("warning devid %Lu not found already\n", devid);
Chris Masonf2984462008-04-10 16:19:33 -04001301 device = kzalloc(sizeof(*device), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04001302 if (!device)
1303 return -ENOMEM;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001304 list_add(&device->dev_list,
1305 &root->fs_info->fs_devices->devices);
Chris Masonb248a412008-04-14 09:48:18 -04001306 device->barriers = 1;
Chris Mason8790d502008-04-03 16:29:03 -04001307 spin_lock_init(&device->io_lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04001308 }
Chris Mason0b86a832008-03-24 15:01:56 -04001309
1310 fill_device_from_item(leaf, dev_item, device);
1311 device->dev_root = root->fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001312 ret = 0;
1313#if 0
1314 ret = btrfs_open_device(device);
1315 if (ret) {
1316 kfree(device);
1317 }
1318#endif
1319 return ret;
1320}
1321
Chris Mason0d81ba52008-03-24 15:02:07 -04001322int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1323{
1324 struct btrfs_dev_item *dev_item;
1325
1326 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1327 dev_item);
1328 return read_one_dev(root, buf, dev_item);
1329}
1330
Chris Mason0b86a832008-03-24 15:01:56 -04001331int btrfs_read_sys_array(struct btrfs_root *root)
1332{
1333 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1334 struct extent_buffer *sb = root->fs_info->sb_buffer;
1335 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04001336 struct btrfs_chunk *chunk;
1337 struct btrfs_key key;
1338 u32 num_stripes;
1339 u32 array_size;
1340 u32 len = 0;
1341 u8 *ptr;
1342 unsigned long sb_ptr;
1343 u32 cur;
1344 int ret;
Chris Mason0b86a832008-03-24 15:01:56 -04001345
1346 array_size = btrfs_super_sys_array_size(super_copy);
1347
1348 /*
1349 * we do this loop twice, once for the device items and
1350 * once for all of the chunks. This way there are device
1351 * structs filled in for every chunk
1352 */
Chris Mason0b86a832008-03-24 15:01:56 -04001353 ptr = super_copy->sys_chunk_array;
1354 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1355 cur = 0;
1356
1357 while (cur < array_size) {
1358 disk_key = (struct btrfs_disk_key *)ptr;
1359 btrfs_disk_key_to_cpu(&key, disk_key);
1360
1361 len = sizeof(*disk_key);
1362 ptr += len;
1363 sb_ptr += len;
1364 cur += len;
1365
Chris Mason0d81ba52008-03-24 15:02:07 -04001366 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04001367 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04001368 ret = read_one_chunk(root, &key, sb, chunk);
1369 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001370 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1371 len = btrfs_chunk_item_size(num_stripes);
1372 } else {
1373 BUG();
1374 }
1375 ptr += len;
1376 sb_ptr += len;
1377 cur += len;
1378 }
Chris Mason0b86a832008-03-24 15:01:56 -04001379 return 0;
1380}
1381
1382int btrfs_read_chunk_tree(struct btrfs_root *root)
1383{
1384 struct btrfs_path *path;
1385 struct extent_buffer *leaf;
1386 struct btrfs_key key;
1387 struct btrfs_key found_key;
1388 int ret;
1389 int slot;
1390
1391 root = root->fs_info->chunk_root;
1392
1393 path = btrfs_alloc_path();
1394 if (!path)
1395 return -ENOMEM;
1396
1397 /* first we search for all of the device items, and then we
1398 * read in all of the chunk items. This way we can create chunk
1399 * mappings that reference all of the devices that are afound
1400 */
1401 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1402 key.offset = 0;
1403 key.type = 0;
1404again:
1405 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1406 while(1) {
1407 leaf = path->nodes[0];
1408 slot = path->slots[0];
1409 if (slot >= btrfs_header_nritems(leaf)) {
1410 ret = btrfs_next_leaf(root, path);
1411 if (ret == 0)
1412 continue;
1413 if (ret < 0)
1414 goto error;
1415 break;
1416 }
1417 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1418 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1419 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1420 break;
1421 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1422 struct btrfs_dev_item *dev_item;
1423 dev_item = btrfs_item_ptr(leaf, slot,
1424 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04001425 ret = read_one_dev(root, leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04001426 BUG_ON(ret);
1427 }
1428 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1429 struct btrfs_chunk *chunk;
1430 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1431 ret = read_one_chunk(root, &found_key, leaf, chunk);
1432 }
1433 path->slots[0]++;
1434 }
1435 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1436 key.objectid = 0;
1437 btrfs_release_path(root, path);
1438 goto again;
1439 }
1440
1441 btrfs_free_path(path);
1442 ret = 0;
1443error:
1444 return ret;
1445}
1446