blob: 460240706505764f82169b2becea0a37b82488ce [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 Mason593060d2008-03-25 16:50:33 -040022#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040023#include "ctree.h"
24#include "extent_map.h"
25#include "disk-io.h"
26#include "transaction.h"
27#include "print-tree.h"
28#include "volumes.h"
29
Chris Mason593060d2008-03-25 16:50:33 -040030struct map_lookup {
31 u64 type;
32 int io_align;
33 int io_width;
34 int stripe_len;
35 int sector_size;
36 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040037 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040038 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040039};
40
41#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040042 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040043
Chris Mason8a4b83c2008-03-24 15:02:07 -040044static DEFINE_MUTEX(uuid_mutex);
45static LIST_HEAD(fs_uuids);
46
47int btrfs_cleanup_fs_uuids(void)
48{
49 struct btrfs_fs_devices *fs_devices;
50 struct list_head *uuid_cur;
51 struct list_head *devices_cur;
52 struct btrfs_device *dev;
53
54 list_for_each(uuid_cur, &fs_uuids) {
55 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
56 list);
57 while(!list_empty(&fs_devices->devices)) {
58 devices_cur = fs_devices->devices.next;
59 dev = list_entry(devices_cur, struct btrfs_device,
60 dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -040061 if (dev->bdev) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040062 close_bdev_excl(dev->bdev);
63 }
64 list_del(&dev->dev_list);
65 kfree(dev);
66 }
67 }
68 return 0;
69}
70
Chris Masona4437552008-04-18 10:29:38 -040071static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
72 u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040073{
74 struct btrfs_device *dev;
75 struct list_head *cur;
76
77 list_for_each(cur, head) {
78 dev = list_entry(cur, struct btrfs_device, dev_list);
Chris Masona4437552008-04-18 10:29:38 -040079 if (dev->devid == devid &&
80 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040081 return dev;
Chris Masona4437552008-04-18 10:29:38 -040082 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040083 }
84 return NULL;
85}
86
87static struct btrfs_fs_devices *find_fsid(u8 *fsid)
88{
89 struct list_head *cur;
90 struct btrfs_fs_devices *fs_devices;
91
92 list_for_each(cur, &fs_uuids) {
93 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
94 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
95 return fs_devices;
96 }
97 return NULL;
98}
99
100static int device_list_add(const char *path,
101 struct btrfs_super_block *disk_super,
102 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
103{
104 struct btrfs_device *device;
105 struct btrfs_fs_devices *fs_devices;
106 u64 found_transid = btrfs_super_generation(disk_super);
107
108 fs_devices = find_fsid(disk_super->fsid);
109 if (!fs_devices) {
110 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
111 if (!fs_devices)
112 return -ENOMEM;
113 INIT_LIST_HEAD(&fs_devices->devices);
114 list_add(&fs_devices->list, &fs_uuids);
115 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
116 fs_devices->latest_devid = devid;
117 fs_devices->latest_trans = found_transid;
118 fs_devices->lowest_devid = (u64)-1;
119 fs_devices->num_devices = 0;
120 device = NULL;
121 } else {
Chris Masona4437552008-04-18 10:29:38 -0400122 device = __find_device(&fs_devices->devices, devid,
123 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400124 }
125 if (!device) {
126 device = kzalloc(sizeof(*device), GFP_NOFS);
127 if (!device) {
128 /* we can safely leave the fs_devices entry around */
129 return -ENOMEM;
130 }
131 device->devid = devid;
Chris Masona4437552008-04-18 10:29:38 -0400132 memcpy(device->uuid, disk_super->dev_item.uuid,
133 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400134 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400135 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136 device->name = kstrdup(path, GFP_NOFS);
137 if (!device->name) {
138 kfree(device);
139 return -ENOMEM;
140 }
141 list_add(&device->dev_list, &fs_devices->devices);
142 fs_devices->num_devices++;
143 }
144
145 if (found_transid > fs_devices->latest_trans) {
146 fs_devices->latest_devid = devid;
147 fs_devices->latest_trans = found_transid;
148 }
149 if (fs_devices->lowest_devid > devid) {
150 fs_devices->lowest_devid = devid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400151 }
152 *fs_devices_ret = fs_devices;
153 return 0;
154}
155
156int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
157{
158 struct list_head *head = &fs_devices->devices;
159 struct list_head *cur;
160 struct btrfs_device *device;
161
162 mutex_lock(&uuid_mutex);
163 list_for_each(cur, head) {
164 device = list_entry(cur, struct btrfs_device, dev_list);
165 if (device->bdev) {
166 close_bdev_excl(device->bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400167 }
168 device->bdev = NULL;
169 }
170 mutex_unlock(&uuid_mutex);
171 return 0;
172}
173
174int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
175 int flags, void *holder)
176{
177 struct block_device *bdev;
178 struct list_head *head = &fs_devices->devices;
179 struct list_head *cur;
180 struct btrfs_device *device;
181 int ret;
182
183 mutex_lock(&uuid_mutex);
184 list_for_each(cur, head) {
185 device = list_entry(cur, struct btrfs_device, dev_list);
186 bdev = open_bdev_excl(device->name, flags, holder);
Chris Masone17cade2008-04-15 15:41:47 -0400187
Chris Mason8a4b83c2008-03-24 15:02:07 -0400188 if (IS_ERR(bdev)) {
189 printk("open %s failed\n", device->name);
190 ret = PTR_ERR(bdev);
191 goto fail;
192 }
193 if (device->devid == fs_devices->latest_devid)
194 fs_devices->latest_bdev = bdev;
195 if (device->devid == fs_devices->lowest_devid) {
196 fs_devices->lowest_bdev = bdev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400197 }
198 device->bdev = bdev;
199 }
200 mutex_unlock(&uuid_mutex);
201 return 0;
202fail:
203 mutex_unlock(&uuid_mutex);
204 btrfs_close_devices(fs_devices);
205 return ret;
206}
207
208int btrfs_scan_one_device(const char *path, int flags, void *holder,
209 struct btrfs_fs_devices **fs_devices_ret)
210{
211 struct btrfs_super_block *disk_super;
212 struct block_device *bdev;
213 struct buffer_head *bh;
214 int ret;
215 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400216 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400217
218 mutex_lock(&uuid_mutex);
219
Chris Mason8a4b83c2008-03-24 15:02:07 -0400220 bdev = open_bdev_excl(path, flags, holder);
221
222 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400223 ret = PTR_ERR(bdev);
224 goto error;
225 }
226
227 ret = set_blocksize(bdev, 4096);
228 if (ret)
229 goto error_close;
230 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
231 if (!bh) {
232 ret = -EIO;
233 goto error_close;
234 }
235 disk_super = (struct btrfs_super_block *)bh->b_data;
236 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
237 sizeof(disk_super->magic))) {
Yane58ca022008-04-01 11:21:34 -0400238 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400239 goto error_brelse;
240 }
241 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400242 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400243 if (disk_super->label[0])
244 printk("device label %s ", disk_super->label);
245 else {
246 /* FIXME, make a readl uuid parser */
247 printk("device fsid %llx-%llx ",
248 *(unsigned long long *)disk_super->fsid,
249 *(unsigned long long *)(disk_super->fsid + 8));
250 }
251 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400252 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
253
254error_brelse:
255 brelse(bh);
256error_close:
257 close_bdev_excl(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400258error:
259 mutex_unlock(&uuid_mutex);
260 return ret;
261}
Chris Mason0b86a832008-03-24 15:01:56 -0400262
263/*
264 * this uses a pretty simple search, the expectation is that it is
265 * called very infrequently and that a given device has a small number
266 * of extents
267 */
268static int find_free_dev_extent(struct btrfs_trans_handle *trans,
269 struct btrfs_device *device,
270 struct btrfs_path *path,
271 u64 num_bytes, u64 *start)
272{
273 struct btrfs_key key;
274 struct btrfs_root *root = device->dev_root;
275 struct btrfs_dev_extent *dev_extent = NULL;
276 u64 hole_size = 0;
277 u64 last_byte = 0;
278 u64 search_start = 0;
279 u64 search_end = device->total_bytes;
280 int ret;
281 int slot = 0;
282 int start_found;
283 struct extent_buffer *l;
284
285 start_found = 0;
286 path->reada = 2;
287
288 /* FIXME use last free of some kind */
289
Chris Mason8a4b83c2008-03-24 15:02:07 -0400290 /* we don't want to overwrite the superblock on the drive,
291 * so we make sure to start at an offset of at least 1MB
292 */
293 search_start = max((u64)1024 * 1024, search_start);
Chris Mason0b86a832008-03-24 15:01:56 -0400294 key.objectid = device->devid;
295 key.offset = search_start;
296 key.type = BTRFS_DEV_EXTENT_KEY;
297 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
298 if (ret < 0)
299 goto error;
300 ret = btrfs_previous_item(root, path, 0, key.type);
301 if (ret < 0)
302 goto error;
303 l = path->nodes[0];
304 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
305 while (1) {
306 l = path->nodes[0];
307 slot = path->slots[0];
308 if (slot >= btrfs_header_nritems(l)) {
309 ret = btrfs_next_leaf(root, path);
310 if (ret == 0)
311 continue;
312 if (ret < 0)
313 goto error;
314no_more_items:
315 if (!start_found) {
316 if (search_start >= search_end) {
317 ret = -ENOSPC;
318 goto error;
319 }
320 *start = search_start;
321 start_found = 1;
322 goto check_pending;
323 }
324 *start = last_byte > search_start ?
325 last_byte : search_start;
326 if (search_end <= *start) {
327 ret = -ENOSPC;
328 goto error;
329 }
330 goto check_pending;
331 }
332 btrfs_item_key_to_cpu(l, &key, slot);
333
334 if (key.objectid < device->devid)
335 goto next;
336
337 if (key.objectid > device->devid)
338 goto no_more_items;
339
340 if (key.offset >= search_start && key.offset > last_byte &&
341 start_found) {
342 if (last_byte < search_start)
343 last_byte = search_start;
344 hole_size = key.offset - last_byte;
345 if (key.offset > last_byte &&
346 hole_size >= num_bytes) {
347 *start = last_byte;
348 goto check_pending;
349 }
350 }
351 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
352 goto next;
353 }
354
355 start_found = 1;
356 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
357 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
358next:
359 path->slots[0]++;
360 cond_resched();
361 }
362check_pending:
363 /* we have to make sure we didn't find an extent that has already
364 * been allocated by the map tree or the original allocation
365 */
366 btrfs_release_path(root, path);
367 BUG_ON(*start < search_start);
368
Chris Mason6324fbf2008-03-24 15:01:59 -0400369 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400370 ret = -ENOSPC;
371 goto error;
372 }
373 /* check for pending inserts here */
374 return 0;
375
376error:
377 btrfs_release_path(root, path);
378 return ret;
379}
380
381int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
382 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400383 u64 chunk_tree, u64 chunk_objectid,
384 u64 chunk_offset,
385 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400386{
387 int ret;
388 struct btrfs_path *path;
389 struct btrfs_root *root = device->dev_root;
390 struct btrfs_dev_extent *extent;
391 struct extent_buffer *leaf;
392 struct btrfs_key key;
393
394 path = btrfs_alloc_path();
395 if (!path)
396 return -ENOMEM;
397
398 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
Chris Mason6324fbf2008-03-24 15:01:59 -0400399 if (ret) {
Chris Mason0b86a832008-03-24 15:01:56 -0400400 goto err;
Chris Mason6324fbf2008-03-24 15:01:59 -0400401 }
Chris Mason0b86a832008-03-24 15:01:56 -0400402
403 key.objectid = device->devid;
404 key.offset = *start;
405 key.type = BTRFS_DEV_EXTENT_KEY;
406 ret = btrfs_insert_empty_item(trans, root, path, &key,
407 sizeof(*extent));
408 BUG_ON(ret);
409
410 leaf = path->nodes[0];
411 extent = btrfs_item_ptr(leaf, path->slots[0],
412 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400413 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
414 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
415 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
416
417 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
418 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
419 BTRFS_UUID_SIZE);
420
Chris Mason0b86a832008-03-24 15:01:56 -0400421 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
422 btrfs_mark_buffer_dirty(leaf);
423err:
424 btrfs_free_path(path);
425 return ret;
426}
427
Chris Masone17cade2008-04-15 15:41:47 -0400428static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400429{
430 struct btrfs_path *path;
431 int ret;
432 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400433 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400434 struct btrfs_key found_key;
435
436 path = btrfs_alloc_path();
437 BUG_ON(!path);
438
Chris Masone17cade2008-04-15 15:41:47 -0400439 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400440 key.offset = (u64)-1;
441 key.type = BTRFS_CHUNK_ITEM_KEY;
442
443 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
444 if (ret < 0)
445 goto error;
446
447 BUG_ON(ret == 0);
448
449 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
450 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400451 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400452 } else {
453 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
454 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400455 if (found_key.objectid != objectid)
456 *offset = 0;
457 else {
458 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
459 struct btrfs_chunk);
460 *offset = found_key.offset +
461 btrfs_chunk_length(path->nodes[0], chunk);
462 }
Chris Mason0b86a832008-03-24 15:01:56 -0400463 }
464 ret = 0;
465error:
466 btrfs_free_path(path);
467 return ret;
468}
469
Chris Mason0b86a832008-03-24 15:01:56 -0400470static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
471 u64 *objectid)
472{
473 int ret;
474 struct btrfs_key key;
475 struct btrfs_key found_key;
476
477 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
478 key.type = BTRFS_DEV_ITEM_KEY;
479 key.offset = (u64)-1;
480
481 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
482 if (ret < 0)
483 goto error;
484
485 BUG_ON(ret == 0);
486
487 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
488 BTRFS_DEV_ITEM_KEY);
489 if (ret) {
490 *objectid = 1;
491 } else {
492 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
493 path->slots[0]);
494 *objectid = found_key.offset + 1;
495 }
496 ret = 0;
497error:
498 btrfs_release_path(root, path);
499 return ret;
500}
501
502/*
503 * the device information is stored in the chunk root
504 * the btrfs_device struct should be fully filled in
505 */
506int btrfs_add_device(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
508 struct btrfs_device *device)
509{
510 int ret;
511 struct btrfs_path *path;
512 struct btrfs_dev_item *dev_item;
513 struct extent_buffer *leaf;
514 struct btrfs_key key;
515 unsigned long ptr;
516 u64 free_devid;
517
518 root = root->fs_info->chunk_root;
519
520 path = btrfs_alloc_path();
521 if (!path)
522 return -ENOMEM;
523
524 ret = find_next_devid(root, path, &free_devid);
525 if (ret)
526 goto out;
527
528 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
529 key.type = BTRFS_DEV_ITEM_KEY;
530 key.offset = free_devid;
531
532 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400533 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400534 if (ret)
535 goto out;
536
537 leaf = path->nodes[0];
538 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
539
Chris Mason8a4b83c2008-03-24 15:02:07 -0400540 device->devid = free_devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400541 btrfs_set_device_id(leaf, dev_item, device->devid);
542 btrfs_set_device_type(leaf, dev_item, device->type);
543 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
544 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
545 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400546 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
547 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400548 btrfs_set_device_group(leaf, dev_item, 0);
549 btrfs_set_device_seek_speed(leaf, dev_item, 0);
550 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400551
Chris Mason0b86a832008-03-24 15:01:56 -0400552 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400553 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400554 btrfs_mark_buffer_dirty(leaf);
555 ret = 0;
556
557out:
558 btrfs_free_path(path);
559 return ret;
560}
561int btrfs_update_device(struct btrfs_trans_handle *trans,
562 struct btrfs_device *device)
563{
564 int ret;
565 struct btrfs_path *path;
566 struct btrfs_root *root;
567 struct btrfs_dev_item *dev_item;
568 struct extent_buffer *leaf;
569 struct btrfs_key key;
570
571 root = device->dev_root->fs_info->chunk_root;
572
573 path = btrfs_alloc_path();
574 if (!path)
575 return -ENOMEM;
576
577 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
578 key.type = BTRFS_DEV_ITEM_KEY;
579 key.offset = device->devid;
580
581 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
582 if (ret < 0)
583 goto out;
584
585 if (ret > 0) {
586 ret = -ENOENT;
587 goto out;
588 }
589
590 leaf = path->nodes[0];
591 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
592
593 btrfs_set_device_id(leaf, dev_item, device->devid);
594 btrfs_set_device_type(leaf, dev_item, device->type);
595 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
596 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
597 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400598 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
599 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
600 btrfs_mark_buffer_dirty(leaf);
601
602out:
603 btrfs_free_path(path);
604 return ret;
605}
606
607int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
608 struct btrfs_root *root,
609 struct btrfs_key *key,
610 struct btrfs_chunk *chunk, int item_size)
611{
612 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
613 struct btrfs_disk_key disk_key;
614 u32 array_size;
615 u8 *ptr;
616
617 array_size = btrfs_super_sys_array_size(super_copy);
618 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
619 return -EFBIG;
620
621 ptr = super_copy->sys_chunk_array + array_size;
622 btrfs_cpu_key_to_disk(&disk_key, key);
623 memcpy(ptr, &disk_key, sizeof(disk_key));
624 ptr += sizeof(disk_key);
625 memcpy(ptr, chunk, item_size);
626 item_size += sizeof(disk_key);
627 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
628 return 0;
629}
630
Chris Mason9b3f68b2008-04-18 10:29:51 -0400631static u64 div_factor(u64 num, int factor)
632{
633 if (factor == 10)
634 return num;
635 num *= factor;
636 do_div(num, 10);
637 return num;
638}
639
640static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
641 int sub_stripes)
642{
643 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
644 return calc_size;
645 else if (type & BTRFS_BLOCK_GROUP_RAID10)
646 return calc_size * (num_stripes / sub_stripes);
647 else
648 return calc_size * num_stripes;
649}
650
651
Chris Mason0b86a832008-03-24 15:01:56 -0400652int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
653 struct btrfs_root *extent_root, u64 *start,
Chris Mason6324fbf2008-03-24 15:01:59 -0400654 u64 *num_bytes, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -0400655{
656 u64 dev_offset;
Chris Mason593060d2008-03-25 16:50:33 -0400657 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400658 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
659 struct btrfs_stripe *stripes;
660 struct btrfs_device *device = NULL;
661 struct btrfs_chunk *chunk;
Chris Mason6324fbf2008-03-24 15:01:59 -0400662 struct list_head private_devs;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400663 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
Chris Mason6324fbf2008-03-24 15:01:59 -0400664 struct list_head *cur;
Chris Mason0b86a832008-03-24 15:01:56 -0400665 struct extent_map_tree *em_tree;
666 struct map_lookup *map;
667 struct extent_map *em;
Chris Masona40a90a2008-04-18 11:55:51 -0400668 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400669 u64 physical;
670 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400671 u64 max_chunk_size = calc_size;
672 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -0400673 u64 avail;
674 u64 max_avail = 0;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400675 u64 percent_max;
Chris Mason6324fbf2008-03-24 15:01:59 -0400676 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -0400677 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -0400678 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -0400679 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400680 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -0400681 int index;
Chris Mason593060d2008-03-25 16:50:33 -0400682 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400683 struct btrfs_key key;
684
Chris Mason6324fbf2008-03-24 15:01:59 -0400685 if (list_empty(dev_list))
686 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -0400687
Chris Masona40a90a2008-04-18 11:55:51 -0400688 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Chris Mason593060d2008-03-25 16:50:33 -0400689 num_stripes = btrfs_super_num_devices(&info->super_copy);
Chris Masona40a90a2008-04-18 11:55:51 -0400690 min_stripes = 2;
691 }
692 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400693 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -0400694 min_stripes = 2;
695 }
Chris Mason8790d502008-04-03 16:29:03 -0400696 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
697 num_stripes = min_t(u64, 2,
698 btrfs_super_num_devices(&info->super_copy));
Chris Mason9b3f68b2008-04-18 10:29:51 -0400699 if (num_stripes < 2)
700 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -0400701 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -0400702 }
Chris Mason321aecc2008-04-16 10:49:51 -0400703 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
704 num_stripes = btrfs_super_num_devices(&info->super_copy);
705 if (num_stripes < 4)
706 return -ENOSPC;
707 num_stripes &= ~(u32)1;
708 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -0400709 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -0400710 }
Chris Mason9b3f68b2008-04-18 10:29:51 -0400711
712 if (type & BTRFS_BLOCK_GROUP_DATA) {
713 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -0400714 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400715 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
716 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -0400717 min_stripe_size = 32 * 1024 * 1024;
718 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
719 calc_size = 8 * 1024 * 1024;
720 max_chunk_size = calc_size * 2;
721 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400722 }
723
724 /* we don't want a chunk larger than 10% of the FS */
725 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
726 max_chunk_size = min(percent_max, max_chunk_size);
727
Chris Masona40a90a2008-04-18 11:55:51 -0400728again:
Chris Mason9b3f68b2008-04-18 10:29:51 -0400729 if (calc_size * num_stripes > max_chunk_size) {
730 calc_size = max_chunk_size;
731 do_div(calc_size, num_stripes);
732 do_div(calc_size, stripe_len);
733 calc_size *= stripe_len;
734 }
735 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -0400736 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -0400737
Chris Mason9b3f68b2008-04-18 10:29:51 -0400738 do_div(calc_size, stripe_len);
739 calc_size *= stripe_len;
740
Chris Mason6324fbf2008-03-24 15:01:59 -0400741 INIT_LIST_HEAD(&private_devs);
742 cur = dev_list->next;
743 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -0400744
745 if (type & BTRFS_BLOCK_GROUP_DUP)
746 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400747 else
748 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -0400749
Chris Masonad5bd912008-04-21 08:28:10 -0400750 /* we add 1MB because we never use the first 1MB of the device */
751 min_free += 1024 * 1024;
752
Chris Mason6324fbf2008-03-24 15:01:59 -0400753 /* build a private list of devices we will allocate from */
754 while(index < num_stripes) {
755 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Mason611f0e02008-04-03 16:29:03 -0400756
Chris Mason6324fbf2008-03-24 15:01:59 -0400757 avail = device->total_bytes - device->bytes_used;
758 cur = cur->next;
Chris Mason611f0e02008-04-03 16:29:03 -0400759 if (avail >= min_free) {
Chris Mason6324fbf2008-03-24 15:01:59 -0400760 list_move_tail(&device->dev_list, &private_devs);
761 index++;
Chris Mason611f0e02008-04-03 16:29:03 -0400762 if (type & BTRFS_BLOCK_GROUP_DUP)
763 index++;
Chris Masona40a90a2008-04-18 11:55:51 -0400764 } else if (avail > max_avail)
765 max_avail = avail;
Chris Mason6324fbf2008-03-24 15:01:59 -0400766 if (cur == dev_list)
767 break;
768 }
769 if (index < num_stripes) {
770 list_splice(&private_devs, dev_list);
Chris Masona40a90a2008-04-18 11:55:51 -0400771 if (index >= min_stripes) {
772 num_stripes = index;
773 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
774 num_stripes /= sub_stripes;
775 num_stripes *= sub_stripes;
776 }
777 looped = 1;
778 goto again;
779 }
Chris Mason6324fbf2008-03-24 15:01:59 -0400780 if (!looped && max_avail > 0) {
781 looped = 1;
782 calc_size = max_avail;
783 goto again;
784 }
785 return -ENOSPC;
786 }
Chris Masone17cade2008-04-15 15:41:47 -0400787 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
788 key.type = BTRFS_CHUNK_ITEM_KEY;
789 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
790 &key.offset);
Chris Mason0b86a832008-03-24 15:01:56 -0400791 if (ret)
792 return ret;
793
Chris Mason0b86a832008-03-24 15:01:56 -0400794 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
795 if (!chunk)
796 return -ENOMEM;
797
Chris Mason593060d2008-03-25 16:50:33 -0400798 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
799 if (!map) {
800 kfree(chunk);
801 return -ENOMEM;
802 }
803
Chris Mason0b86a832008-03-24 15:01:56 -0400804 stripes = &chunk->stripe;
Chris Mason9b3f68b2008-04-18 10:29:51 -0400805 *num_bytes = chunk_bytes_by_type(type, calc_size,
806 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -0400807
Chris Mason8790d502008-04-03 16:29:03 -0400808
Chris Mason6324fbf2008-03-24 15:01:59 -0400809 index = 0;
Chris Masone17cade2008-04-15 15:41:47 -0400810printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -0400811 while(index < num_stripes) {
Chris Masone17cade2008-04-15 15:41:47 -0400812 struct btrfs_stripe *stripe;
Chris Mason6324fbf2008-03-24 15:01:59 -0400813 BUG_ON(list_empty(&private_devs));
814 cur = private_devs.next;
815 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Mason611f0e02008-04-03 16:29:03 -0400816
817 /* loop over this device again if we're doing a dup group */
818 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
819 (index == num_stripes - 1))
820 list_move_tail(&device->dev_list, dev_list);
Chris Mason0b86a832008-03-24 15:01:56 -0400821
822 ret = btrfs_alloc_dev_extent(trans, device,
Chris Masone17cade2008-04-15 15:41:47 -0400823 info->chunk_root->root_key.objectid,
824 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
825 calc_size, &dev_offset);
Chris Mason0b86a832008-03-24 15:01:56 -0400826 BUG_ON(ret);
Chris Masone17cade2008-04-15 15:41:47 -0400827printk("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 -0400828 device->bytes_used += calc_size;
829 ret = btrfs_update_device(trans, device);
830 BUG_ON(ret);
831
Chris Mason593060d2008-03-25 16:50:33 -0400832 map->stripes[index].dev = device;
833 map->stripes[index].physical = dev_offset;
Chris Masone17cade2008-04-15 15:41:47 -0400834 stripe = stripes + index;
835 btrfs_set_stack_stripe_devid(stripe, device->devid);
836 btrfs_set_stack_stripe_offset(stripe, dev_offset);
837 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400838 physical = dev_offset;
839 index++;
840 }
Chris Mason6324fbf2008-03-24 15:01:59 -0400841 BUG_ON(!list_empty(&private_devs));
Chris Mason0b86a832008-03-24 15:01:56 -0400842
Chris Masone17cade2008-04-15 15:41:47 -0400843 /* key was set above */
844 btrfs_set_stack_chunk_length(chunk, *num_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -0400845 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
Chris Mason593060d2008-03-25 16:50:33 -0400846 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -0400847 btrfs_set_stack_chunk_type(chunk, type);
848 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -0400849 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
850 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -0400851 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
Chris Mason321aecc2008-04-16 10:49:51 -0400852 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
Chris Mason593060d2008-03-25 16:50:33 -0400853 map->sector_size = extent_root->sectorsize;
854 map->stripe_len = stripe_len;
855 map->io_align = stripe_len;
856 map->io_width = stripe_len;
857 map->type = type;
858 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -0400859 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -0400860
861 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
862 btrfs_chunk_item_size(num_stripes));
863 BUG_ON(ret);
Chris Masone17cade2008-04-15 15:41:47 -0400864 *start = key.offset;;
Chris Mason0b86a832008-03-24 15:01:56 -0400865
866 em = alloc_extent_map(GFP_NOFS);
867 if (!em)
868 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400869 em->bdev = (struct block_device *)map;
Chris Masone17cade2008-04-15 15:41:47 -0400870 em->start = key.offset;
871 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400872 em->block_start = 0;
873
Chris Mason0b86a832008-03-24 15:01:56 -0400874 kfree(chunk);
875
876 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
877 spin_lock(&em_tree->lock);
878 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -0400879 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -0400880 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400881 free_extent_map(em);
882 return ret;
883}
884
885void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
886{
887 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
888}
889
890void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
891{
892 struct extent_map *em;
893
894 while(1) {
895 spin_lock(&tree->map_tree.lock);
896 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
897 if (em)
898 remove_extent_mapping(&tree->map_tree, em);
899 spin_unlock(&tree->map_tree.lock);
900 if (!em)
901 break;
902 kfree(em->bdev);
903 /* once for us */
904 free_extent_map(em);
905 /* once for the tree */
906 free_extent_map(em);
907 }
908}
909
Chris Masonf1885912008-04-09 16:28:12 -0400910int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
911{
912 struct extent_map *em;
913 struct map_lookup *map;
914 struct extent_map_tree *em_tree = &map_tree->map_tree;
915 int ret;
916
917 spin_lock(&em_tree->lock);
918 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -0400919 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -0400920 BUG_ON(!em);
921
922 BUG_ON(em->start > logical || em->start + em->len < logical);
923 map = (struct map_lookup *)em->bdev;
924 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
925 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -0400926 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
927 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -0400928 else
929 ret = 1;
930 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -0400931 return ret;
932}
933
Chris Masonf2d8d742008-04-21 10:03:05 -0400934static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
935 u64 logical, u64 *length,
936 struct btrfs_multi_bio **multi_ret,
937 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -0400938{
939 struct extent_map *em;
940 struct map_lookup *map;
941 struct extent_map_tree *em_tree = &map_tree->map_tree;
942 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -0400943 u64 stripe_offset;
944 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -0400945 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -0400946 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -0400947 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -0400948 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -0400949 int num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -0400950 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400951
Chris Masoncea9e442008-04-09 16:28:12 -0400952 if (multi_ret && !(rw & (1 << BIO_RW))) {
953 stripes_allocated = 1;
954 }
955again:
956 if (multi_ret) {
957 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
958 GFP_NOFS);
959 if (!multi)
960 return -ENOMEM;
961 }
Chris Mason0b86a832008-03-24 15:01:56 -0400962
963 spin_lock(&em_tree->lock);
964 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -0400965 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -0400966
967 if (!em && unplug_page)
968 return 0;
969
Chris Mason3b951512008-04-17 11:29:12 -0400970 if (!em) {
971 printk("unable to find logical %Lu\n", logical);
Chris Masonf2d8d742008-04-21 10:03:05 -0400972 BUG();
Chris Mason3b951512008-04-17 11:29:12 -0400973 }
Chris Mason0b86a832008-03-24 15:01:56 -0400974
975 BUG_ON(em->start > logical || em->start + em->len < logical);
976 map = (struct map_lookup *)em->bdev;
977 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -0400978
Chris Masonf1885912008-04-09 16:28:12 -0400979 if (mirror_num > map->num_stripes)
980 mirror_num = 0;
981
Chris Masoncea9e442008-04-09 16:28:12 -0400982 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -0400983 if (rw & (1 << BIO_RW)) {
984 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
985 BTRFS_BLOCK_GROUP_DUP)) {
986 stripes_required = map->num_stripes;
987 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
988 stripes_required = map->sub_stripes;
989 }
990 }
991 if (multi_ret && rw == WRITE &&
992 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -0400993 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -0400994 free_extent_map(em);
995 kfree(multi);
996 goto again;
997 }
Chris Mason593060d2008-03-25 16:50:33 -0400998 stripe_nr = offset;
999 /*
1000 * stripe_nr counts the total number of stripes we have to stride
1001 * to get to this block
1002 */
1003 do_div(stripe_nr, map->stripe_len);
1004
1005 stripe_offset = stripe_nr * map->stripe_len;
1006 BUG_ON(offset < stripe_offset);
1007
1008 /* stripe_offset is the offset of this block in its stripe*/
1009 stripe_offset = offset - stripe_offset;
1010
Chris Masoncea9e442008-04-09 16:28:12 -04001011 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001012 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04001013 BTRFS_BLOCK_GROUP_DUP)) {
1014 /* we limit the length of each bio to what fits in a stripe */
1015 *length = min_t(u64, em->len - offset,
1016 map->stripe_len - stripe_offset);
1017 } else {
1018 *length = em->len - offset;
1019 }
Chris Masonf2d8d742008-04-21 10:03:05 -04001020
1021 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04001022 goto out;
1023
Chris Masonf2d8d742008-04-21 10:03:05 -04001024 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04001025 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001026 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04001027 if (unplug_page || (rw & (1 << BIO_RW)))
1028 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001029 else if (mirror_num) {
1030 stripe_index = mirror_num - 1;
1031 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001032 int i;
1033 u64 least = (u64)-1;
1034 struct btrfs_device *cur;
Chris Mason593060d2008-03-25 16:50:33 -04001035
Chris Mason8790d502008-04-03 16:29:03 -04001036 for (i = 0; i < map->num_stripes; i++) {
1037 cur = map->stripes[i].dev;
1038 spin_lock(&cur->io_lock);
1039 if (cur->total_ios < least) {
1040 least = cur->total_ios;
1041 stripe_index = i;
1042 }
1043 spin_unlock(&cur->io_lock);
1044 }
Chris Mason8790d502008-04-03 16:29:03 -04001045 }
Chris Mason611f0e02008-04-03 16:29:03 -04001046 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04001047 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04001048 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001049 else if (mirror_num)
1050 stripe_index = mirror_num - 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001051 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1052 int factor = map->num_stripes / map->sub_stripes;
1053 int orig_stripe_nr = stripe_nr;
1054
1055 stripe_index = do_div(stripe_nr, factor);
1056 stripe_index *= map->sub_stripes;
1057
Chris Masonf2d8d742008-04-21 10:03:05 -04001058 if (unplug_page || (rw & (1 << BIO_RW)))
1059 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001060 else if (mirror_num)
1061 stripe_index += mirror_num - 1;
1062 else
1063 stripe_index += orig_stripe_nr % map->sub_stripes;
Chris Mason8790d502008-04-03 16:29:03 -04001064 } else {
1065 /*
1066 * after this do_div call, stripe_nr is the number of stripes
1067 * on this device we have to walk to find the data, and
1068 * stripe_index is the number of our device in the stripe array
1069 */
1070 stripe_index = do_div(stripe_nr, map->num_stripes);
1071 }
Chris Mason593060d2008-03-25 16:50:33 -04001072 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001073
Chris Masonf2d8d742008-04-21 10:03:05 -04001074 for (i = 0; i < num_stripes; i++) {
1075 if (unplug_page) {
1076 struct btrfs_device *device;
1077 struct backing_dev_info *bdi;
1078
1079 device = map->stripes[stripe_index].dev;
1080 bdi = blk_get_backing_dev_info(device->bdev);
1081 if (bdi->unplug_io_fn) {
1082 bdi->unplug_io_fn(bdi, unplug_page);
1083 }
1084 } else {
1085 multi->stripes[i].physical =
1086 map->stripes[stripe_index].physical +
1087 stripe_offset + stripe_nr * map->stripe_len;
1088 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1089 }
Chris Masoncea9e442008-04-09 16:28:12 -04001090 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04001091 }
Chris Masonf2d8d742008-04-21 10:03:05 -04001092 if (multi_ret) {
1093 *multi_ret = multi;
1094 multi->num_stripes = num_stripes;
1095 }
Chris Masoncea9e442008-04-09 16:28:12 -04001096out:
Chris Mason0b86a832008-03-24 15:01:56 -04001097 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04001098 return 0;
1099}
1100
Chris Masonf2d8d742008-04-21 10:03:05 -04001101int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1102 u64 logical, u64 *length,
1103 struct btrfs_multi_bio **multi_ret, int mirror_num)
1104{
1105 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
1106 mirror_num, NULL);
1107}
1108
1109int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
1110 u64 logical, struct page *page)
1111{
1112 u64 length = PAGE_CACHE_SIZE;
1113 return __btrfs_map_block(map_tree, READ, logical, &length,
1114 NULL, 0, page);
1115}
1116
1117
Chris Mason8790d502008-04-03 16:29:03 -04001118#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1119static void end_bio_multi_stripe(struct bio *bio, int err)
1120#else
1121static int end_bio_multi_stripe(struct bio *bio,
1122 unsigned int bytes_done, int err)
1123#endif
1124{
Chris Masoncea9e442008-04-09 16:28:12 -04001125 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason8790d502008-04-03 16:29:03 -04001126
1127#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1128 if (bio->bi_size)
1129 return 1;
1130#endif
1131 if (err)
1132 multi->error = err;
1133
Chris Masoncea9e442008-04-09 16:28:12 -04001134 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason8790d502008-04-03 16:29:03 -04001135 bio->bi_private = multi->private;
1136 bio->bi_end_io = multi->end_io;
1137
1138 if (!err && multi->error)
1139 err = multi->error;
1140 kfree(multi);
1141
Miguel73f61b22008-04-11 15:50:59 -04001142#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1143 bio_endio(bio, bio->bi_size, err);
1144#else
Chris Mason8790d502008-04-03 16:29:03 -04001145 bio_endio(bio, err);
Miguel73f61b22008-04-11 15:50:59 -04001146#endif
Chris Mason8790d502008-04-03 16:29:03 -04001147 } else {
1148 bio_put(bio);
1149 }
1150#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1151 return 0;
1152#endif
1153}
1154
Chris Masonf1885912008-04-09 16:28:12 -04001155int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1156 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04001157{
1158 struct btrfs_mapping_tree *map_tree;
1159 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04001160 struct bio *first_bio = bio;
Chris Mason0b86a832008-03-24 15:01:56 -04001161 u64 logical = bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04001162 u64 length = 0;
1163 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04001164 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001165 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04001166 int dev_nr = 0;
1167 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001168
Chris Masonf2d8d742008-04-21 10:03:05 -04001169 length = bio->bi_size;
Chris Mason8790d502008-04-03 16:29:03 -04001170
Chris Mason0b86a832008-03-24 15:01:56 -04001171 map_tree = &root->fs_info->mapping_tree;
1172 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001173
Chris Masonf1885912008-04-09 16:28:12 -04001174 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
1175 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04001176 BUG_ON(ret);
1177
1178 total_devs = multi->num_stripes;
1179 if (map_length < length) {
1180 printk("mapping failed logical %Lu bio len %Lu "
1181 "len %Lu\n", logical, length, map_length);
1182 BUG();
1183 }
1184 multi->end_io = first_bio->bi_end_io;
1185 multi->private = first_bio->bi_private;
1186 atomic_set(&multi->stripes_pending, multi->num_stripes);
1187
Chris Mason8790d502008-04-03 16:29:03 -04001188 while(dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04001189 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04001190 if (dev_nr < total_devs - 1) {
1191 bio = bio_clone(first_bio, GFP_NOFS);
1192 BUG_ON(!bio);
1193 } else {
1194 bio = first_bio;
1195 }
1196 bio->bi_private = multi;
1197 bio->bi_end_io = end_bio_multi_stripe;
1198 }
Chris Masoncea9e442008-04-09 16:28:12 -04001199 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1200 dev = multi->stripes[dev_nr].dev;
Chris Mason8790d502008-04-03 16:29:03 -04001201 bio->bi_bdev = dev->bdev;
1202 spin_lock(&dev->io_lock);
1203 dev->total_ios++;
1204 spin_unlock(&dev->io_lock);
1205 submit_bio(rw, bio);
1206 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04001207 }
Chris Masoncea9e442008-04-09 16:28:12 -04001208 if (total_devs == 1)
1209 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04001210 return 0;
1211}
1212
Chris Masona4437552008-04-18 10:29:38 -04001213struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1214 u8 *uuid)
Chris Mason0b86a832008-03-24 15:01:56 -04001215{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001216 struct list_head *head = &root->fs_info->fs_devices->devices;
Chris Mason0b86a832008-03-24 15:01:56 -04001217
Chris Masona4437552008-04-18 10:29:38 -04001218 return __find_device(head, devid, uuid);
Chris Mason0b86a832008-03-24 15:01:56 -04001219}
1220
1221static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1222 struct extent_buffer *leaf,
1223 struct btrfs_chunk *chunk)
1224{
1225 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1226 struct map_lookup *map;
1227 struct extent_map *em;
1228 u64 logical;
1229 u64 length;
1230 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04001231 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04001232 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04001233 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04001234 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04001235
Chris Masone17cade2008-04-15 15:41:47 -04001236 logical = key->offset;
1237 length = btrfs_chunk_length(leaf, chunk);
Chris Mason0b86a832008-03-24 15:01:56 -04001238 spin_lock(&map_tree->map_tree.lock);
1239 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04001240 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04001241
1242 /* already mapped? */
1243 if (em && em->start <= logical && em->start + em->len > logical) {
1244 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04001245 return 0;
1246 } else if (em) {
1247 free_extent_map(em);
1248 }
Chris Mason0b86a832008-03-24 15:01:56 -04001249
1250 map = kzalloc(sizeof(*map), GFP_NOFS);
1251 if (!map)
1252 return -ENOMEM;
1253
1254 em = alloc_extent_map(GFP_NOFS);
1255 if (!em)
1256 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04001257 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1258 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04001259 if (!map) {
1260 free_extent_map(em);
1261 return -ENOMEM;
1262 }
1263
1264 em->bdev = (struct block_device *)map;
1265 em->start = logical;
1266 em->len = length;
1267 em->block_start = 0;
1268
Chris Mason593060d2008-03-25 16:50:33 -04001269 map->num_stripes = num_stripes;
1270 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1271 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1272 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1273 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1274 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04001275 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04001276 for (i = 0; i < num_stripes; i++) {
1277 map->stripes[i].physical =
1278 btrfs_stripe_offset_nr(leaf, chunk, i);
1279 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04001280 read_extent_buffer(leaf, uuid, (unsigned long)
1281 btrfs_stripe_dev_uuid_nr(chunk, i),
1282 BTRFS_UUID_SIZE);
1283 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
Chris Mason593060d2008-03-25 16:50:33 -04001284 if (!map->stripes[i].dev) {
1285 kfree(map);
1286 free_extent_map(em);
1287 return -EIO;
1288 }
Chris Mason0b86a832008-03-24 15:01:56 -04001289 }
1290
1291 spin_lock(&map_tree->map_tree.lock);
1292 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04001293 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04001294 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001295 free_extent_map(em);
1296
1297 return 0;
1298}
1299
1300static int fill_device_from_item(struct extent_buffer *leaf,
1301 struct btrfs_dev_item *dev_item,
1302 struct btrfs_device *device)
1303{
1304 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001305
1306 device->devid = btrfs_device_id(leaf, dev_item);
1307 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1308 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1309 device->type = btrfs_device_type(leaf, dev_item);
1310 device->io_align = btrfs_device_io_align(leaf, dev_item);
1311 device->io_width = btrfs_device_io_width(leaf, dev_item);
1312 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04001313
1314 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001315 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001316
Chris Mason0b86a832008-03-24 15:01:56 -04001317 return 0;
1318}
1319
Chris Mason0d81ba52008-03-24 15:02:07 -04001320static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04001321 struct extent_buffer *leaf,
1322 struct btrfs_dev_item *dev_item)
1323{
1324 struct btrfs_device *device;
1325 u64 devid;
1326 int ret;
Chris Masona4437552008-04-18 10:29:38 -04001327 u8 dev_uuid[BTRFS_UUID_SIZE];
1328
Chris Mason0b86a832008-03-24 15:01:56 -04001329 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04001330 read_extent_buffer(leaf, dev_uuid,
1331 (unsigned long)btrfs_device_uuid(dev_item),
1332 BTRFS_UUID_SIZE);
1333 device = btrfs_find_device(root, devid, dev_uuid);
Chris Mason6324fbf2008-03-24 15:01:59 -04001334 if (!device) {
Chris Mason8a4b83c2008-03-24 15:02:07 -04001335 printk("warning devid %Lu not found already\n", devid);
Chris Masonf2984462008-04-10 16:19:33 -04001336 device = kzalloc(sizeof(*device), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04001337 if (!device)
1338 return -ENOMEM;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001339 list_add(&device->dev_list,
1340 &root->fs_info->fs_devices->devices);
Chris Masonb248a412008-04-14 09:48:18 -04001341 device->barriers = 1;
Chris Mason8790d502008-04-03 16:29:03 -04001342 spin_lock_init(&device->io_lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04001343 }
Chris Mason0b86a832008-03-24 15:01:56 -04001344
1345 fill_device_from_item(leaf, dev_item, device);
1346 device->dev_root = root->fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001347 ret = 0;
1348#if 0
1349 ret = btrfs_open_device(device);
1350 if (ret) {
1351 kfree(device);
1352 }
1353#endif
1354 return ret;
1355}
1356
Chris Mason0d81ba52008-03-24 15:02:07 -04001357int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1358{
1359 struct btrfs_dev_item *dev_item;
1360
1361 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1362 dev_item);
1363 return read_one_dev(root, buf, dev_item);
1364}
1365
Chris Mason0b86a832008-03-24 15:01:56 -04001366int btrfs_read_sys_array(struct btrfs_root *root)
1367{
1368 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1369 struct extent_buffer *sb = root->fs_info->sb_buffer;
1370 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04001371 struct btrfs_chunk *chunk;
1372 struct btrfs_key key;
1373 u32 num_stripes;
1374 u32 array_size;
1375 u32 len = 0;
1376 u8 *ptr;
1377 unsigned long sb_ptr;
1378 u32 cur;
1379 int ret;
Chris Mason0b86a832008-03-24 15:01:56 -04001380
1381 array_size = btrfs_super_sys_array_size(super_copy);
1382
1383 /*
1384 * we do this loop twice, once for the device items and
1385 * once for all of the chunks. This way there are device
1386 * structs filled in for every chunk
1387 */
Chris Mason0b86a832008-03-24 15:01:56 -04001388 ptr = super_copy->sys_chunk_array;
1389 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1390 cur = 0;
1391
1392 while (cur < array_size) {
1393 disk_key = (struct btrfs_disk_key *)ptr;
1394 btrfs_disk_key_to_cpu(&key, disk_key);
1395
1396 len = sizeof(*disk_key);
1397 ptr += len;
1398 sb_ptr += len;
1399 cur += len;
1400
Chris Mason0d81ba52008-03-24 15:02:07 -04001401 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04001402 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04001403 ret = read_one_chunk(root, &key, sb, chunk);
1404 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001405 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1406 len = btrfs_chunk_item_size(num_stripes);
1407 } else {
1408 BUG();
1409 }
1410 ptr += len;
1411 sb_ptr += len;
1412 cur += len;
1413 }
Chris Mason0b86a832008-03-24 15:01:56 -04001414 return 0;
1415}
1416
1417int btrfs_read_chunk_tree(struct btrfs_root *root)
1418{
1419 struct btrfs_path *path;
1420 struct extent_buffer *leaf;
1421 struct btrfs_key key;
1422 struct btrfs_key found_key;
1423 int ret;
1424 int slot;
1425
1426 root = root->fs_info->chunk_root;
1427
1428 path = btrfs_alloc_path();
1429 if (!path)
1430 return -ENOMEM;
1431
1432 /* first we search for all of the device items, and then we
1433 * read in all of the chunk items. This way we can create chunk
1434 * mappings that reference all of the devices that are afound
1435 */
1436 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1437 key.offset = 0;
1438 key.type = 0;
1439again:
1440 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1441 while(1) {
1442 leaf = path->nodes[0];
1443 slot = path->slots[0];
1444 if (slot >= btrfs_header_nritems(leaf)) {
1445 ret = btrfs_next_leaf(root, path);
1446 if (ret == 0)
1447 continue;
1448 if (ret < 0)
1449 goto error;
1450 break;
1451 }
1452 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1453 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1454 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1455 break;
1456 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1457 struct btrfs_dev_item *dev_item;
1458 dev_item = btrfs_item_ptr(leaf, slot,
1459 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04001460 ret = read_one_dev(root, leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04001461 BUG_ON(ret);
1462 }
1463 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1464 struct btrfs_chunk *chunk;
1465 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1466 ret = read_one_chunk(root, &found_key, leaf, chunk);
1467 }
1468 path->slots[0]++;
1469 }
1470 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1471 key.objectid = 0;
1472 btrfs_release_path(root, path);
1473 goto again;
1474 }
1475
1476 btrfs_free_path(path);
1477 ret = 0;
1478error:
1479 return ret;
1480}
1481