diff options
author | Chris Mason <chris.mason@oracle.com> | 2008-03-24 15:01:56 -0400 |
---|---|---|
committer | Chris Mason <chris.mason@oracle.com> | 2008-09-25 11:04:00 -0400 |
commit | 0b86a832a1f38abec695864ec2eaedc9d2383f1b (patch) | |
tree | 8ec0db0b55f77eb6a23fe7db3c1064d298db55c1 /fs/btrfs/volumes.c | |
parent | 7f93bf8d27653726e3721c01fefc523487ecf2af (diff) |
Btrfs: Add support for multiple devices per filesystem
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/volumes.c')
-rw-r--r-- | fs/btrfs/volumes.c | 852 |
1 files changed, 852 insertions, 0 deletions
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c new file mode 100644 index 000000000000..90a8d45dc6d7 --- /dev/null +++ b/fs/btrfs/volumes.c | |||
@@ -0,0 +1,852 @@ | |||
1 | /* | ||
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> | ||
20 | #include "ctree.h" | ||
21 | #include "extent_map.h" | ||
22 | #include "disk-io.h" | ||
23 | #include "transaction.h" | ||
24 | #include "print-tree.h" | ||
25 | #include "volumes.h" | ||
26 | |||
27 | struct map_lookup { | ||
28 | struct btrfs_device *dev; | ||
29 | u64 physical; | ||
30 | }; | ||
31 | |||
32 | /* | ||
33 | * this uses a pretty simple search, the expectation is that it is | ||
34 | * called very infrequently and that a given device has a small number | ||
35 | * of extents | ||
36 | */ | ||
37 | static int find_free_dev_extent(struct btrfs_trans_handle *trans, | ||
38 | struct btrfs_device *device, | ||
39 | struct btrfs_path *path, | ||
40 | u64 num_bytes, u64 *start) | ||
41 | { | ||
42 | struct btrfs_key key; | ||
43 | struct btrfs_root *root = device->dev_root; | ||
44 | struct btrfs_dev_extent *dev_extent = NULL; | ||
45 | u64 hole_size = 0; | ||
46 | u64 last_byte = 0; | ||
47 | u64 search_start = 0; | ||
48 | u64 search_end = device->total_bytes; | ||
49 | int ret; | ||
50 | int slot = 0; | ||
51 | int start_found; | ||
52 | struct extent_buffer *l; | ||
53 | |||
54 | start_found = 0; | ||
55 | path->reada = 2; | ||
56 | |||
57 | /* FIXME use last free of some kind */ | ||
58 | |||
59 | key.objectid = device->devid; | ||
60 | key.offset = search_start; | ||
61 | key.type = BTRFS_DEV_EXTENT_KEY; | ||
62 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); | ||
63 | if (ret < 0) | ||
64 | goto error; | ||
65 | ret = btrfs_previous_item(root, path, 0, key.type); | ||
66 | if (ret < 0) | ||
67 | goto error; | ||
68 | l = path->nodes[0]; | ||
69 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | ||
70 | while (1) { | ||
71 | l = path->nodes[0]; | ||
72 | slot = path->slots[0]; | ||
73 | if (slot >= btrfs_header_nritems(l)) { | ||
74 | ret = btrfs_next_leaf(root, path); | ||
75 | if (ret == 0) | ||
76 | continue; | ||
77 | if (ret < 0) | ||
78 | goto error; | ||
79 | no_more_items: | ||
80 | if (!start_found) { | ||
81 | if (search_start >= search_end) { | ||
82 | ret = -ENOSPC; | ||
83 | goto error; | ||
84 | } | ||
85 | *start = search_start; | ||
86 | start_found = 1; | ||
87 | goto check_pending; | ||
88 | } | ||
89 | *start = last_byte > search_start ? | ||
90 | last_byte : search_start; | ||
91 | if (search_end <= *start) { | ||
92 | ret = -ENOSPC; | ||
93 | goto error; | ||
94 | } | ||
95 | goto check_pending; | ||
96 | } | ||
97 | btrfs_item_key_to_cpu(l, &key, slot); | ||
98 | |||
99 | if (key.objectid < device->devid) | ||
100 | goto next; | ||
101 | |||
102 | if (key.objectid > device->devid) | ||
103 | goto no_more_items; | ||
104 | |||
105 | if (key.offset >= search_start && key.offset > last_byte && | ||
106 | start_found) { | ||
107 | if (last_byte < search_start) | ||
108 | last_byte = search_start; | ||
109 | hole_size = key.offset - last_byte; | ||
110 | if (key.offset > last_byte && | ||
111 | hole_size >= num_bytes) { | ||
112 | *start = last_byte; | ||
113 | goto check_pending; | ||
114 | } | ||
115 | } | ||
116 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) { | ||
117 | goto next; | ||
118 | } | ||
119 | |||
120 | start_found = 1; | ||
121 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | ||
122 | last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent); | ||
123 | next: | ||
124 | path->slots[0]++; | ||
125 | cond_resched(); | ||
126 | } | ||
127 | check_pending: | ||
128 | /* we have to make sure we didn't find an extent that has already | ||
129 | * been allocated by the map tree or the original allocation | ||
130 | */ | ||
131 | btrfs_release_path(root, path); | ||
132 | BUG_ON(*start < search_start); | ||
133 | |||
134 | if (*start + num_bytes >= search_end) { | ||
135 | ret = -ENOSPC; | ||
136 | goto error; | ||
137 | } | ||
138 | /* check for pending inserts here */ | ||
139 | return 0; | ||
140 | |||
141 | error: | ||
142 | btrfs_release_path(root, path); | ||
143 | return ret; | ||
144 | } | ||
145 | |||
146 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, | ||
147 | struct btrfs_device *device, | ||
148 | u64 owner, u64 num_bytes, u64 *start) | ||
149 | { | ||
150 | int ret; | ||
151 | struct btrfs_path *path; | ||
152 | struct btrfs_root *root = device->dev_root; | ||
153 | struct btrfs_dev_extent *extent; | ||
154 | struct extent_buffer *leaf; | ||
155 | struct btrfs_key key; | ||
156 | |||
157 | path = btrfs_alloc_path(); | ||
158 | if (!path) | ||
159 | return -ENOMEM; | ||
160 | |||
161 | ret = find_free_dev_extent(trans, device, path, num_bytes, start); | ||
162 | if (ret) | ||
163 | goto err; | ||
164 | |||
165 | key.objectid = device->devid; | ||
166 | key.offset = *start; | ||
167 | key.type = BTRFS_DEV_EXTENT_KEY; | ||
168 | ret = btrfs_insert_empty_item(trans, root, path, &key, | ||
169 | sizeof(*extent)); | ||
170 | BUG_ON(ret); | ||
171 | |||
172 | leaf = path->nodes[0]; | ||
173 | extent = btrfs_item_ptr(leaf, path->slots[0], | ||
174 | struct btrfs_dev_extent); | ||
175 | btrfs_set_dev_extent_owner(leaf, extent, owner); | ||
176 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); | ||
177 | btrfs_mark_buffer_dirty(leaf); | ||
178 | err: | ||
179 | btrfs_free_path(path); | ||
180 | return ret; | ||
181 | } | ||
182 | |||
183 | static int find_next_chunk(struct btrfs_root *root, u64 *objectid) | ||
184 | { | ||
185 | struct btrfs_path *path; | ||
186 | int ret; | ||
187 | struct btrfs_key key; | ||
188 | struct btrfs_key found_key; | ||
189 | |||
190 | path = btrfs_alloc_path(); | ||
191 | BUG_ON(!path); | ||
192 | |||
193 | key.objectid = (u64)-1; | ||
194 | key.offset = (u64)-1; | ||
195 | key.type = BTRFS_CHUNK_ITEM_KEY; | ||
196 | |||
197 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | ||
198 | if (ret < 0) | ||
199 | goto error; | ||
200 | |||
201 | BUG_ON(ret == 0); | ||
202 | |||
203 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); | ||
204 | if (ret) { | ||
205 | *objectid = 0; | ||
206 | } else { | ||
207 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | ||
208 | path->slots[0]); | ||
209 | *objectid = found_key.objectid + found_key.offset; | ||
210 | } | ||
211 | ret = 0; | ||
212 | error: | ||
213 | btrfs_free_path(path); | ||
214 | return ret; | ||
215 | } | ||
216 | |||
217 | static struct btrfs_device *next_device(struct list_head *head, | ||
218 | struct list_head *last) | ||
219 | { | ||
220 | struct list_head *next = last->next; | ||
221 | struct btrfs_device *dev; | ||
222 | |||
223 | if (list_empty(head)) | ||
224 | return NULL; | ||
225 | |||
226 | if (next == head) | ||
227 | next = next->next; | ||
228 | |||
229 | dev = list_entry(next, struct btrfs_device, dev_list); | ||
230 | return dev; | ||
231 | } | ||
232 | |||
233 | static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path, | ||
234 | u64 *objectid) | ||
235 | { | ||
236 | int ret; | ||
237 | struct btrfs_key key; | ||
238 | struct btrfs_key found_key; | ||
239 | |||
240 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | ||
241 | key.type = BTRFS_DEV_ITEM_KEY; | ||
242 | key.offset = (u64)-1; | ||
243 | |||
244 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | ||
245 | if (ret < 0) | ||
246 | goto error; | ||
247 | |||
248 | BUG_ON(ret == 0); | ||
249 | |||
250 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, | ||
251 | BTRFS_DEV_ITEM_KEY); | ||
252 | if (ret) { | ||
253 | *objectid = 1; | ||
254 | } else { | ||
255 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | ||
256 | path->slots[0]); | ||
257 | *objectid = found_key.offset + 1; | ||
258 | } | ||
259 | ret = 0; | ||
260 | error: | ||
261 | btrfs_release_path(root, path); | ||
262 | return ret; | ||
263 | } | ||
264 | |||
265 | /* | ||
266 | * the device information is stored in the chunk root | ||
267 | * the btrfs_device struct should be fully filled in | ||
268 | */ | ||
269 | int btrfs_add_device(struct btrfs_trans_handle *trans, | ||
270 | struct btrfs_root *root, | ||
271 | struct btrfs_device *device) | ||
272 | { | ||
273 | int ret; | ||
274 | struct btrfs_path *path; | ||
275 | struct btrfs_dev_item *dev_item; | ||
276 | struct extent_buffer *leaf; | ||
277 | struct btrfs_key key; | ||
278 | unsigned long ptr; | ||
279 | u64 free_devid; | ||
280 | |||
281 | root = root->fs_info->chunk_root; | ||
282 | |||
283 | path = btrfs_alloc_path(); | ||
284 | if (!path) | ||
285 | return -ENOMEM; | ||
286 | |||
287 | ret = find_next_devid(root, path, &free_devid); | ||
288 | if (ret) | ||
289 | goto out; | ||
290 | |||
291 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | ||
292 | key.type = BTRFS_DEV_ITEM_KEY; | ||
293 | key.offset = free_devid; | ||
294 | |||
295 | ret = btrfs_insert_empty_item(trans, root, path, &key, | ||
296 | sizeof(*dev_item) + device->name_len); | ||
297 | if (ret) | ||
298 | goto out; | ||
299 | |||
300 | leaf = path->nodes[0]; | ||
301 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | ||
302 | |||
303 | btrfs_set_device_id(leaf, dev_item, device->devid); | ||
304 | btrfs_set_device_type(leaf, dev_item, device->type); | ||
305 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | ||
306 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | ||
307 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | ||
308 | btrfs_set_device_rdev(leaf, dev_item, device->rdev); | ||
309 | btrfs_set_device_partition(leaf, dev_item, device->partition); | ||
310 | btrfs_set_device_name_len(leaf, dev_item, device->name_len); | ||
311 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); | ||
312 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); | ||
313 | |||
314 | ptr = (unsigned long)btrfs_device_name(dev_item); | ||
315 | write_extent_buffer(leaf, device->name, ptr, device->name_len); | ||
316 | |||
317 | ptr = (unsigned long)btrfs_device_uuid(dev_item); | ||
318 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE); | ||
319 | btrfs_mark_buffer_dirty(leaf); | ||
320 | ret = 0; | ||
321 | |||
322 | out: | ||
323 | btrfs_free_path(path); | ||
324 | return ret; | ||
325 | } | ||
326 | int btrfs_update_device(struct btrfs_trans_handle *trans, | ||
327 | struct btrfs_device *device) | ||
328 | { | ||
329 | int ret; | ||
330 | struct btrfs_path *path; | ||
331 | struct btrfs_root *root; | ||
332 | struct btrfs_dev_item *dev_item; | ||
333 | struct extent_buffer *leaf; | ||
334 | struct btrfs_key key; | ||
335 | |||
336 | root = device->dev_root->fs_info->chunk_root; | ||
337 | |||
338 | path = btrfs_alloc_path(); | ||
339 | if (!path) | ||
340 | return -ENOMEM; | ||
341 | |||
342 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | ||
343 | key.type = BTRFS_DEV_ITEM_KEY; | ||
344 | key.offset = device->devid; | ||
345 | |||
346 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | ||
347 | if (ret < 0) | ||
348 | goto out; | ||
349 | |||
350 | if (ret > 0) { | ||
351 | ret = -ENOENT; | ||
352 | goto out; | ||
353 | } | ||
354 | |||
355 | leaf = path->nodes[0]; | ||
356 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | ||
357 | |||
358 | btrfs_set_device_id(leaf, dev_item, device->devid); | ||
359 | btrfs_set_device_type(leaf, dev_item, device->type); | ||
360 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | ||
361 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | ||
362 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | ||
363 | btrfs_set_device_rdev(leaf, dev_item, device->rdev); | ||
364 | btrfs_set_device_partition(leaf, dev_item, device->partition); | ||
365 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); | ||
366 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); | ||
367 | btrfs_mark_buffer_dirty(leaf); | ||
368 | |||
369 | out: | ||
370 | btrfs_free_path(path); | ||
371 | return ret; | ||
372 | } | ||
373 | |||
374 | int btrfs_add_system_chunk(struct btrfs_trans_handle *trans, | ||
375 | struct btrfs_root *root, | ||
376 | struct btrfs_key *key, | ||
377 | struct btrfs_chunk *chunk, int item_size) | ||
378 | { | ||
379 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | ||
380 | struct btrfs_disk_key disk_key; | ||
381 | u32 array_size; | ||
382 | u8 *ptr; | ||
383 | |||
384 | array_size = btrfs_super_sys_array_size(super_copy); | ||
385 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) | ||
386 | return -EFBIG; | ||
387 | |||
388 | ptr = super_copy->sys_chunk_array + array_size; | ||
389 | btrfs_cpu_key_to_disk(&disk_key, key); | ||
390 | memcpy(ptr, &disk_key, sizeof(disk_key)); | ||
391 | ptr += sizeof(disk_key); | ||
392 | memcpy(ptr, chunk, item_size); | ||
393 | item_size += sizeof(disk_key); | ||
394 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); | ||
395 | return 0; | ||
396 | } | ||
397 | |||
398 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, | ||
399 | struct btrfs_root *extent_root, u64 *start, | ||
400 | u64 *num_bytes, u32 type) | ||
401 | { | ||
402 | u64 dev_offset; | ||
403 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; | ||
404 | struct btrfs_stripe *stripes; | ||
405 | struct btrfs_device *device = NULL; | ||
406 | struct btrfs_chunk *chunk; | ||
407 | struct list_head *dev_list = &extent_root->fs_info->devices; | ||
408 | struct list_head *last_dev = extent_root->fs_info->last_device; | ||
409 | struct extent_map_tree *em_tree; | ||
410 | struct map_lookup *map; | ||
411 | struct extent_map *em; | ||
412 | u64 physical; | ||
413 | u64 calc_size = 1024 * 1024 * 1024; | ||
414 | int num_stripes; | ||
415 | int ret; | ||
416 | int index = 0; | ||
417 | struct btrfs_key key; | ||
418 | |||
419 | |||
420 | ret = find_next_chunk(chunk_root, &key.objectid); | ||
421 | if (ret) | ||
422 | return ret; | ||
423 | |||
424 | num_stripes = 1; | ||
425 | chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS); | ||
426 | if (!chunk) | ||
427 | return -ENOMEM; | ||
428 | |||
429 | stripes = &chunk->stripe; | ||
430 | |||
431 | *num_bytes = calc_size; | ||
432 | while(index < num_stripes) { | ||
433 | device = next_device(dev_list, last_dev); | ||
434 | BUG_ON(!device); | ||
435 | last_dev = &device->dev_list; | ||
436 | extent_root->fs_info->last_device = last_dev; | ||
437 | |||
438 | ret = btrfs_alloc_dev_extent(trans, device, | ||
439 | key.objectid, | ||
440 | calc_size, &dev_offset); | ||
441 | BUG_ON(ret); | ||
442 | |||
443 | device->bytes_used += calc_size; | ||
444 | ret = btrfs_update_device(trans, device); | ||
445 | BUG_ON(ret); | ||
446 | |||
447 | btrfs_set_stack_stripe_devid(stripes + index, device->devid); | ||
448 | btrfs_set_stack_stripe_offset(stripes + index, dev_offset); | ||
449 | physical = dev_offset; | ||
450 | index++; | ||
451 | } | ||
452 | |||
453 | /* key.objectid was set above */ | ||
454 | key.offset = *num_bytes; | ||
455 | key.type = BTRFS_CHUNK_ITEM_KEY; | ||
456 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); | ||
457 | btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024); | ||
458 | btrfs_set_stack_chunk_type(chunk, type); | ||
459 | btrfs_set_stack_chunk_num_stripes(chunk, num_stripes); | ||
460 | btrfs_set_stack_chunk_io_align(chunk, extent_root->sectorsize); | ||
461 | btrfs_set_stack_chunk_io_width(chunk, extent_root->sectorsize); | ||
462 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); | ||
463 | |||
464 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, | ||
465 | btrfs_chunk_item_size(num_stripes)); | ||
466 | BUG_ON(ret); | ||
467 | *start = key.objectid; | ||
468 | |||
469 | em = alloc_extent_map(GFP_NOFS); | ||
470 | if (!em) | ||
471 | return -ENOMEM; | ||
472 | map = kmalloc(sizeof(*map), GFP_NOFS); | ||
473 | if (!map) { | ||
474 | free_extent_map(em); | ||
475 | return -ENOMEM; | ||
476 | } | ||
477 | |||
478 | em->bdev = (struct block_device *)map; | ||
479 | em->start = key.objectid; | ||
480 | em->len = key.offset; | ||
481 | em->block_start = 0; | ||
482 | |||
483 | map->physical = physical; | ||
484 | map->dev = device; | ||
485 | |||
486 | if (!map->dev) { | ||
487 | kfree(map); | ||
488 | free_extent_map(em); | ||
489 | return -EIO; | ||
490 | } | ||
491 | kfree(chunk); | ||
492 | |||
493 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; | ||
494 | spin_lock(&em_tree->lock); | ||
495 | ret = add_extent_mapping(em_tree, em); | ||
496 | BUG_ON(ret); | ||
497 | spin_unlock(&em_tree->lock); | ||
498 | free_extent_map(em); | ||
499 | return ret; | ||
500 | } | ||
501 | |||
502 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) | ||
503 | { | ||
504 | extent_map_tree_init(&tree->map_tree, GFP_NOFS); | ||
505 | } | ||
506 | |||
507 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) | ||
508 | { | ||
509 | struct extent_map *em; | ||
510 | |||
511 | while(1) { | ||
512 | spin_lock(&tree->map_tree.lock); | ||
513 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); | ||
514 | if (em) | ||
515 | remove_extent_mapping(&tree->map_tree, em); | ||
516 | spin_unlock(&tree->map_tree.lock); | ||
517 | if (!em) | ||
518 | break; | ||
519 | kfree(em->bdev); | ||
520 | /* once for us */ | ||
521 | free_extent_map(em); | ||
522 | /* once for the tree */ | ||
523 | free_extent_map(em); | ||
524 | } | ||
525 | } | ||
526 | |||
527 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, | ||
528 | u64 logical, u64 *phys, u64 *length, | ||
529 | struct btrfs_device **dev) | ||
530 | { | ||
531 | struct extent_map *em; | ||
532 | struct map_lookup *map; | ||
533 | struct extent_map_tree *em_tree = &map_tree->map_tree; | ||
534 | u64 offset; | ||
535 | |||
536 | |||
537 | spin_lock(&em_tree->lock); | ||
538 | em = lookup_extent_mapping(em_tree, logical, *length); | ||
539 | BUG_ON(!em); | ||
540 | |||
541 | BUG_ON(em->start > logical || em->start + em->len < logical); | ||
542 | map = (struct map_lookup *)em->bdev; | ||
543 | offset = logical - em->start; | ||
544 | *phys = map->physical + offset; | ||
545 | *length = em->len - offset; | ||
546 | *dev = map->dev; | ||
547 | free_extent_map(em); | ||
548 | spin_unlock(&em_tree->lock); | ||
549 | return 0; | ||
550 | } | ||
551 | |||
552 | int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio) | ||
553 | { | ||
554 | struct btrfs_mapping_tree *map_tree; | ||
555 | struct btrfs_device *dev; | ||
556 | u64 logical = bio->bi_sector << 9; | ||
557 | u64 physical; | ||
558 | u64 length = 0; | ||
559 | u64 map_length; | ||
560 | struct bio_vec *bvec; | ||
561 | int i; | ||
562 | int ret; | ||
563 | |||
564 | bio_for_each_segment(bvec, bio, i) { | ||
565 | length += bvec->bv_len; | ||
566 | } | ||
567 | map_tree = &root->fs_info->mapping_tree; | ||
568 | map_length = length; | ||
569 | ret = btrfs_map_block(map_tree, logical, &physical, &map_length, &dev); | ||
570 | BUG_ON(map_length < length); | ||
571 | bio->bi_sector = physical >> 9; | ||
572 | bio->bi_bdev = dev->bdev; | ||
573 | submit_bio(rw, bio); | ||
574 | return 0; | ||
575 | } | ||
576 | |||
577 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid) | ||
578 | { | ||
579 | struct btrfs_device *dev; | ||
580 | struct list_head *cur = root->fs_info->devices.next; | ||
581 | struct list_head *head = &root->fs_info->devices; | ||
582 | |||
583 | while(cur != head) { | ||
584 | dev = list_entry(cur, struct btrfs_device, dev_list); | ||
585 | if (dev->devid == devid) | ||
586 | return dev; | ||
587 | cur = cur->next; | ||
588 | } | ||
589 | return NULL; | ||
590 | } | ||
591 | |||
592 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, | ||
593 | struct extent_buffer *leaf, | ||
594 | struct btrfs_chunk *chunk) | ||
595 | { | ||
596 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; | ||
597 | struct map_lookup *map; | ||
598 | struct extent_map *em; | ||
599 | u64 logical; | ||
600 | u64 length; | ||
601 | u64 devid; | ||
602 | int ret; | ||
603 | |||
604 | logical = key->objectid; | ||
605 | length = key->offset; | ||
606 | spin_lock(&map_tree->map_tree.lock); | ||
607 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); | ||
608 | |||
609 | /* already mapped? */ | ||
610 | if (em && em->start <= logical && em->start + em->len > logical) { | ||
611 | free_extent_map(em); | ||
612 | spin_unlock(&map_tree->map_tree.lock); | ||
613 | return 0; | ||
614 | } else if (em) { | ||
615 | free_extent_map(em); | ||
616 | } | ||
617 | spin_unlock(&map_tree->map_tree.lock); | ||
618 | |||
619 | map = kzalloc(sizeof(*map), GFP_NOFS); | ||
620 | if (!map) | ||
621 | return -ENOMEM; | ||
622 | |||
623 | em = alloc_extent_map(GFP_NOFS); | ||
624 | if (!em) | ||
625 | return -ENOMEM; | ||
626 | map = kmalloc(sizeof(*map), GFP_NOFS); | ||
627 | if (!map) { | ||
628 | free_extent_map(em); | ||
629 | return -ENOMEM; | ||
630 | } | ||
631 | |||
632 | em->bdev = (struct block_device *)map; | ||
633 | em->start = logical; | ||
634 | em->len = length; | ||
635 | em->block_start = 0; | ||
636 | |||
637 | map->physical = btrfs_stripe_offset_nr(leaf, chunk, 0); | ||
638 | devid = btrfs_stripe_devid_nr(leaf, chunk, 0); | ||
639 | map->dev = btrfs_find_device(root, devid); | ||
640 | if (!map->dev) { | ||
641 | kfree(map); | ||
642 | free_extent_map(em); | ||
643 | return -EIO; | ||
644 | } | ||
645 | |||
646 | spin_lock(&map_tree->map_tree.lock); | ||
647 | ret = add_extent_mapping(&map_tree->map_tree, em); | ||
648 | BUG_ON(ret); | ||
649 | spin_unlock(&map_tree->map_tree.lock); | ||
650 | free_extent_map(em); | ||
651 | |||
652 | return 0; | ||
653 | } | ||
654 | |||
655 | static int fill_device_from_item(struct extent_buffer *leaf, | ||
656 | struct btrfs_dev_item *dev_item, | ||
657 | struct btrfs_device *device) | ||
658 | { | ||
659 | unsigned long ptr; | ||
660 | char *name; | ||
661 | |||
662 | device->devid = btrfs_device_id(leaf, dev_item); | ||
663 | device->total_bytes = btrfs_device_total_bytes(leaf, dev_item); | ||
664 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); | ||
665 | device->type = btrfs_device_type(leaf, dev_item); | ||
666 | device->io_align = btrfs_device_io_align(leaf, dev_item); | ||
667 | device->io_width = btrfs_device_io_width(leaf, dev_item); | ||
668 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); | ||
669 | device->rdev = btrfs_device_rdev(leaf, dev_item); | ||
670 | device->partition = btrfs_device_partition(leaf, dev_item); | ||
671 | device->name_len = btrfs_device_name_len(leaf, dev_item); | ||
672 | |||
673 | ptr = (unsigned long)btrfs_device_uuid(dev_item); | ||
674 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE); | ||
675 | |||
676 | name = kmalloc(device->name_len + 1, GFP_NOFS); | ||
677 | if (!name) | ||
678 | return -ENOMEM; | ||
679 | device->name = name; | ||
680 | ptr = (unsigned long)btrfs_device_name(dev_item); | ||
681 | read_extent_buffer(leaf, name, ptr, device->name_len); | ||
682 | name[device->name_len] = '\0'; | ||
683 | return 0; | ||
684 | } | ||
685 | |||
686 | static int read_one_dev(struct btrfs_root *root, struct btrfs_key *key, | ||
687 | struct extent_buffer *leaf, | ||
688 | struct btrfs_dev_item *dev_item) | ||
689 | { | ||
690 | struct btrfs_device *device; | ||
691 | u64 devid; | ||
692 | int ret; | ||
693 | |||
694 | devid = btrfs_device_id(leaf, dev_item); | ||
695 | if (btrfs_find_device(root, devid)) | ||
696 | return 0; | ||
697 | |||
698 | device = kmalloc(sizeof(*device), GFP_NOFS); | ||
699 | if (!device) | ||
700 | return -ENOMEM; | ||
701 | |||
702 | fill_device_from_item(leaf, dev_item, device); | ||
703 | device->dev_root = root->fs_info->dev_root; | ||
704 | device->bdev = root->fs_info->sb->s_bdev; | ||
705 | list_add(&device->dev_list, &root->fs_info->devices); | ||
706 | memcpy(&device->dev_key, key, sizeof(*key)); | ||
707 | ret = 0; | ||
708 | #if 0 | ||
709 | ret = btrfs_open_device(device); | ||
710 | if (ret) { | ||
711 | kfree(device); | ||
712 | } | ||
713 | #endif | ||
714 | return ret; | ||
715 | } | ||
716 | |||
717 | int btrfs_read_sys_array(struct btrfs_root *root) | ||
718 | { | ||
719 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | ||
720 | struct extent_buffer *sb = root->fs_info->sb_buffer; | ||
721 | struct btrfs_disk_key *disk_key; | ||
722 | struct btrfs_dev_item *dev_item; | ||
723 | struct btrfs_chunk *chunk; | ||
724 | struct btrfs_key key; | ||
725 | u32 num_stripes; | ||
726 | u32 array_size; | ||
727 | u32 len = 0; | ||
728 | u8 *ptr; | ||
729 | unsigned long sb_ptr; | ||
730 | u32 cur; | ||
731 | int ret; | ||
732 | int dev_only = 1; | ||
733 | |||
734 | array_size = btrfs_super_sys_array_size(super_copy); | ||
735 | |||
736 | /* | ||
737 | * we do this loop twice, once for the device items and | ||
738 | * once for all of the chunks. This way there are device | ||
739 | * structs filled in for every chunk | ||
740 | */ | ||
741 | again: | ||
742 | ptr = super_copy->sys_chunk_array; | ||
743 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); | ||
744 | cur = 0; | ||
745 | |||
746 | while (cur < array_size) { | ||
747 | disk_key = (struct btrfs_disk_key *)ptr; | ||
748 | btrfs_disk_key_to_cpu(&key, disk_key); | ||
749 | |||
750 | len = sizeof(*disk_key); | ||
751 | ptr += len; | ||
752 | sb_ptr += len; | ||
753 | cur += len; | ||
754 | |||
755 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID && | ||
756 | key.type == BTRFS_DEV_ITEM_KEY) { | ||
757 | dev_item = (struct btrfs_dev_item *)sb_ptr; | ||
758 | if (dev_only) { | ||
759 | ret = read_one_dev(root, &key, sb, dev_item); | ||
760 | BUG_ON(ret); | ||
761 | } | ||
762 | len = sizeof(*dev_item); | ||
763 | len += btrfs_device_name_len(sb, dev_item); | ||
764 | } else if (key.type == BTRFS_CHUNK_ITEM_KEY) { | ||
765 | |||
766 | chunk = (struct btrfs_chunk *)sb_ptr; | ||
767 | if (!dev_only) { | ||
768 | ret = read_one_chunk(root, &key, sb, chunk); | ||
769 | BUG_ON(ret); | ||
770 | } | ||
771 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); | ||
772 | len = btrfs_chunk_item_size(num_stripes); | ||
773 | } else { | ||
774 | BUG(); | ||
775 | } | ||
776 | ptr += len; | ||
777 | sb_ptr += len; | ||
778 | cur += len; | ||
779 | } | ||
780 | if (dev_only == 1) { | ||
781 | dev_only = 0; | ||
782 | goto again; | ||
783 | } | ||
784 | return 0; | ||
785 | } | ||
786 | |||
787 | int btrfs_read_chunk_tree(struct btrfs_root *root) | ||
788 | { | ||
789 | struct btrfs_path *path; | ||
790 | struct extent_buffer *leaf; | ||
791 | struct btrfs_key key; | ||
792 | struct btrfs_key found_key; | ||
793 | int ret; | ||
794 | int slot; | ||
795 | |||
796 | root = root->fs_info->chunk_root; | ||
797 | |||
798 | path = btrfs_alloc_path(); | ||
799 | if (!path) | ||
800 | return -ENOMEM; | ||
801 | |||
802 | /* first we search for all of the device items, and then we | ||
803 | * read in all of the chunk items. This way we can create chunk | ||
804 | * mappings that reference all of the devices that are afound | ||
805 | */ | ||
806 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | ||
807 | key.offset = 0; | ||
808 | key.type = 0; | ||
809 | again: | ||
810 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | ||
811 | while(1) { | ||
812 | leaf = path->nodes[0]; | ||
813 | slot = path->slots[0]; | ||
814 | if (slot >= btrfs_header_nritems(leaf)) { | ||
815 | ret = btrfs_next_leaf(root, path); | ||
816 | if (ret == 0) | ||
817 | continue; | ||
818 | if (ret < 0) | ||
819 | goto error; | ||
820 | break; | ||
821 | } | ||
822 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | ||
823 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | ||
824 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) | ||
825 | break; | ||
826 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { | ||
827 | struct btrfs_dev_item *dev_item; | ||
828 | dev_item = btrfs_item_ptr(leaf, slot, | ||
829 | struct btrfs_dev_item); | ||
830 | ret = read_one_dev(root, &found_key, leaf, | ||
831 | dev_item); | ||
832 | BUG_ON(ret); | ||
833 | } | ||
834 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { | ||
835 | struct btrfs_chunk *chunk; | ||
836 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); | ||
837 | ret = read_one_chunk(root, &found_key, leaf, chunk); | ||
838 | } | ||
839 | path->slots[0]++; | ||
840 | } | ||
841 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | ||
842 | key.objectid = 0; | ||
843 | btrfs_release_path(root, path); | ||
844 | goto again; | ||
845 | } | ||
846 | |||
847 | btrfs_free_path(path); | ||
848 | ret = 0; | ||
849 | error: | ||
850 | return ret; | ||
851 | } | ||
852 | |||