blob: b93c15aa17db054814e3012bf12e15d2b7dc5f6e [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040020#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040021#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040022#include <linux/random.h>
Chris Mason593060d2008-03-25 16:50:33 -040023#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040024#include "ctree.h"
25#include "extent_map.h"
26#include "disk-io.h"
27#include "transaction.h"
28#include "print-tree.h"
29#include "volumes.h"
30
Chris Mason593060d2008-03-25 16:50:33 -040031struct map_lookup {
32 u64 type;
33 int io_align;
34 int io_width;
35 int stripe_len;
36 int sector_size;
37 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040038 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040039 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040040};
41
42#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040043 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040044
Chris Mason8a4b83c2008-03-24 15:02:07 -040045static DEFINE_MUTEX(uuid_mutex);
46static LIST_HEAD(fs_uuids);
47
48int btrfs_cleanup_fs_uuids(void)
49{
50 struct btrfs_fs_devices *fs_devices;
51 struct list_head *uuid_cur;
52 struct list_head *devices_cur;
53 struct btrfs_device *dev;
54
55 list_for_each(uuid_cur, &fs_uuids) {
56 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
57 list);
58 while(!list_empty(&fs_devices->devices)) {
59 devices_cur = fs_devices->devices.next;
60 dev = list_entry(devices_cur, struct btrfs_device,
61 dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -040062 if (dev->bdev) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040063 close_bdev_excl(dev->bdev);
64 }
65 list_del(&dev->dev_list);
66 kfree(dev);
67 }
68 }
69 return 0;
70}
71
Chris Masona4437552008-04-18 10:29:38 -040072static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
73 u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040074{
75 struct btrfs_device *dev;
76 struct list_head *cur;
77
78 list_for_each(cur, head) {
79 dev = list_entry(cur, struct btrfs_device, dev_list);
Chris Masona4437552008-04-18 10:29:38 -040080 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040081 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040082 return dev;
Chris Masona4437552008-04-18 10:29:38 -040083 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040084 }
85 return NULL;
86}
87
88static struct btrfs_fs_devices *find_fsid(u8 *fsid)
89{
90 struct list_head *cur;
91 struct btrfs_fs_devices *fs_devices;
92
93 list_for_each(cur, &fs_uuids) {
94 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
95 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
96 return fs_devices;
97 }
98 return NULL;
99}
100
101static int device_list_add(const char *path,
102 struct btrfs_super_block *disk_super,
103 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
104{
105 struct btrfs_device *device;
106 struct btrfs_fs_devices *fs_devices;
107 u64 found_transid = btrfs_super_generation(disk_super);
108
109 fs_devices = find_fsid(disk_super->fsid);
110 if (!fs_devices) {
111 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
112 if (!fs_devices)
113 return -ENOMEM;
114 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400115 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400116 list_add(&fs_devices->list, &fs_uuids);
117 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
118 fs_devices->latest_devid = devid;
119 fs_devices->latest_trans = found_transid;
120 fs_devices->lowest_devid = (u64)-1;
121 fs_devices->num_devices = 0;
122 device = NULL;
123 } else {
Chris Masona4437552008-04-18 10:29:38 -0400124 device = __find_device(&fs_devices->devices, devid,
125 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400126 }
127 if (!device) {
128 device = kzalloc(sizeof(*device), GFP_NOFS);
129 if (!device) {
130 /* we can safely leave the fs_devices entry around */
131 return -ENOMEM;
132 }
133 device->devid = devid;
Chris Masona4437552008-04-18 10:29:38 -0400134 memcpy(device->uuid, disk_super->dev_item.uuid,
135 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400136 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400137 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400138 device->name = kstrdup(path, GFP_NOFS);
139 if (!device->name) {
140 kfree(device);
141 return -ENOMEM;
142 }
143 list_add(&device->dev_list, &fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400144 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145 fs_devices->num_devices++;
146 }
147
148 if (found_transid > fs_devices->latest_trans) {
149 fs_devices->latest_devid = devid;
150 fs_devices->latest_trans = found_transid;
151 }
152 if (fs_devices->lowest_devid > devid) {
153 fs_devices->lowest_devid = devid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400154 }
155 *fs_devices_ret = fs_devices;
156 return 0;
157}
158
159int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
160{
161 struct list_head *head = &fs_devices->devices;
162 struct list_head *cur;
163 struct btrfs_device *device;
164
165 mutex_lock(&uuid_mutex);
166 list_for_each(cur, head) {
167 device = list_entry(cur, struct btrfs_device, dev_list);
168 if (device->bdev) {
169 close_bdev_excl(device->bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400170 }
171 device->bdev = NULL;
172 }
173 mutex_unlock(&uuid_mutex);
174 return 0;
175}
176
177int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
178 int flags, void *holder)
179{
180 struct block_device *bdev;
181 struct list_head *head = &fs_devices->devices;
182 struct list_head *cur;
183 struct btrfs_device *device;
184 int ret;
185
186 mutex_lock(&uuid_mutex);
187 list_for_each(cur, head) {
188 device = list_entry(cur, struct btrfs_device, dev_list);
189 bdev = open_bdev_excl(device->name, flags, holder);
Chris Masone17cade2008-04-15 15:41:47 -0400190
Chris Mason8a4b83c2008-03-24 15:02:07 -0400191 if (IS_ERR(bdev)) {
192 printk("open %s failed\n", device->name);
193 ret = PTR_ERR(bdev);
194 goto fail;
195 }
196 if (device->devid == fs_devices->latest_devid)
197 fs_devices->latest_bdev = bdev;
198 if (device->devid == fs_devices->lowest_devid) {
199 fs_devices->lowest_bdev = bdev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400200 }
201 device->bdev = bdev;
202 }
203 mutex_unlock(&uuid_mutex);
204 return 0;
205fail:
206 mutex_unlock(&uuid_mutex);
207 btrfs_close_devices(fs_devices);
208 return ret;
209}
210
211int btrfs_scan_one_device(const char *path, int flags, void *holder,
212 struct btrfs_fs_devices **fs_devices_ret)
213{
214 struct btrfs_super_block *disk_super;
215 struct block_device *bdev;
216 struct buffer_head *bh;
217 int ret;
218 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400219 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400220
221 mutex_lock(&uuid_mutex);
222
Chris Mason8a4b83c2008-03-24 15:02:07 -0400223 bdev = open_bdev_excl(path, flags, holder);
224
225 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400226 ret = PTR_ERR(bdev);
227 goto error;
228 }
229
230 ret = set_blocksize(bdev, 4096);
231 if (ret)
232 goto error_close;
233 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
234 if (!bh) {
235 ret = -EIO;
236 goto error_close;
237 }
238 disk_super = (struct btrfs_super_block *)bh->b_data;
239 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
240 sizeof(disk_super->magic))) {
Yane58ca022008-04-01 11:21:34 -0400241 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400242 goto error_brelse;
243 }
244 devid = le64_to_cpu(disk_super->dev_item.devid);
Chris Masonf2984462008-04-10 16:19:33 -0400245 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400246 if (disk_super->label[0])
247 printk("device label %s ", disk_super->label);
248 else {
249 /* FIXME, make a readl uuid parser */
250 printk("device fsid %llx-%llx ",
251 *(unsigned long long *)disk_super->fsid,
252 *(unsigned long long *)(disk_super->fsid + 8));
253 }
254 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400255 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
256
257error_brelse:
258 brelse(bh);
259error_close:
260 close_bdev_excl(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400261error:
262 mutex_unlock(&uuid_mutex);
263 return ret;
264}
Chris Mason0b86a832008-03-24 15:01:56 -0400265
266/*
267 * this uses a pretty simple search, the expectation is that it is
268 * called very infrequently and that a given device has a small number
269 * of extents
270 */
271static int find_free_dev_extent(struct btrfs_trans_handle *trans,
272 struct btrfs_device *device,
273 struct btrfs_path *path,
274 u64 num_bytes, u64 *start)
275{
276 struct btrfs_key key;
277 struct btrfs_root *root = device->dev_root;
278 struct btrfs_dev_extent *dev_extent = NULL;
279 u64 hole_size = 0;
280 u64 last_byte = 0;
281 u64 search_start = 0;
282 u64 search_end = device->total_bytes;
283 int ret;
284 int slot = 0;
285 int start_found;
286 struct extent_buffer *l;
287
288 start_found = 0;
289 path->reada = 2;
290
291 /* FIXME use last free of some kind */
292
Chris Mason8a4b83c2008-03-24 15:02:07 -0400293 /* we don't want to overwrite the superblock on the drive,
294 * so we make sure to start at an offset of at least 1MB
295 */
296 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400297
298 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
299 search_start = max(root->fs_info->alloc_start, search_start);
300
Chris Mason0b86a832008-03-24 15:01:56 -0400301 key.objectid = device->devid;
302 key.offset = search_start;
303 key.type = BTRFS_DEV_EXTENT_KEY;
304 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
305 if (ret < 0)
306 goto error;
307 ret = btrfs_previous_item(root, path, 0, key.type);
308 if (ret < 0)
309 goto error;
310 l = path->nodes[0];
311 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
312 while (1) {
313 l = path->nodes[0];
314 slot = path->slots[0];
315 if (slot >= btrfs_header_nritems(l)) {
316 ret = btrfs_next_leaf(root, path);
317 if (ret == 0)
318 continue;
319 if (ret < 0)
320 goto error;
321no_more_items:
322 if (!start_found) {
323 if (search_start >= search_end) {
324 ret = -ENOSPC;
325 goto error;
326 }
327 *start = search_start;
328 start_found = 1;
329 goto check_pending;
330 }
331 *start = last_byte > search_start ?
332 last_byte : search_start;
333 if (search_end <= *start) {
334 ret = -ENOSPC;
335 goto error;
336 }
337 goto check_pending;
338 }
339 btrfs_item_key_to_cpu(l, &key, slot);
340
341 if (key.objectid < device->devid)
342 goto next;
343
344 if (key.objectid > device->devid)
345 goto no_more_items;
346
347 if (key.offset >= search_start && key.offset > last_byte &&
348 start_found) {
349 if (last_byte < search_start)
350 last_byte = search_start;
351 hole_size = key.offset - last_byte;
352 if (key.offset > last_byte &&
353 hole_size >= num_bytes) {
354 *start = last_byte;
355 goto check_pending;
356 }
357 }
358 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
359 goto next;
360 }
361
362 start_found = 1;
363 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
364 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
365next:
366 path->slots[0]++;
367 cond_resched();
368 }
369check_pending:
370 /* we have to make sure we didn't find an extent that has already
371 * been allocated by the map tree or the original allocation
372 */
373 btrfs_release_path(root, path);
374 BUG_ON(*start < search_start);
375
Chris Mason6324fbf2008-03-24 15:01:59 -0400376 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400377 ret = -ENOSPC;
378 goto error;
379 }
380 /* check for pending inserts here */
381 return 0;
382
383error:
384 btrfs_release_path(root, path);
385 return ret;
386}
387
Chris Mason8f18cf12008-04-25 16:53:30 -0400388int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
389 struct btrfs_device *device,
390 u64 start)
391{
392 int ret;
393 struct btrfs_path *path;
394 struct btrfs_root *root = device->dev_root;
395 struct btrfs_key key;
396
397 path = btrfs_alloc_path();
398 if (!path)
399 return -ENOMEM;
400
401 key.objectid = device->devid;
402 key.offset = start;
403 key.type = BTRFS_DEV_EXTENT_KEY;
404
405 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
406 BUG_ON(ret);
407
408 ret = btrfs_del_item(trans, root, path);
409 BUG_ON(ret);
410
411 btrfs_free_path(path);
412 return ret;
413}
414
Chris Mason0b86a832008-03-24 15:01:56 -0400415int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
416 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400417 u64 chunk_tree, u64 chunk_objectid,
418 u64 chunk_offset,
419 u64 num_bytes, u64 *start)
Chris Mason0b86a832008-03-24 15:01:56 -0400420{
421 int ret;
422 struct btrfs_path *path;
423 struct btrfs_root *root = device->dev_root;
424 struct btrfs_dev_extent *extent;
425 struct extent_buffer *leaf;
426 struct btrfs_key key;
427
428 path = btrfs_alloc_path();
429 if (!path)
430 return -ENOMEM;
431
432 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
Chris Mason6324fbf2008-03-24 15:01:59 -0400433 if (ret) {
Chris Mason0b86a832008-03-24 15:01:56 -0400434 goto err;
Chris Mason6324fbf2008-03-24 15:01:59 -0400435 }
Chris Mason0b86a832008-03-24 15:01:56 -0400436
437 key.objectid = device->devid;
438 key.offset = *start;
439 key.type = BTRFS_DEV_EXTENT_KEY;
440 ret = btrfs_insert_empty_item(trans, root, path, &key,
441 sizeof(*extent));
442 BUG_ON(ret);
443
444 leaf = path->nodes[0];
445 extent = btrfs_item_ptr(leaf, path->slots[0],
446 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400447 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
448 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
449 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
450
451 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
452 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
453 BTRFS_UUID_SIZE);
454
Chris Mason0b86a832008-03-24 15:01:56 -0400455 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
456 btrfs_mark_buffer_dirty(leaf);
457err:
458 btrfs_free_path(path);
459 return ret;
460}
461
Chris Masone17cade2008-04-15 15:41:47 -0400462static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400463{
464 struct btrfs_path *path;
465 int ret;
466 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400467 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400468 struct btrfs_key found_key;
469
470 path = btrfs_alloc_path();
471 BUG_ON(!path);
472
Chris Masone17cade2008-04-15 15:41:47 -0400473 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400474 key.offset = (u64)-1;
475 key.type = BTRFS_CHUNK_ITEM_KEY;
476
477 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
478 if (ret < 0)
479 goto error;
480
481 BUG_ON(ret == 0);
482
483 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
484 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400485 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400486 } else {
487 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
488 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400489 if (found_key.objectid != objectid)
490 *offset = 0;
491 else {
492 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
493 struct btrfs_chunk);
494 *offset = found_key.offset +
495 btrfs_chunk_length(path->nodes[0], chunk);
496 }
Chris Mason0b86a832008-03-24 15:01:56 -0400497 }
498 ret = 0;
499error:
500 btrfs_free_path(path);
501 return ret;
502}
503
Chris Mason0b86a832008-03-24 15:01:56 -0400504static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
505 u64 *objectid)
506{
507 int ret;
508 struct btrfs_key key;
509 struct btrfs_key found_key;
510
511 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
512 key.type = BTRFS_DEV_ITEM_KEY;
513 key.offset = (u64)-1;
514
515 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
516 if (ret < 0)
517 goto error;
518
519 BUG_ON(ret == 0);
520
521 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
522 BTRFS_DEV_ITEM_KEY);
523 if (ret) {
524 *objectid = 1;
525 } else {
526 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
527 path->slots[0]);
528 *objectid = found_key.offset + 1;
529 }
530 ret = 0;
531error:
532 btrfs_release_path(root, path);
533 return ret;
534}
535
536/*
537 * the device information is stored in the chunk root
538 * the btrfs_device struct should be fully filled in
539 */
540int btrfs_add_device(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root,
542 struct btrfs_device *device)
543{
544 int ret;
545 struct btrfs_path *path;
546 struct btrfs_dev_item *dev_item;
547 struct extent_buffer *leaf;
548 struct btrfs_key key;
549 unsigned long ptr;
550 u64 free_devid;
551
552 root = root->fs_info->chunk_root;
553
554 path = btrfs_alloc_path();
555 if (!path)
556 return -ENOMEM;
557
558 ret = find_next_devid(root, path, &free_devid);
559 if (ret)
560 goto out;
561
562 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
563 key.type = BTRFS_DEV_ITEM_KEY;
564 key.offset = free_devid;
565
566 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -0400567 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -0400568 if (ret)
569 goto out;
570
571 leaf = path->nodes[0];
572 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
573
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 device->devid = free_devid;
Chris Mason0b86a832008-03-24 15:01:56 -0400575 btrfs_set_device_id(leaf, dev_item, device->devid);
576 btrfs_set_device_type(leaf, dev_item, device->type);
577 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
578 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
579 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400580 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
581 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -0400582 btrfs_set_device_group(leaf, dev_item, 0);
583 btrfs_set_device_seek_speed(leaf, dev_item, 0);
584 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400585
Chris Mason0b86a832008-03-24 15:01:56 -0400586 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -0400587 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400588 btrfs_mark_buffer_dirty(leaf);
589 ret = 0;
590
591out:
592 btrfs_free_path(path);
593 return ret;
594}
Chris Mason8f18cf12008-04-25 16:53:30 -0400595
Chris Mason788f20e2008-04-28 15:29:42 -0400596int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
597{
598 struct btrfs_trans_handle *trans;
599 struct btrfs_device *device;
600 struct block_device *bdev;
601 struct list_head *cur;
602 struct list_head *devices;
603 u64 total_bytes;
604 int ret = 0;
605
606
607 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
608 if (!bdev) {
609 return -EIO;
610 }
611 mutex_lock(&root->fs_info->fs_mutex);
612 trans = btrfs_start_transaction(root, 1);
613 devices = &root->fs_info->fs_devices->devices;
614 list_for_each(cur, devices) {
615 device = list_entry(cur, struct btrfs_device, dev_list);
616 if (device->bdev == bdev) {
617 ret = -EEXIST;
618 goto out;
619 }
620 }
621
622 device = kzalloc(sizeof(*device), GFP_NOFS);
623 if (!device) {
624 /* we can safely leave the fs_devices entry around */
625 ret = -ENOMEM;
626 goto out_close_bdev;
627 }
628
629 device->barriers = 1;
630 generate_random_uuid(device->uuid);
631 spin_lock_init(&device->io_lock);
632 device->name = kstrdup(device_path, GFP_NOFS);
633 if (!device->name) {
634 kfree(device);
635 goto out_close_bdev;
636 }
637 device->io_width = root->sectorsize;
638 device->io_align = root->sectorsize;
639 device->sector_size = root->sectorsize;
640 device->total_bytes = i_size_read(bdev->bd_inode);
641 device->dev_root = root->fs_info->dev_root;
642 device->bdev = bdev;
643
644 ret = btrfs_add_device(trans, root, device);
645 if (ret)
646 goto out_close_bdev;
647
648 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
649 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
650 total_bytes + device->total_bytes);
651
652 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
653 btrfs_set_super_num_devices(&root->fs_info->super_copy,
654 total_bytes + 1);
655
656 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
657 list_add(&device->dev_alloc_list,
658 &root->fs_info->fs_devices->alloc_list);
659 root->fs_info->fs_devices->num_devices++;
660out:
661 btrfs_end_transaction(trans, root);
662 mutex_unlock(&root->fs_info->fs_mutex);
663 return ret;
664
665out_close_bdev:
666 close_bdev_excl(bdev);
667 goto out;
668}
669
Chris Mason0b86a832008-03-24 15:01:56 -0400670int btrfs_update_device(struct btrfs_trans_handle *trans,
671 struct btrfs_device *device)
672{
673 int ret;
674 struct btrfs_path *path;
675 struct btrfs_root *root;
676 struct btrfs_dev_item *dev_item;
677 struct extent_buffer *leaf;
678 struct btrfs_key key;
679
680 root = device->dev_root->fs_info->chunk_root;
681
682 path = btrfs_alloc_path();
683 if (!path)
684 return -ENOMEM;
685
686 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
687 key.type = BTRFS_DEV_ITEM_KEY;
688 key.offset = device->devid;
689
690 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
691 if (ret < 0)
692 goto out;
693
694 if (ret > 0) {
695 ret = -ENOENT;
696 goto out;
697 }
698
699 leaf = path->nodes[0];
700 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
701
702 btrfs_set_device_id(leaf, dev_item, device->devid);
703 btrfs_set_device_type(leaf, dev_item, device->type);
704 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
705 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
706 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -0400707 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
708 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
709 btrfs_mark_buffer_dirty(leaf);
710
711out:
712 btrfs_free_path(path);
713 return ret;
714}
715
Chris Mason8f18cf12008-04-25 16:53:30 -0400716int btrfs_grow_device(struct btrfs_trans_handle *trans,
717 struct btrfs_device *device, u64 new_size)
718{
719 struct btrfs_super_block *super_copy =
720 &device->dev_root->fs_info->super_copy;
721 u64 old_total = btrfs_super_total_bytes(super_copy);
722 u64 diff = new_size - device->total_bytes;
723
724 btrfs_set_super_total_bytes(super_copy, old_total + diff);
725 return btrfs_update_device(trans, device);
726}
727
728static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
729 struct btrfs_root *root,
730 u64 chunk_tree, u64 chunk_objectid,
731 u64 chunk_offset)
732{
733 int ret;
734 struct btrfs_path *path;
735 struct btrfs_key key;
736
737 root = root->fs_info->chunk_root;
738 path = btrfs_alloc_path();
739 if (!path)
740 return -ENOMEM;
741
742 key.objectid = chunk_objectid;
743 key.offset = chunk_offset;
744 key.type = BTRFS_CHUNK_ITEM_KEY;
745
746 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
747 BUG_ON(ret);
748
749 ret = btrfs_del_item(trans, root, path);
750 BUG_ON(ret);
751
752 btrfs_free_path(path);
753 return 0;
754}
755
756int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
757 chunk_offset)
758{
759 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
760 struct btrfs_disk_key *disk_key;
761 struct btrfs_chunk *chunk;
762 u8 *ptr;
763 int ret = 0;
764 u32 num_stripes;
765 u32 array_size;
766 u32 len = 0;
767 u32 cur;
768 struct btrfs_key key;
769
770 array_size = btrfs_super_sys_array_size(super_copy);
771
772 ptr = super_copy->sys_chunk_array;
773 cur = 0;
774
775 while (cur < array_size) {
776 disk_key = (struct btrfs_disk_key *)ptr;
777 btrfs_disk_key_to_cpu(&key, disk_key);
778
779 len = sizeof(*disk_key);
780
781 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
782 chunk = (struct btrfs_chunk *)(ptr + len);
783 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
784 len += btrfs_chunk_item_size(num_stripes);
785 } else {
786 ret = -EIO;
787 break;
788 }
789 if (key.objectid == chunk_objectid &&
790 key.offset == chunk_offset) {
791 memmove(ptr, ptr + len, array_size - (cur + len));
792 array_size -= len;
793 btrfs_set_super_sys_array_size(super_copy, array_size);
794 } else {
795 ptr += len;
796 cur += len;
797 }
798 }
799 return ret;
800}
801
802
803int btrfs_relocate_chunk(struct btrfs_root *root,
804 u64 chunk_tree, u64 chunk_objectid,
805 u64 chunk_offset)
806{
807 struct extent_map_tree *em_tree;
808 struct btrfs_root *extent_root;
809 struct btrfs_trans_handle *trans;
810 struct extent_map *em;
811 struct map_lookup *map;
812 int ret;
813 int i;
814
815 root = root->fs_info->chunk_root;
816 extent_root = root->fs_info->extent_root;
817 em_tree = &root->fs_info->mapping_tree.map_tree;
818
819 /* step one, relocate all the extents inside this chunk */
820 ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
821 BUG_ON(ret);
822
823 trans = btrfs_start_transaction(root, 1);
824 BUG_ON(!trans);
825
826 /*
827 * step two, delete the device extents and the
828 * chunk tree entries
829 */
830 spin_lock(&em_tree->lock);
831 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
832 spin_unlock(&em_tree->lock);
833
834 BUG_ON(em->start > chunk_offset || em->start + em->len < chunk_offset);
835 map = (struct map_lookup *)em->bdev;
836
837 for (i = 0; i < map->num_stripes; i++) {
838 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
839 map->stripes[i].physical);
840 BUG_ON(ret);
841 }
842 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
843 chunk_offset);
844
845 BUG_ON(ret);
846
847 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
848 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
849 BUG_ON(ret);
850 goto out;
851 }
852
853
854
855 spin_lock(&em_tree->lock);
856 remove_extent_mapping(em_tree, em);
857 kfree(map);
858 em->bdev = NULL;
859
860 /* once for the tree */
861 free_extent_map(em);
862 spin_unlock(&em_tree->lock);
863
864out:
865 /* once for us */
866 free_extent_map(em);
867
868 btrfs_end_transaction(trans, root);
869 return 0;
870}
871
872/*
873 * shrinking a device means finding all of the device extents past
874 * the new size, and then following the back refs to the chunks.
875 * The chunk relocation code actually frees the device extent
876 */
877int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
878{
879 struct btrfs_trans_handle *trans;
880 struct btrfs_root *root = device->dev_root;
881 struct btrfs_dev_extent *dev_extent = NULL;
882 struct btrfs_path *path;
883 u64 length;
884 u64 chunk_tree;
885 u64 chunk_objectid;
886 u64 chunk_offset;
887 int ret;
888 int slot;
889 struct extent_buffer *l;
890 struct btrfs_key key;
891 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
892 u64 old_total = btrfs_super_total_bytes(super_copy);
893 u64 diff = device->total_bytes - new_size;
894
895
896 path = btrfs_alloc_path();
897 if (!path)
898 return -ENOMEM;
899
900 trans = btrfs_start_transaction(root, 1);
901 if (!trans) {
902 ret = -ENOMEM;
903 goto done;
904 }
905
906 path->reada = 2;
907
908 device->total_bytes = new_size;
909 ret = btrfs_update_device(trans, device);
910 if (ret) {
911 btrfs_end_transaction(trans, root);
912 goto done;
913 }
914 WARN_ON(diff > old_total);
915 btrfs_set_super_total_bytes(super_copy, old_total - diff);
916 btrfs_end_transaction(trans, root);
917
918 key.objectid = device->devid;
919 key.offset = (u64)-1;
920 key.type = BTRFS_DEV_EXTENT_KEY;
921
922 while (1) {
923 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
924 if (ret < 0)
925 goto done;
926
927 ret = btrfs_previous_item(root, path, 0, key.type);
928 if (ret < 0)
929 goto done;
930 if (ret) {
931 ret = 0;
932 goto done;
933 }
934
935 l = path->nodes[0];
936 slot = path->slots[0];
937 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
938
939 if (key.objectid != device->devid)
940 goto done;
941
942 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
943 length = btrfs_dev_extent_length(l, dev_extent);
944
945 if (key.offset + length <= new_size)
946 goto done;
947
948 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
949 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
950 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
951 btrfs_release_path(root, path);
952
953 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
954 chunk_offset);
955 if (ret)
956 goto done;
957 }
958
959done:
960 btrfs_free_path(path);
961 return ret;
962}
963
Chris Mason0b86a832008-03-24 15:01:56 -0400964int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
965 struct btrfs_root *root,
966 struct btrfs_key *key,
967 struct btrfs_chunk *chunk, int item_size)
968{
969 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
970 struct btrfs_disk_key disk_key;
971 u32 array_size;
972 u8 *ptr;
973
974 array_size = btrfs_super_sys_array_size(super_copy);
975 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
976 return -EFBIG;
977
978 ptr = super_copy->sys_chunk_array + array_size;
979 btrfs_cpu_key_to_disk(&disk_key, key);
980 memcpy(ptr, &disk_key, sizeof(disk_key));
981 ptr += sizeof(disk_key);
982 memcpy(ptr, chunk, item_size);
983 item_size += sizeof(disk_key);
984 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
985 return 0;
986}
987
Chris Mason9b3f68b2008-04-18 10:29:51 -0400988static u64 div_factor(u64 num, int factor)
989{
990 if (factor == 10)
991 return num;
992 num *= factor;
993 do_div(num, 10);
994 return num;
995}
996
997static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
998 int sub_stripes)
999{
1000 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1001 return calc_size;
1002 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1003 return calc_size * (num_stripes / sub_stripes);
1004 else
1005 return calc_size * num_stripes;
1006}
1007
1008
Chris Mason0b86a832008-03-24 15:01:56 -04001009int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1010 struct btrfs_root *extent_root, u64 *start,
Chris Mason6324fbf2008-03-24 15:01:59 -04001011 u64 *num_bytes, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04001012{
1013 u64 dev_offset;
Chris Mason593060d2008-03-25 16:50:33 -04001014 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04001015 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001016 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -04001017 struct btrfs_stripe *stripes;
1018 struct btrfs_device *device = NULL;
1019 struct btrfs_chunk *chunk;
Chris Mason6324fbf2008-03-24 15:01:59 -04001020 struct list_head private_devs;
Chris Masonb3075712008-04-22 09:22:07 -04001021 struct list_head *dev_list;
Chris Mason6324fbf2008-03-24 15:01:59 -04001022 struct list_head *cur;
Chris Mason0b86a832008-03-24 15:01:56 -04001023 struct extent_map_tree *em_tree;
1024 struct map_lookup *map;
1025 struct extent_map *em;
Chris Masona40a90a2008-04-18 11:55:51 -04001026 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001027 u64 physical;
1028 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001029 u64 max_chunk_size = calc_size;
1030 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04001031 u64 avail;
1032 u64 max_avail = 0;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001033 u64 percent_max;
Chris Mason6324fbf2008-03-24 15:01:59 -04001034 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04001035 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001036 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001037 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001038 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001039 int index;
Chris Mason593060d2008-03-25 16:50:33 -04001040 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04001041 struct btrfs_key key;
1042
Chris Masonb3075712008-04-22 09:22:07 -04001043 dev_list = &extent_root->fs_info->fs_devices->alloc_list;
Chris Mason6324fbf2008-03-24 15:01:59 -04001044 if (list_empty(dev_list))
1045 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04001046
Chris Masona40a90a2008-04-18 11:55:51 -04001047 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Chris Mason593060d2008-03-25 16:50:33 -04001048 num_stripes = btrfs_super_num_devices(&info->super_copy);
Chris Masona40a90a2008-04-18 11:55:51 -04001049 min_stripes = 2;
1050 }
1051 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04001052 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001053 min_stripes = 2;
1054 }
Chris Mason8790d502008-04-03 16:29:03 -04001055 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1056 num_stripes = min_t(u64, 2,
1057 btrfs_super_num_devices(&info->super_copy));
Chris Mason9b3f68b2008-04-18 10:29:51 -04001058 if (num_stripes < 2)
1059 return -ENOSPC;
Chris Masona40a90a2008-04-18 11:55:51 -04001060 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04001061 }
Chris Mason321aecc2008-04-16 10:49:51 -04001062 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1063 num_stripes = btrfs_super_num_devices(&info->super_copy);
1064 if (num_stripes < 4)
1065 return -ENOSPC;
1066 num_stripes &= ~(u32)1;
1067 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04001068 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04001069 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04001070
1071 if (type & BTRFS_BLOCK_GROUP_DATA) {
1072 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001073 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001074 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1075 max_chunk_size = 4 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04001076 min_stripe_size = 32 * 1024 * 1024;
1077 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1078 calc_size = 8 * 1024 * 1024;
1079 max_chunk_size = calc_size * 2;
1080 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001081 }
1082
Chris Mason8f18cf12008-04-25 16:53:30 -04001083 path = btrfs_alloc_path();
1084 if (!path)
1085 return -ENOMEM;
1086
Chris Mason9b3f68b2008-04-18 10:29:51 -04001087 /* we don't want a chunk larger than 10% of the FS */
1088 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
1089 max_chunk_size = min(percent_max, max_chunk_size);
1090
Chris Masona40a90a2008-04-18 11:55:51 -04001091again:
Chris Mason9b3f68b2008-04-18 10:29:51 -04001092 if (calc_size * num_stripes > max_chunk_size) {
1093 calc_size = max_chunk_size;
1094 do_div(calc_size, num_stripes);
1095 do_div(calc_size, stripe_len);
1096 calc_size *= stripe_len;
1097 }
1098 /* we don't want tiny stripes */
Chris Masona40a90a2008-04-18 11:55:51 -04001099 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04001100
Chris Mason9b3f68b2008-04-18 10:29:51 -04001101 do_div(calc_size, stripe_len);
1102 calc_size *= stripe_len;
1103
Chris Mason6324fbf2008-03-24 15:01:59 -04001104 INIT_LIST_HEAD(&private_devs);
1105 cur = dev_list->next;
1106 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04001107
1108 if (type & BTRFS_BLOCK_GROUP_DUP)
1109 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001110 else
1111 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04001112
Chris Masonad5bd912008-04-21 08:28:10 -04001113 /* we add 1MB because we never use the first 1MB of the device */
1114 min_free += 1024 * 1024;
1115
Chris Mason6324fbf2008-03-24 15:01:59 -04001116 /* build a private list of devices we will allocate from */
1117 while(index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04001118 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Chris Mason611f0e02008-04-03 16:29:03 -04001119
Chris Mason6324fbf2008-03-24 15:01:59 -04001120 avail = device->total_bytes - device->bytes_used;
1121 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04001122
Chris Mason611f0e02008-04-03 16:29:03 -04001123 if (avail >= min_free) {
Chris Mason8f18cf12008-04-25 16:53:30 -04001124 u64 ignored_start = 0;
1125 ret = find_free_dev_extent(trans, device, path,
1126 min_free,
1127 &ignored_start);
1128 if (ret == 0) {
1129 list_move_tail(&device->dev_alloc_list,
1130 &private_devs);
Chris Mason611f0e02008-04-03 16:29:03 -04001131 index++;
Chris Mason8f18cf12008-04-25 16:53:30 -04001132 if (type & BTRFS_BLOCK_GROUP_DUP)
1133 index++;
1134 }
Chris Masona40a90a2008-04-18 11:55:51 -04001135 } else if (avail > max_avail)
1136 max_avail = avail;
Chris Mason6324fbf2008-03-24 15:01:59 -04001137 if (cur == dev_list)
1138 break;
1139 }
1140 if (index < num_stripes) {
1141 list_splice(&private_devs, dev_list);
Chris Masona40a90a2008-04-18 11:55:51 -04001142 if (index >= min_stripes) {
1143 num_stripes = index;
1144 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1145 num_stripes /= sub_stripes;
1146 num_stripes *= sub_stripes;
1147 }
1148 looped = 1;
1149 goto again;
1150 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001151 if (!looped && max_avail > 0) {
1152 looped = 1;
1153 calc_size = max_avail;
1154 goto again;
1155 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001156 btrfs_free_path(path);
Chris Mason6324fbf2008-03-24 15:01:59 -04001157 return -ENOSPC;
1158 }
Chris Masone17cade2008-04-15 15:41:47 -04001159 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1160 key.type = BTRFS_CHUNK_ITEM_KEY;
1161 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1162 &key.offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001163 if (ret) {
1164 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001165 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001166 }
Chris Mason0b86a832008-03-24 15:01:56 -04001167
Chris Mason0b86a832008-03-24 15:01:56 -04001168 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
Chris Mason8f18cf12008-04-25 16:53:30 -04001169 if (!chunk) {
1170 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001171 return -ENOMEM;
Chris Mason8f18cf12008-04-25 16:53:30 -04001172 }
Chris Mason0b86a832008-03-24 15:01:56 -04001173
Chris Mason593060d2008-03-25 16:50:33 -04001174 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1175 if (!map) {
1176 kfree(chunk);
Chris Mason8f18cf12008-04-25 16:53:30 -04001177 btrfs_free_path(path);
Chris Mason593060d2008-03-25 16:50:33 -04001178 return -ENOMEM;
1179 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001180 btrfs_free_path(path);
1181 path = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04001182
Chris Mason0b86a832008-03-24 15:01:56 -04001183 stripes = &chunk->stripe;
Chris Mason9b3f68b2008-04-18 10:29:51 -04001184 *num_bytes = chunk_bytes_by_type(type, calc_size,
1185 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04001186
Chris Mason6324fbf2008-03-24 15:01:59 -04001187 index = 0;
Chris Masone17cade2008-04-15 15:41:47 -04001188printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001189 while(index < num_stripes) {
Chris Masone17cade2008-04-15 15:41:47 -04001190 struct btrfs_stripe *stripe;
Chris Mason6324fbf2008-03-24 15:01:59 -04001191 BUG_ON(list_empty(&private_devs));
1192 cur = private_devs.next;
Chris Masonb3075712008-04-22 09:22:07 -04001193 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Chris Mason611f0e02008-04-03 16:29:03 -04001194
1195 /* loop over this device again if we're doing a dup group */
1196 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1197 (index == num_stripes - 1))
Chris Masonb3075712008-04-22 09:22:07 -04001198 list_move_tail(&device->dev_alloc_list, dev_list);
Chris Mason0b86a832008-03-24 15:01:56 -04001199
1200 ret = btrfs_alloc_dev_extent(trans, device,
Chris Masone17cade2008-04-15 15:41:47 -04001201 info->chunk_root->root_key.objectid,
1202 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1203 calc_size, &dev_offset);
Chris Mason0b86a832008-03-24 15:01:56 -04001204 BUG_ON(ret);
Chris Masone17cade2008-04-15 15:41:47 -04001205printk("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 -04001206 device->bytes_used += calc_size;
1207 ret = btrfs_update_device(trans, device);
1208 BUG_ON(ret);
1209
Chris Mason593060d2008-03-25 16:50:33 -04001210 map->stripes[index].dev = device;
1211 map->stripes[index].physical = dev_offset;
Chris Masone17cade2008-04-15 15:41:47 -04001212 stripe = stripes + index;
1213 btrfs_set_stack_stripe_devid(stripe, device->devid);
1214 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1215 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001216 physical = dev_offset;
1217 index++;
1218 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001219 BUG_ON(!list_empty(&private_devs));
Chris Mason0b86a832008-03-24 15:01:56 -04001220
Chris Masone17cade2008-04-15 15:41:47 -04001221 /* key was set above */
1222 btrfs_set_stack_chunk_length(chunk, *num_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001223 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
Chris Mason593060d2008-03-25 16:50:33 -04001224 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -04001225 btrfs_set_stack_chunk_type(chunk, type);
1226 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001227 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1228 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
Chris Mason0b86a832008-03-24 15:01:56 -04001229 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
Chris Mason321aecc2008-04-16 10:49:51 -04001230 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001231 map->sector_size = extent_root->sectorsize;
1232 map->stripe_len = stripe_len;
1233 map->io_align = stripe_len;
1234 map->io_width = stripe_len;
1235 map->type = type;
1236 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001237 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04001238
1239 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1240 btrfs_chunk_item_size(num_stripes));
1241 BUG_ON(ret);
Chris Masone17cade2008-04-15 15:41:47 -04001242 *start = key.offset;;
Chris Mason0b86a832008-03-24 15:01:56 -04001243
1244 em = alloc_extent_map(GFP_NOFS);
1245 if (!em)
1246 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001247 em->bdev = (struct block_device *)map;
Chris Masone17cade2008-04-15 15:41:47 -04001248 em->start = key.offset;
1249 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04001250 em->block_start = 0;
1251
Chris Mason8f18cf12008-04-25 16:53:30 -04001252 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1253 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1254 chunk, btrfs_chunk_item_size(num_stripes));
1255 BUG_ON(ret);
1256 }
Chris Mason0b86a832008-03-24 15:01:56 -04001257 kfree(chunk);
1258
1259 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
1260 spin_lock(&em_tree->lock);
1261 ret = add_extent_mapping(em_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04001262 spin_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04001263 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001264 free_extent_map(em);
1265 return ret;
1266}
1267
1268void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1269{
1270 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
1271}
1272
1273void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
1274{
1275 struct extent_map *em;
1276
1277 while(1) {
1278 spin_lock(&tree->map_tree.lock);
1279 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
1280 if (em)
1281 remove_extent_mapping(&tree->map_tree, em);
1282 spin_unlock(&tree->map_tree.lock);
1283 if (!em)
1284 break;
1285 kfree(em->bdev);
1286 /* once for us */
1287 free_extent_map(em);
1288 /* once for the tree */
1289 free_extent_map(em);
1290 }
1291}
1292
Chris Masonf1885912008-04-09 16:28:12 -04001293int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1294{
1295 struct extent_map *em;
1296 struct map_lookup *map;
1297 struct extent_map_tree *em_tree = &map_tree->map_tree;
1298 int ret;
1299
1300 spin_lock(&em_tree->lock);
1301 em = lookup_extent_mapping(em_tree, logical, len);
Chris Masonb248a412008-04-14 09:48:18 -04001302 spin_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04001303 BUG_ON(!em);
1304
1305 BUG_ON(em->start > logical || em->start + em->len < logical);
1306 map = (struct map_lookup *)em->bdev;
1307 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1308 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001309 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1310 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001311 else
1312 ret = 1;
1313 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04001314 return ret;
1315}
1316
Chris Masonf2d8d742008-04-21 10:03:05 -04001317static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1318 u64 logical, u64 *length,
1319 struct btrfs_multi_bio **multi_ret,
1320 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04001321{
1322 struct extent_map *em;
1323 struct map_lookup *map;
1324 struct extent_map_tree *em_tree = &map_tree->map_tree;
1325 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04001326 u64 stripe_offset;
1327 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04001328 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04001329 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04001330 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04001331 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04001332 int num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04001333 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001334
Chris Masoncea9e442008-04-09 16:28:12 -04001335 if (multi_ret && !(rw & (1 << BIO_RW))) {
1336 stripes_allocated = 1;
1337 }
1338again:
1339 if (multi_ret) {
1340 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1341 GFP_NOFS);
1342 if (!multi)
1343 return -ENOMEM;
1344 }
Chris Mason0b86a832008-03-24 15:01:56 -04001345
1346 spin_lock(&em_tree->lock);
1347 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Masonb248a412008-04-14 09:48:18 -04001348 spin_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04001349
1350 if (!em && unplug_page)
1351 return 0;
1352
Chris Mason3b951512008-04-17 11:29:12 -04001353 if (!em) {
1354 printk("unable to find logical %Lu\n", logical);
Chris Masonf2d8d742008-04-21 10:03:05 -04001355 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04001356 }
Chris Mason0b86a832008-03-24 15:01:56 -04001357
1358 BUG_ON(em->start > logical || em->start + em->len < logical);
1359 map = (struct map_lookup *)em->bdev;
1360 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04001361
Chris Masonf1885912008-04-09 16:28:12 -04001362 if (mirror_num > map->num_stripes)
1363 mirror_num = 0;
1364
Chris Masoncea9e442008-04-09 16:28:12 -04001365 /* if our multi bio struct is too small, back off and try again */
Chris Mason321aecc2008-04-16 10:49:51 -04001366 if (rw & (1 << BIO_RW)) {
1367 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1368 BTRFS_BLOCK_GROUP_DUP)) {
1369 stripes_required = map->num_stripes;
1370 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1371 stripes_required = map->sub_stripes;
1372 }
1373 }
1374 if (multi_ret && rw == WRITE &&
1375 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04001376 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04001377 free_extent_map(em);
1378 kfree(multi);
1379 goto again;
1380 }
Chris Mason593060d2008-03-25 16:50:33 -04001381 stripe_nr = offset;
1382 /*
1383 * stripe_nr counts the total number of stripes we have to stride
1384 * to get to this block
1385 */
1386 do_div(stripe_nr, map->stripe_len);
1387
1388 stripe_offset = stripe_nr * map->stripe_len;
1389 BUG_ON(offset < stripe_offset);
1390
1391 /* stripe_offset is the offset of this block in its stripe*/
1392 stripe_offset = offset - stripe_offset;
1393
Chris Masoncea9e442008-04-09 16:28:12 -04001394 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001395 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04001396 BTRFS_BLOCK_GROUP_DUP)) {
1397 /* we limit the length of each bio to what fits in a stripe */
1398 *length = min_t(u64, em->len - offset,
1399 map->stripe_len - stripe_offset);
1400 } else {
1401 *length = em->len - offset;
1402 }
Chris Masonf2d8d742008-04-21 10:03:05 -04001403
1404 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04001405 goto out;
1406
Chris Masonf2d8d742008-04-21 10:03:05 -04001407 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04001408 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001409 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Chris Masonf2d8d742008-04-21 10:03:05 -04001410 if (unplug_page || (rw & (1 << BIO_RW)))
1411 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001412 else if (mirror_num) {
1413 stripe_index = mirror_num - 1;
1414 } else {
Chris Mason3c12ac72008-04-21 12:01:38 -04001415 u64 orig_stripe_nr = stripe_nr;
1416 stripe_index = do_div(orig_stripe_nr, num_stripes);
Chris Mason8790d502008-04-03 16:29:03 -04001417 }
Chris Mason611f0e02008-04-03 16:29:03 -04001418 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Chris Masoncea9e442008-04-09 16:28:12 -04001419 if (rw & (1 << BIO_RW))
Chris Masonf2d8d742008-04-21 10:03:05 -04001420 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04001421 else if (mirror_num)
1422 stripe_index = mirror_num - 1;
Chris Mason321aecc2008-04-16 10:49:51 -04001423 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1424 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001425
1426 stripe_index = do_div(stripe_nr, factor);
1427 stripe_index *= map->sub_stripes;
1428
Chris Masonf2d8d742008-04-21 10:03:05 -04001429 if (unplug_page || (rw & (1 << BIO_RW)))
1430 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04001431 else if (mirror_num)
1432 stripe_index += mirror_num - 1;
Chris Mason3c12ac72008-04-21 12:01:38 -04001433 else {
1434 u64 orig_stripe_nr = stripe_nr;
1435 stripe_index += do_div(orig_stripe_nr,
1436 map->sub_stripes);
1437 }
Chris Mason8790d502008-04-03 16:29:03 -04001438 } else {
1439 /*
1440 * after this do_div call, stripe_nr is the number of stripes
1441 * on this device we have to walk to find the data, and
1442 * stripe_index is the number of our device in the stripe array
1443 */
1444 stripe_index = do_div(stripe_nr, map->num_stripes);
1445 }
Chris Mason593060d2008-03-25 16:50:33 -04001446 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04001447
Chris Masonf2d8d742008-04-21 10:03:05 -04001448 for (i = 0; i < num_stripes; i++) {
1449 if (unplug_page) {
1450 struct btrfs_device *device;
1451 struct backing_dev_info *bdi;
1452
1453 device = map->stripes[stripe_index].dev;
1454 bdi = blk_get_backing_dev_info(device->bdev);
1455 if (bdi->unplug_io_fn) {
1456 bdi->unplug_io_fn(bdi, unplug_page);
1457 }
1458 } else {
1459 multi->stripes[i].physical =
1460 map->stripes[stripe_index].physical +
1461 stripe_offset + stripe_nr * map->stripe_len;
1462 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1463 }
Chris Masoncea9e442008-04-09 16:28:12 -04001464 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04001465 }
Chris Masonf2d8d742008-04-21 10:03:05 -04001466 if (multi_ret) {
1467 *multi_ret = multi;
1468 multi->num_stripes = num_stripes;
1469 }
Chris Masoncea9e442008-04-09 16:28:12 -04001470out:
Chris Mason0b86a832008-03-24 15:01:56 -04001471 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04001472 return 0;
1473}
1474
Chris Masonf2d8d742008-04-21 10:03:05 -04001475int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1476 u64 logical, u64 *length,
1477 struct btrfs_multi_bio **multi_ret, int mirror_num)
1478{
1479 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
1480 mirror_num, NULL);
1481}
1482
1483int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
1484 u64 logical, struct page *page)
1485{
1486 u64 length = PAGE_CACHE_SIZE;
1487 return __btrfs_map_block(map_tree, READ, logical, &length,
1488 NULL, 0, page);
1489}
1490
1491
Chris Mason8790d502008-04-03 16:29:03 -04001492#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1493static void end_bio_multi_stripe(struct bio *bio, int err)
1494#else
1495static int end_bio_multi_stripe(struct bio *bio,
1496 unsigned int bytes_done, int err)
1497#endif
1498{
Chris Masoncea9e442008-04-09 16:28:12 -04001499 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason8790d502008-04-03 16:29:03 -04001500
1501#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1502 if (bio->bi_size)
1503 return 1;
1504#endif
1505 if (err)
1506 multi->error = err;
1507
Chris Masoncea9e442008-04-09 16:28:12 -04001508 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason8790d502008-04-03 16:29:03 -04001509 bio->bi_private = multi->private;
1510 bio->bi_end_io = multi->end_io;
1511
1512 if (!err && multi->error)
1513 err = multi->error;
1514 kfree(multi);
1515
Miguel73f61b22008-04-11 15:50:59 -04001516#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1517 bio_endio(bio, bio->bi_size, err);
1518#else
Chris Mason8790d502008-04-03 16:29:03 -04001519 bio_endio(bio, err);
Miguel73f61b22008-04-11 15:50:59 -04001520#endif
Chris Mason8790d502008-04-03 16:29:03 -04001521 } else {
1522 bio_put(bio);
1523 }
1524#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1525 return 0;
1526#endif
1527}
1528
Chris Masonf1885912008-04-09 16:28:12 -04001529int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1530 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04001531{
1532 struct btrfs_mapping_tree *map_tree;
1533 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04001534 struct bio *first_bio = bio;
Chris Mason0b86a832008-03-24 15:01:56 -04001535 u64 logical = bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04001536 u64 length = 0;
1537 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04001538 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001539 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04001540 int dev_nr = 0;
1541 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001542
Chris Masonf2d8d742008-04-21 10:03:05 -04001543 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001544 map_tree = &root->fs_info->mapping_tree;
1545 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001546
Chris Masonf1885912008-04-09 16:28:12 -04001547 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
1548 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04001549 BUG_ON(ret);
1550
1551 total_devs = multi->num_stripes;
1552 if (map_length < length) {
1553 printk("mapping failed logical %Lu bio len %Lu "
1554 "len %Lu\n", logical, length, map_length);
1555 BUG();
1556 }
1557 multi->end_io = first_bio->bi_end_io;
1558 multi->private = first_bio->bi_private;
1559 atomic_set(&multi->stripes_pending, multi->num_stripes);
1560
Chris Mason8790d502008-04-03 16:29:03 -04001561 while(dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04001562 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04001563 if (dev_nr < total_devs - 1) {
1564 bio = bio_clone(first_bio, GFP_NOFS);
1565 BUG_ON(!bio);
1566 } else {
1567 bio = first_bio;
1568 }
1569 bio->bi_private = multi;
1570 bio->bi_end_io = end_bio_multi_stripe;
1571 }
Chris Masoncea9e442008-04-09 16:28:12 -04001572 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1573 dev = multi->stripes[dev_nr].dev;
Chris Masone1c4b742008-04-22 13:26:46 -04001574
Chris Mason8790d502008-04-03 16:29:03 -04001575 bio->bi_bdev = dev->bdev;
1576 spin_lock(&dev->io_lock);
1577 dev->total_ios++;
1578 spin_unlock(&dev->io_lock);
1579 submit_bio(rw, bio);
1580 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04001581 }
Chris Masoncea9e442008-04-09 16:28:12 -04001582 if (total_devs == 1)
1583 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04001584 return 0;
1585}
1586
Chris Masona4437552008-04-18 10:29:38 -04001587struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1588 u8 *uuid)
Chris Mason0b86a832008-03-24 15:01:56 -04001589{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001590 struct list_head *head = &root->fs_info->fs_devices->devices;
Chris Mason0b86a832008-03-24 15:01:56 -04001591
Chris Masona4437552008-04-18 10:29:38 -04001592 return __find_device(head, devid, uuid);
Chris Mason0b86a832008-03-24 15:01:56 -04001593}
1594
1595static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1596 struct extent_buffer *leaf,
1597 struct btrfs_chunk *chunk)
1598{
1599 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1600 struct map_lookup *map;
1601 struct extent_map *em;
1602 u64 logical;
1603 u64 length;
1604 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04001605 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04001606 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04001607 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04001608 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04001609
Chris Masone17cade2008-04-15 15:41:47 -04001610 logical = key->offset;
1611 length = btrfs_chunk_length(leaf, chunk);
Chris Mason0b86a832008-03-24 15:01:56 -04001612 spin_lock(&map_tree->map_tree.lock);
1613 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Masonb248a412008-04-14 09:48:18 -04001614 spin_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04001615
1616 /* already mapped? */
1617 if (em && em->start <= logical && em->start + em->len > logical) {
1618 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04001619 return 0;
1620 } else if (em) {
1621 free_extent_map(em);
1622 }
Chris Mason0b86a832008-03-24 15:01:56 -04001623
1624 map = kzalloc(sizeof(*map), GFP_NOFS);
1625 if (!map)
1626 return -ENOMEM;
1627
1628 em = alloc_extent_map(GFP_NOFS);
1629 if (!em)
1630 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04001631 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1632 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04001633 if (!map) {
1634 free_extent_map(em);
1635 return -ENOMEM;
1636 }
1637
1638 em->bdev = (struct block_device *)map;
1639 em->start = logical;
1640 em->len = length;
1641 em->block_start = 0;
1642
Chris Mason593060d2008-03-25 16:50:33 -04001643 map->num_stripes = num_stripes;
1644 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1645 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1646 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1647 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1648 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04001649 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04001650 for (i = 0; i < num_stripes; i++) {
1651 map->stripes[i].physical =
1652 btrfs_stripe_offset_nr(leaf, chunk, i);
1653 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04001654 read_extent_buffer(leaf, uuid, (unsigned long)
1655 btrfs_stripe_dev_uuid_nr(chunk, i),
1656 BTRFS_UUID_SIZE);
1657 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
Chris Mason593060d2008-03-25 16:50:33 -04001658 if (!map->stripes[i].dev) {
1659 kfree(map);
1660 free_extent_map(em);
1661 return -EIO;
1662 }
Chris Mason0b86a832008-03-24 15:01:56 -04001663 }
1664
1665 spin_lock(&map_tree->map_tree.lock);
1666 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason0b86a832008-03-24 15:01:56 -04001667 spin_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04001668 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04001669 free_extent_map(em);
1670
1671 return 0;
1672}
1673
1674static int fill_device_from_item(struct extent_buffer *leaf,
1675 struct btrfs_dev_item *dev_item,
1676 struct btrfs_device *device)
1677{
1678 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001679
1680 device->devid = btrfs_device_id(leaf, dev_item);
1681 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1682 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1683 device->type = btrfs_device_type(leaf, dev_item);
1684 device->io_align = btrfs_device_io_align(leaf, dev_item);
1685 device->io_width = btrfs_device_io_width(leaf, dev_item);
1686 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04001687
1688 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001689 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001690
Chris Mason0b86a832008-03-24 15:01:56 -04001691 return 0;
1692}
1693
Chris Mason0d81ba52008-03-24 15:02:07 -04001694static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04001695 struct extent_buffer *leaf,
1696 struct btrfs_dev_item *dev_item)
1697{
1698 struct btrfs_device *device;
1699 u64 devid;
1700 int ret;
Chris Masona4437552008-04-18 10:29:38 -04001701 u8 dev_uuid[BTRFS_UUID_SIZE];
1702
Chris Mason0b86a832008-03-24 15:01:56 -04001703 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04001704 read_extent_buffer(leaf, dev_uuid,
1705 (unsigned long)btrfs_device_uuid(dev_item),
1706 BTRFS_UUID_SIZE);
1707 device = btrfs_find_device(root, devid, dev_uuid);
Chris Mason6324fbf2008-03-24 15:01:59 -04001708 if (!device) {
Chris Mason8a4b83c2008-03-24 15:02:07 -04001709 printk("warning devid %Lu not found already\n", devid);
Chris Masonf2984462008-04-10 16:19:33 -04001710 device = kzalloc(sizeof(*device), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04001711 if (!device)
1712 return -ENOMEM;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001713 list_add(&device->dev_list,
1714 &root->fs_info->fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -04001715 list_add(&device->dev_alloc_list,
1716 &root->fs_info->fs_devices->alloc_list);
Chris Masonb248a412008-04-14 09:48:18 -04001717 device->barriers = 1;
Chris Mason8790d502008-04-03 16:29:03 -04001718 spin_lock_init(&device->io_lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04001719 }
Chris Mason0b86a832008-03-24 15:01:56 -04001720
1721 fill_device_from_item(leaf, dev_item, device);
1722 device->dev_root = root->fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001723 ret = 0;
1724#if 0
1725 ret = btrfs_open_device(device);
1726 if (ret) {
1727 kfree(device);
1728 }
1729#endif
1730 return ret;
1731}
1732
Chris Mason0d81ba52008-03-24 15:02:07 -04001733int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1734{
1735 struct btrfs_dev_item *dev_item;
1736
1737 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1738 dev_item);
1739 return read_one_dev(root, buf, dev_item);
1740}
1741
Chris Mason0b86a832008-03-24 15:01:56 -04001742int btrfs_read_sys_array(struct btrfs_root *root)
1743{
1744 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1745 struct extent_buffer *sb = root->fs_info->sb_buffer;
1746 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04001747 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04001748 u8 *ptr;
1749 unsigned long sb_ptr;
1750 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001751 u32 num_stripes;
1752 u32 array_size;
1753 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001754 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04001755 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04001756
1757 array_size = btrfs_super_sys_array_size(super_copy);
1758
Chris Mason0b86a832008-03-24 15:01:56 -04001759 ptr = super_copy->sys_chunk_array;
1760 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1761 cur = 0;
1762
1763 while (cur < array_size) {
1764 disk_key = (struct btrfs_disk_key *)ptr;
1765 btrfs_disk_key_to_cpu(&key, disk_key);
1766
1767 len = sizeof(*disk_key);
1768 ptr += len;
1769 sb_ptr += len;
1770 cur += len;
1771
Chris Mason0d81ba52008-03-24 15:02:07 -04001772 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04001773 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04001774 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04001775 if (ret)
1776 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001777 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1778 len = btrfs_chunk_item_size(num_stripes);
1779 } else {
Chris Mason84eed902008-04-25 09:04:37 -04001780 ret = -EIO;
1781 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001782 }
1783 ptr += len;
1784 sb_ptr += len;
1785 cur += len;
1786 }
Chris Mason84eed902008-04-25 09:04:37 -04001787 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04001788}
1789
1790int btrfs_read_chunk_tree(struct btrfs_root *root)
1791{
1792 struct btrfs_path *path;
1793 struct extent_buffer *leaf;
1794 struct btrfs_key key;
1795 struct btrfs_key found_key;
1796 int ret;
1797 int slot;
1798
1799 root = root->fs_info->chunk_root;
1800
1801 path = btrfs_alloc_path();
1802 if (!path)
1803 return -ENOMEM;
1804
1805 /* first we search for all of the device items, and then we
1806 * read in all of the chunk items. This way we can create chunk
1807 * mappings that reference all of the devices that are afound
1808 */
1809 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1810 key.offset = 0;
1811 key.type = 0;
1812again:
1813 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1814 while(1) {
1815 leaf = path->nodes[0];
1816 slot = path->slots[0];
1817 if (slot >= btrfs_header_nritems(leaf)) {
1818 ret = btrfs_next_leaf(root, path);
1819 if (ret == 0)
1820 continue;
1821 if (ret < 0)
1822 goto error;
1823 break;
1824 }
1825 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1826 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1827 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1828 break;
1829 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1830 struct btrfs_dev_item *dev_item;
1831 dev_item = btrfs_item_ptr(leaf, slot,
1832 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04001833 ret = read_one_dev(root, leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04001834 BUG_ON(ret);
1835 }
1836 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1837 struct btrfs_chunk *chunk;
1838 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1839 ret = read_one_chunk(root, &found_key, leaf, chunk);
1840 }
1841 path->slots[0]++;
1842 }
1843 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1844 key.objectid = 0;
1845 btrfs_release_path(root, path);
1846 goto again;
1847 }
1848
1849 btrfs_free_path(path);
1850 ret = 0;
1851error:
1852 return ret;
1853}
1854