]>
Commit | Line | Data |
---|---|---|
0b86a832 CM |
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> | |
8a4b83cc | 20 | #include <linux/buffer_head.h> |
593060d7 | 21 | #include <asm/div64.h> |
0b86a832 CM |
22 | #include "ctree.h" |
23 | #include "extent_map.h" | |
24 | #include "disk-io.h" | |
25 | #include "transaction.h" | |
26 | #include "print-tree.h" | |
27 | #include "volumes.h" | |
28 | ||
593060d7 CM |
29 | struct map_lookup { |
30 | u64 type; | |
31 | int io_align; | |
32 | int io_width; | |
33 | int stripe_len; | |
34 | int sector_size; | |
35 | int num_stripes; | |
321aecc6 | 36 | int sub_stripes; |
cea9e445 | 37 | struct btrfs_bio_stripe stripes[]; |
593060d7 CM |
38 | }; |
39 | ||
40 | #define map_lookup_size(n) (sizeof(struct map_lookup) + \ | |
cea9e445 | 41 | (sizeof(struct btrfs_bio_stripe) * (n))) |
593060d7 | 42 | |
8a4b83cc CM |
43 | static DEFINE_MUTEX(uuid_mutex); |
44 | static LIST_HEAD(fs_uuids); | |
45 | ||
46 | int btrfs_cleanup_fs_uuids(void) | |
47 | { | |
48 | struct btrfs_fs_devices *fs_devices; | |
49 | struct list_head *uuid_cur; | |
50 | struct list_head *devices_cur; | |
51 | struct btrfs_device *dev; | |
52 | ||
53 | list_for_each(uuid_cur, &fs_uuids) { | |
54 | fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices, | |
55 | list); | |
56 | while(!list_empty(&fs_devices->devices)) { | |
57 | devices_cur = fs_devices->devices.next; | |
58 | dev = list_entry(devices_cur, struct btrfs_device, | |
59 | dev_list); | |
60 | printk("uuid cleanup finds %s\n", dev->name); | |
61 | if (dev->bdev) { | |
62 | printk("closing\n"); | |
63 | close_bdev_excl(dev->bdev); | |
64 | } | |
65 | list_del(&dev->dev_list); | |
66 | kfree(dev); | |
67 | } | |
68 | } | |
69 | return 0; | |
70 | } | |
71 | ||
72 | static struct btrfs_device *__find_device(struct list_head *head, u64 devid) | |
73 | { | |
74 | struct btrfs_device *dev; | |
75 | struct list_head *cur; | |
76 | ||
77 | list_for_each(cur, head) { | |
78 | dev = list_entry(cur, struct btrfs_device, dev_list); | |
79 | if (dev->devid == devid) | |
80 | return dev; | |
81 | } | |
82 | return NULL; | |
83 | } | |
84 | ||
85 | static struct btrfs_fs_devices *find_fsid(u8 *fsid) | |
86 | { | |
87 | struct list_head *cur; | |
88 | struct btrfs_fs_devices *fs_devices; | |
89 | ||
90 | list_for_each(cur, &fs_uuids) { | |
91 | fs_devices = list_entry(cur, struct btrfs_fs_devices, list); | |
92 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) | |
93 | return fs_devices; | |
94 | } | |
95 | return NULL; | |
96 | } | |
97 | ||
98 | static int device_list_add(const char *path, | |
99 | struct btrfs_super_block *disk_super, | |
100 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) | |
101 | { | |
102 | struct btrfs_device *device; | |
103 | struct btrfs_fs_devices *fs_devices; | |
104 | u64 found_transid = btrfs_super_generation(disk_super); | |
105 | ||
106 | fs_devices = find_fsid(disk_super->fsid); | |
107 | if (!fs_devices) { | |
108 | fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS); | |
109 | if (!fs_devices) | |
110 | return -ENOMEM; | |
111 | INIT_LIST_HEAD(&fs_devices->devices); | |
112 | list_add(&fs_devices->list, &fs_uuids); | |
113 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); | |
114 | fs_devices->latest_devid = devid; | |
115 | fs_devices->latest_trans = found_transid; | |
116 | fs_devices->lowest_devid = (u64)-1; | |
117 | fs_devices->num_devices = 0; | |
118 | device = NULL; | |
119 | } else { | |
120 | device = __find_device(&fs_devices->devices, devid); | |
121 | } | |
122 | if (!device) { | |
123 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
124 | if (!device) { | |
125 | /* we can safely leave the fs_devices entry around */ | |
126 | return -ENOMEM; | |
127 | } | |
128 | device->devid = devid; | |
f2984462 | 129 | device->barriers = 1; |
b248a415 | 130 | spin_lock_init(&device->io_lock); |
8a4b83cc CM |
131 | device->name = kstrdup(path, GFP_NOFS); |
132 | if (!device->name) { | |
133 | kfree(device); | |
134 | return -ENOMEM; | |
135 | } | |
136 | list_add(&device->dev_list, &fs_devices->devices); | |
137 | fs_devices->num_devices++; | |
138 | } | |
139 | ||
140 | if (found_transid > fs_devices->latest_trans) { | |
141 | fs_devices->latest_devid = devid; | |
142 | fs_devices->latest_trans = found_transid; | |
143 | } | |
144 | if (fs_devices->lowest_devid > devid) { | |
145 | fs_devices->lowest_devid = devid; | |
146 | printk("lowest devid now %Lu\n", devid); | |
147 | } | |
148 | *fs_devices_ret = fs_devices; | |
149 | return 0; | |
150 | } | |
151 | ||
152 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) | |
153 | { | |
154 | struct list_head *head = &fs_devices->devices; | |
155 | struct list_head *cur; | |
156 | struct btrfs_device *device; | |
157 | ||
158 | mutex_lock(&uuid_mutex); | |
159 | list_for_each(cur, head) { | |
160 | device = list_entry(cur, struct btrfs_device, dev_list); | |
161 | if (device->bdev) { | |
162 | close_bdev_excl(device->bdev); | |
163 | printk("close devices closes %s\n", device->name); | |
164 | } | |
165 | device->bdev = NULL; | |
166 | } | |
167 | mutex_unlock(&uuid_mutex); | |
168 | return 0; | |
169 | } | |
170 | ||
171 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, | |
172 | int flags, void *holder) | |
173 | { | |
174 | struct block_device *bdev; | |
175 | struct list_head *head = &fs_devices->devices; | |
176 | struct list_head *cur; | |
177 | struct btrfs_device *device; | |
178 | int ret; | |
179 | ||
180 | mutex_lock(&uuid_mutex); | |
181 | list_for_each(cur, head) { | |
182 | device = list_entry(cur, struct btrfs_device, dev_list); | |
183 | bdev = open_bdev_excl(device->name, flags, holder); | |
e17cade2 | 184 | |
8a4b83cc CM |
185 | if (IS_ERR(bdev)) { |
186 | printk("open %s failed\n", device->name); | |
187 | ret = PTR_ERR(bdev); | |
188 | goto fail; | |
189 | } | |
190 | if (device->devid == fs_devices->latest_devid) | |
191 | fs_devices->latest_bdev = bdev; | |
192 | if (device->devid == fs_devices->lowest_devid) { | |
193 | fs_devices->lowest_bdev = bdev; | |
8a4b83cc CM |
194 | } |
195 | device->bdev = bdev; | |
196 | } | |
197 | mutex_unlock(&uuid_mutex); | |
198 | return 0; | |
199 | fail: | |
200 | mutex_unlock(&uuid_mutex); | |
201 | btrfs_close_devices(fs_devices); | |
202 | return ret; | |
203 | } | |
204 | ||
205 | int btrfs_scan_one_device(const char *path, int flags, void *holder, | |
206 | struct btrfs_fs_devices **fs_devices_ret) | |
207 | { | |
208 | struct btrfs_super_block *disk_super; | |
209 | struct block_device *bdev; | |
210 | struct buffer_head *bh; | |
211 | int ret; | |
212 | u64 devid; | |
f2984462 | 213 | u64 transid; |
8a4b83cc CM |
214 | |
215 | mutex_lock(&uuid_mutex); | |
216 | ||
217 | printk("scan one opens %s\n", path); | |
218 | bdev = open_bdev_excl(path, flags, holder); | |
219 | ||
220 | if (IS_ERR(bdev)) { | |
221 | printk("open failed\n"); | |
222 | ret = PTR_ERR(bdev); | |
223 | goto error; | |
224 | } | |
225 | ||
226 | ret = set_blocksize(bdev, 4096); | |
227 | if (ret) | |
228 | goto error_close; | |
229 | bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096); | |
230 | if (!bh) { | |
231 | ret = -EIO; | |
232 | goto error_close; | |
233 | } | |
234 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
235 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, | |
236 | sizeof(disk_super->magic))) { | |
237 | printk("no btrfs found on %s\n", path); | |
e58ca020 | 238 | ret = -EINVAL; |
8a4b83cc CM |
239 | goto error_brelse; |
240 | } | |
241 | devid = le64_to_cpu(disk_super->dev_item.devid); | |
f2984462 CM |
242 | transid = btrfs_super_generation(disk_super); |
243 | printk("found device %Lu transid %Lu on %s\n", devid, transid, path); | |
8a4b83cc CM |
244 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
245 | ||
246 | error_brelse: | |
247 | brelse(bh); | |
248 | error_close: | |
249 | close_bdev_excl(bdev); | |
8a4b83cc CM |
250 | error: |
251 | mutex_unlock(&uuid_mutex); | |
252 | return ret; | |
253 | } | |
0b86a832 CM |
254 | |
255 | /* | |
256 | * this uses a pretty simple search, the expectation is that it is | |
257 | * called very infrequently and that a given device has a small number | |
258 | * of extents | |
259 | */ | |
260 | static int find_free_dev_extent(struct btrfs_trans_handle *trans, | |
261 | struct btrfs_device *device, | |
262 | struct btrfs_path *path, | |
263 | u64 num_bytes, u64 *start) | |
264 | { | |
265 | struct btrfs_key key; | |
266 | struct btrfs_root *root = device->dev_root; | |
267 | struct btrfs_dev_extent *dev_extent = NULL; | |
268 | u64 hole_size = 0; | |
269 | u64 last_byte = 0; | |
270 | u64 search_start = 0; | |
271 | u64 search_end = device->total_bytes; | |
272 | int ret; | |
273 | int slot = 0; | |
274 | int start_found; | |
275 | struct extent_buffer *l; | |
276 | ||
277 | start_found = 0; | |
278 | path->reada = 2; | |
279 | ||
280 | /* FIXME use last free of some kind */ | |
281 | ||
8a4b83cc CM |
282 | /* we don't want to overwrite the superblock on the drive, |
283 | * so we make sure to start at an offset of at least 1MB | |
284 | */ | |
285 | search_start = max((u64)1024 * 1024, search_start); | |
0b86a832 CM |
286 | key.objectid = device->devid; |
287 | key.offset = search_start; | |
288 | key.type = BTRFS_DEV_EXTENT_KEY; | |
289 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); | |
290 | if (ret < 0) | |
291 | goto error; | |
292 | ret = btrfs_previous_item(root, path, 0, key.type); | |
293 | if (ret < 0) | |
294 | goto error; | |
295 | l = path->nodes[0]; | |
296 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
297 | while (1) { | |
298 | l = path->nodes[0]; | |
299 | slot = path->slots[0]; | |
300 | if (slot >= btrfs_header_nritems(l)) { | |
301 | ret = btrfs_next_leaf(root, path); | |
302 | if (ret == 0) | |
303 | continue; | |
304 | if (ret < 0) | |
305 | goto error; | |
306 | no_more_items: | |
307 | if (!start_found) { | |
308 | if (search_start >= search_end) { | |
309 | ret = -ENOSPC; | |
310 | goto error; | |
311 | } | |
312 | *start = search_start; | |
313 | start_found = 1; | |
314 | goto check_pending; | |
315 | } | |
316 | *start = last_byte > search_start ? | |
317 | last_byte : search_start; | |
318 | if (search_end <= *start) { | |
319 | ret = -ENOSPC; | |
320 | goto error; | |
321 | } | |
322 | goto check_pending; | |
323 | } | |
324 | btrfs_item_key_to_cpu(l, &key, slot); | |
325 | ||
326 | if (key.objectid < device->devid) | |
327 | goto next; | |
328 | ||
329 | if (key.objectid > device->devid) | |
330 | goto no_more_items; | |
331 | ||
332 | if (key.offset >= search_start && key.offset > last_byte && | |
333 | start_found) { | |
334 | if (last_byte < search_start) | |
335 | last_byte = search_start; | |
336 | hole_size = key.offset - last_byte; | |
337 | if (key.offset > last_byte && | |
338 | hole_size >= num_bytes) { | |
339 | *start = last_byte; | |
340 | goto check_pending; | |
341 | } | |
342 | } | |
343 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) { | |
344 | goto next; | |
345 | } | |
346 | ||
347 | start_found = 1; | |
348 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
349 | last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent); | |
350 | next: | |
351 | path->slots[0]++; | |
352 | cond_resched(); | |
353 | } | |
354 | check_pending: | |
355 | /* we have to make sure we didn't find an extent that has already | |
356 | * been allocated by the map tree or the original allocation | |
357 | */ | |
358 | btrfs_release_path(root, path); | |
359 | BUG_ON(*start < search_start); | |
360 | ||
6324fbf3 | 361 | if (*start + num_bytes > search_end) { |
0b86a832 CM |
362 | ret = -ENOSPC; |
363 | goto error; | |
364 | } | |
365 | /* check for pending inserts here */ | |
366 | return 0; | |
367 | ||
368 | error: | |
369 | btrfs_release_path(root, path); | |
370 | return ret; | |
371 | } | |
372 | ||
373 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, | |
374 | struct btrfs_device *device, | |
e17cade2 CM |
375 | u64 chunk_tree, u64 chunk_objectid, |
376 | u64 chunk_offset, | |
377 | u64 num_bytes, u64 *start) | |
0b86a832 CM |
378 | { |
379 | int ret; | |
380 | struct btrfs_path *path; | |
381 | struct btrfs_root *root = device->dev_root; | |
382 | struct btrfs_dev_extent *extent; | |
383 | struct extent_buffer *leaf; | |
384 | struct btrfs_key key; | |
385 | ||
386 | path = btrfs_alloc_path(); | |
387 | if (!path) | |
388 | return -ENOMEM; | |
389 | ||
390 | ret = find_free_dev_extent(trans, device, path, num_bytes, start); | |
6324fbf3 | 391 | if (ret) { |
0b86a832 | 392 | goto err; |
6324fbf3 | 393 | } |
0b86a832 CM |
394 | |
395 | key.objectid = device->devid; | |
396 | key.offset = *start; | |
397 | key.type = BTRFS_DEV_EXTENT_KEY; | |
398 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
399 | sizeof(*extent)); | |
400 | BUG_ON(ret); | |
401 | ||
402 | leaf = path->nodes[0]; | |
403 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
404 | struct btrfs_dev_extent); | |
e17cade2 CM |
405 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
406 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); | |
407 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); | |
408 | ||
409 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, | |
410 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), | |
411 | BTRFS_UUID_SIZE); | |
412 | ||
0b86a832 CM |
413 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
414 | btrfs_mark_buffer_dirty(leaf); | |
415 | err: | |
416 | btrfs_free_path(path); | |
417 | return ret; | |
418 | } | |
419 | ||
e17cade2 | 420 | static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset) |
0b86a832 CM |
421 | { |
422 | struct btrfs_path *path; | |
423 | int ret; | |
424 | struct btrfs_key key; | |
e17cade2 | 425 | struct btrfs_chunk *chunk; |
0b86a832 CM |
426 | struct btrfs_key found_key; |
427 | ||
428 | path = btrfs_alloc_path(); | |
429 | BUG_ON(!path); | |
430 | ||
e17cade2 | 431 | key.objectid = objectid; |
0b86a832 CM |
432 | key.offset = (u64)-1; |
433 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
434 | ||
435 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
436 | if (ret < 0) | |
437 | goto error; | |
438 | ||
439 | BUG_ON(ret == 0); | |
440 | ||
441 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); | |
442 | if (ret) { | |
e17cade2 | 443 | *offset = 0; |
0b86a832 CM |
444 | } else { |
445 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
446 | path->slots[0]); | |
e17cade2 CM |
447 | if (found_key.objectid != objectid) |
448 | *offset = 0; | |
449 | else { | |
450 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
451 | struct btrfs_chunk); | |
452 | *offset = found_key.offset + | |
453 | btrfs_chunk_length(path->nodes[0], chunk); | |
454 | } | |
0b86a832 CM |
455 | } |
456 | ret = 0; | |
457 | error: | |
458 | btrfs_free_path(path); | |
459 | return ret; | |
460 | } | |
461 | ||
0b86a832 CM |
462 | static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path, |
463 | u64 *objectid) | |
464 | { | |
465 | int ret; | |
466 | struct btrfs_key key; | |
467 | struct btrfs_key found_key; | |
468 | ||
469 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
470 | key.type = BTRFS_DEV_ITEM_KEY; | |
471 | key.offset = (u64)-1; | |
472 | ||
473 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
474 | if (ret < 0) | |
475 | goto error; | |
476 | ||
477 | BUG_ON(ret == 0); | |
478 | ||
479 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, | |
480 | BTRFS_DEV_ITEM_KEY); | |
481 | if (ret) { | |
482 | *objectid = 1; | |
483 | } else { | |
484 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
485 | path->slots[0]); | |
486 | *objectid = found_key.offset + 1; | |
487 | } | |
488 | ret = 0; | |
489 | error: | |
490 | btrfs_release_path(root, path); | |
491 | return ret; | |
492 | } | |
493 | ||
494 | /* | |
495 | * the device information is stored in the chunk root | |
496 | * the btrfs_device struct should be fully filled in | |
497 | */ | |
498 | int btrfs_add_device(struct btrfs_trans_handle *trans, | |
499 | struct btrfs_root *root, | |
500 | struct btrfs_device *device) | |
501 | { | |
502 | int ret; | |
503 | struct btrfs_path *path; | |
504 | struct btrfs_dev_item *dev_item; | |
505 | struct extent_buffer *leaf; | |
506 | struct btrfs_key key; | |
507 | unsigned long ptr; | |
508 | u64 free_devid; | |
509 | ||
510 | root = root->fs_info->chunk_root; | |
511 | ||
512 | path = btrfs_alloc_path(); | |
513 | if (!path) | |
514 | return -ENOMEM; | |
515 | ||
516 | ret = find_next_devid(root, path, &free_devid); | |
517 | if (ret) | |
518 | goto out; | |
519 | ||
520 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
521 | key.type = BTRFS_DEV_ITEM_KEY; | |
522 | key.offset = free_devid; | |
523 | ||
524 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
0d81ba5d | 525 | sizeof(*dev_item)); |
0b86a832 CM |
526 | if (ret) |
527 | goto out; | |
528 | ||
529 | leaf = path->nodes[0]; | |
530 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
531 | ||
8a4b83cc | 532 | device->devid = free_devid; |
0b86a832 CM |
533 | btrfs_set_device_id(leaf, dev_item, device->devid); |
534 | btrfs_set_device_type(leaf, dev_item, device->type); | |
535 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
536 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
537 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
0b86a832 CM |
538 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
539 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); | |
e17cade2 CM |
540 | btrfs_set_device_group(leaf, dev_item, 0); |
541 | btrfs_set_device_seek_speed(leaf, dev_item, 0); | |
542 | btrfs_set_device_bandwidth(leaf, dev_item, 0); | |
0b86a832 | 543 | |
0b86a832 | 544 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
e17cade2 | 545 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
0b86a832 CM |
546 | btrfs_mark_buffer_dirty(leaf); |
547 | ret = 0; | |
548 | ||
549 | out: | |
550 | btrfs_free_path(path); | |
551 | return ret; | |
552 | } | |
553 | int btrfs_update_device(struct btrfs_trans_handle *trans, | |
554 | struct btrfs_device *device) | |
555 | { | |
556 | int ret; | |
557 | struct btrfs_path *path; | |
558 | struct btrfs_root *root; | |
559 | struct btrfs_dev_item *dev_item; | |
560 | struct extent_buffer *leaf; | |
561 | struct btrfs_key key; | |
562 | ||
563 | root = device->dev_root->fs_info->chunk_root; | |
564 | ||
565 | path = btrfs_alloc_path(); | |
566 | if (!path) | |
567 | return -ENOMEM; | |
568 | ||
569 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
570 | key.type = BTRFS_DEV_ITEM_KEY; | |
571 | key.offset = device->devid; | |
572 | ||
573 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
574 | if (ret < 0) | |
575 | goto out; | |
576 | ||
577 | if (ret > 0) { | |
578 | ret = -ENOENT; | |
579 | goto out; | |
580 | } | |
581 | ||
582 | leaf = path->nodes[0]; | |
583 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
584 | ||
585 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
586 | btrfs_set_device_type(leaf, dev_item, device->type); | |
587 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
588 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
589 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
0b86a832 CM |
590 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
591 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); | |
592 | btrfs_mark_buffer_dirty(leaf); | |
593 | ||
594 | out: | |
595 | btrfs_free_path(path); | |
596 | return ret; | |
597 | } | |
598 | ||
599 | int btrfs_add_system_chunk(struct btrfs_trans_handle *trans, | |
600 | struct btrfs_root *root, | |
601 | struct btrfs_key *key, | |
602 | struct btrfs_chunk *chunk, int item_size) | |
603 | { | |
604 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
605 | struct btrfs_disk_key disk_key; | |
606 | u32 array_size; | |
607 | u8 *ptr; | |
608 | ||
609 | array_size = btrfs_super_sys_array_size(super_copy); | |
610 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) | |
611 | return -EFBIG; | |
612 | ||
613 | ptr = super_copy->sys_chunk_array + array_size; | |
614 | btrfs_cpu_key_to_disk(&disk_key, key); | |
615 | memcpy(ptr, &disk_key, sizeof(disk_key)); | |
616 | ptr += sizeof(disk_key); | |
617 | memcpy(ptr, chunk, item_size); | |
618 | item_size += sizeof(disk_key); | |
619 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); | |
620 | return 0; | |
621 | } | |
622 | ||
623 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, | |
624 | struct btrfs_root *extent_root, u64 *start, | |
6324fbf3 | 625 | u64 *num_bytes, u64 type) |
0b86a832 CM |
626 | { |
627 | u64 dev_offset; | |
593060d7 | 628 | struct btrfs_fs_info *info = extent_root->fs_info; |
0b86a832 CM |
629 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
630 | struct btrfs_stripe *stripes; | |
631 | struct btrfs_device *device = NULL; | |
632 | struct btrfs_chunk *chunk; | |
6324fbf3 | 633 | struct list_head private_devs; |
8a4b83cc | 634 | struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices; |
6324fbf3 | 635 | struct list_head *cur; |
0b86a832 CM |
636 | struct extent_map_tree *em_tree; |
637 | struct map_lookup *map; | |
638 | struct extent_map *em; | |
639 | u64 physical; | |
640 | u64 calc_size = 1024 * 1024 * 1024; | |
611f0e00 | 641 | u64 min_free = calc_size; |
6324fbf3 CM |
642 | u64 avail; |
643 | u64 max_avail = 0; | |
644 | int num_stripes = 1; | |
321aecc6 | 645 | int sub_stripes = 0; |
6324fbf3 | 646 | int looped = 0; |
0b86a832 | 647 | int ret; |
6324fbf3 | 648 | int index; |
593060d7 | 649 | int stripe_len = 64 * 1024; |
0b86a832 CM |
650 | struct btrfs_key key; |
651 | ||
6324fbf3 CM |
652 | if (list_empty(dev_list)) |
653 | return -ENOSPC; | |
593060d7 | 654 | |
8790d502 | 655 | if (type & (BTRFS_BLOCK_GROUP_RAID0)) |
593060d7 | 656 | num_stripes = btrfs_super_num_devices(&info->super_copy); |
611f0e00 CM |
657 | if (type & (BTRFS_BLOCK_GROUP_DUP)) |
658 | num_stripes = 2; | |
8790d502 CM |
659 | if (type & (BTRFS_BLOCK_GROUP_RAID1)) { |
660 | num_stripes = min_t(u64, 2, | |
661 | btrfs_super_num_devices(&info->super_copy)); | |
662 | } | |
321aecc6 CM |
663 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
664 | num_stripes = btrfs_super_num_devices(&info->super_copy); | |
665 | if (num_stripes < 4) | |
666 | return -ENOSPC; | |
667 | num_stripes &= ~(u32)1; | |
668 | sub_stripes = 2; | |
669 | } | |
6324fbf3 CM |
670 | again: |
671 | INIT_LIST_HEAD(&private_devs); | |
672 | cur = dev_list->next; | |
673 | index = 0; | |
611f0e00 CM |
674 | |
675 | if (type & BTRFS_BLOCK_GROUP_DUP) | |
676 | min_free = calc_size * 2; | |
677 | ||
6324fbf3 CM |
678 | /* build a private list of devices we will allocate from */ |
679 | while(index < num_stripes) { | |
680 | device = list_entry(cur, struct btrfs_device, dev_list); | |
611f0e00 | 681 | |
6324fbf3 CM |
682 | avail = device->total_bytes - device->bytes_used; |
683 | cur = cur->next; | |
684 | if (avail > max_avail) | |
685 | max_avail = avail; | |
611f0e00 | 686 | if (avail >= min_free) { |
6324fbf3 CM |
687 | list_move_tail(&device->dev_list, &private_devs); |
688 | index++; | |
611f0e00 CM |
689 | if (type & BTRFS_BLOCK_GROUP_DUP) |
690 | index++; | |
6324fbf3 CM |
691 | } |
692 | if (cur == dev_list) | |
693 | break; | |
694 | } | |
695 | if (index < num_stripes) { | |
696 | list_splice(&private_devs, dev_list); | |
697 | if (!looped && max_avail > 0) { | |
698 | looped = 1; | |
699 | calc_size = max_avail; | |
700 | goto again; | |
701 | } | |
702 | return -ENOSPC; | |
703 | } | |
0b86a832 | 704 | |
e17cade2 CM |
705 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
706 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
707 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
708 | &key.offset); | |
0b86a832 CM |
709 | if (ret) |
710 | return ret; | |
711 | ||
0b86a832 CM |
712 | chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS); |
713 | if (!chunk) | |
714 | return -ENOMEM; | |
715 | ||
593060d7 CM |
716 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
717 | if (!map) { | |
718 | kfree(chunk); | |
719 | return -ENOMEM; | |
720 | } | |
721 | ||
0b86a832 CM |
722 | stripes = &chunk->stripe; |
723 | ||
611f0e00 | 724 | if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP)) |
8790d502 | 725 | *num_bytes = calc_size; |
321aecc6 CM |
726 | else if (type & BTRFS_BLOCK_GROUP_RAID10) |
727 | *num_bytes = calc_size * num_stripes / sub_stripes; | |
8790d502 CM |
728 | else |
729 | *num_bytes = calc_size * num_stripes; | |
730 | ||
6324fbf3 | 731 | index = 0; |
e17cade2 | 732 | printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes); |
0b86a832 | 733 | while(index < num_stripes) { |
e17cade2 | 734 | struct btrfs_stripe *stripe; |
6324fbf3 CM |
735 | BUG_ON(list_empty(&private_devs)); |
736 | cur = private_devs.next; | |
737 | device = list_entry(cur, struct btrfs_device, dev_list); | |
611f0e00 CM |
738 | |
739 | /* loop over this device again if we're doing a dup group */ | |
740 | if (!(type & BTRFS_BLOCK_GROUP_DUP) || | |
741 | (index == num_stripes - 1)) | |
742 | list_move_tail(&device->dev_list, dev_list); | |
0b86a832 CM |
743 | |
744 | ret = btrfs_alloc_dev_extent(trans, device, | |
e17cade2 CM |
745 | info->chunk_root->root_key.objectid, |
746 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset, | |
747 | calc_size, &dev_offset); | |
0b86a832 | 748 | BUG_ON(ret); |
e17cade2 | 749 | printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.offset, calc_size, device->devid, type); |
0b86a832 CM |
750 | device->bytes_used += calc_size; |
751 | ret = btrfs_update_device(trans, device); | |
752 | BUG_ON(ret); | |
753 | ||
593060d7 CM |
754 | map->stripes[index].dev = device; |
755 | map->stripes[index].physical = dev_offset; | |
e17cade2 CM |
756 | stripe = stripes + index; |
757 | btrfs_set_stack_stripe_devid(stripe, device->devid); | |
758 | btrfs_set_stack_stripe_offset(stripe, dev_offset); | |
759 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); | |
0b86a832 CM |
760 | physical = dev_offset; |
761 | index++; | |
762 | } | |
6324fbf3 | 763 | BUG_ON(!list_empty(&private_devs)); |
0b86a832 | 764 | |
e17cade2 CM |
765 | /* key was set above */ |
766 | btrfs_set_stack_chunk_length(chunk, *num_bytes); | |
0b86a832 | 767 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
593060d7 | 768 | btrfs_set_stack_chunk_stripe_len(chunk, stripe_len); |
0b86a832 CM |
769 | btrfs_set_stack_chunk_type(chunk, type); |
770 | btrfs_set_stack_chunk_num_stripes(chunk, num_stripes); | |
593060d7 CM |
771 | btrfs_set_stack_chunk_io_align(chunk, stripe_len); |
772 | btrfs_set_stack_chunk_io_width(chunk, stripe_len); | |
0b86a832 | 773 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
321aecc6 | 774 | btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes); |
593060d7 CM |
775 | map->sector_size = extent_root->sectorsize; |
776 | map->stripe_len = stripe_len; | |
777 | map->io_align = stripe_len; | |
778 | map->io_width = stripe_len; | |
779 | map->type = type; | |
780 | map->num_stripes = num_stripes; | |
321aecc6 | 781 | map->sub_stripes = sub_stripes; |
0b86a832 CM |
782 | |
783 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, | |
784 | btrfs_chunk_item_size(num_stripes)); | |
785 | BUG_ON(ret); | |
e17cade2 | 786 | *start = key.offset;; |
0b86a832 CM |
787 | |
788 | em = alloc_extent_map(GFP_NOFS); | |
789 | if (!em) | |
790 | return -ENOMEM; | |
0b86a832 | 791 | em->bdev = (struct block_device *)map; |
e17cade2 CM |
792 | em->start = key.offset; |
793 | em->len = *num_bytes; | |
0b86a832 CM |
794 | em->block_start = 0; |
795 | ||
0b86a832 CM |
796 | kfree(chunk); |
797 | ||
798 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; | |
799 | spin_lock(&em_tree->lock); | |
800 | ret = add_extent_mapping(em_tree, em); | |
0b86a832 | 801 | spin_unlock(&em_tree->lock); |
b248a415 | 802 | BUG_ON(ret); |
0b86a832 CM |
803 | free_extent_map(em); |
804 | return ret; | |
805 | } | |
806 | ||
807 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) | |
808 | { | |
809 | extent_map_tree_init(&tree->map_tree, GFP_NOFS); | |
810 | } | |
811 | ||
812 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) | |
813 | { | |
814 | struct extent_map *em; | |
815 | ||
816 | while(1) { | |
817 | spin_lock(&tree->map_tree.lock); | |
818 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); | |
819 | if (em) | |
820 | remove_extent_mapping(&tree->map_tree, em); | |
821 | spin_unlock(&tree->map_tree.lock); | |
822 | if (!em) | |
823 | break; | |
824 | kfree(em->bdev); | |
825 | /* once for us */ | |
826 | free_extent_map(em); | |
827 | /* once for the tree */ | |
828 | free_extent_map(em); | |
829 | } | |
830 | } | |
831 | ||
f188591e CM |
832 | int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len) |
833 | { | |
834 | struct extent_map *em; | |
835 | struct map_lookup *map; | |
836 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
837 | int ret; | |
838 | ||
839 | spin_lock(&em_tree->lock); | |
840 | em = lookup_extent_mapping(em_tree, logical, len); | |
b248a415 | 841 | spin_unlock(&em_tree->lock); |
f188591e CM |
842 | BUG_ON(!em); |
843 | ||
844 | BUG_ON(em->start > logical || em->start + em->len < logical); | |
845 | map = (struct map_lookup *)em->bdev; | |
846 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) | |
847 | ret = map->num_stripes; | |
321aecc6 CM |
848 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
849 | ret = map->sub_stripes; | |
f188591e CM |
850 | else |
851 | ret = 1; | |
852 | free_extent_map(em); | |
f188591e CM |
853 | return ret; |
854 | } | |
855 | ||
8790d502 | 856 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
cea9e445 | 857 | u64 logical, u64 *length, |
f188591e | 858 | struct btrfs_multi_bio **multi_ret, int mirror_num) |
0b86a832 CM |
859 | { |
860 | struct extent_map *em; | |
861 | struct map_lookup *map; | |
862 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
863 | u64 offset; | |
593060d7 CM |
864 | u64 stripe_offset; |
865 | u64 stripe_nr; | |
cea9e445 | 866 | int stripes_allocated = 8; |
321aecc6 | 867 | int stripes_required = 1; |
593060d7 | 868 | int stripe_index; |
cea9e445 CM |
869 | int i; |
870 | struct btrfs_multi_bio *multi = NULL; | |
0b86a832 | 871 | |
cea9e445 CM |
872 | if (multi_ret && !(rw & (1 << BIO_RW))) { |
873 | stripes_allocated = 1; | |
874 | } | |
875 | again: | |
876 | if (multi_ret) { | |
877 | multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), | |
878 | GFP_NOFS); | |
879 | if (!multi) | |
880 | return -ENOMEM; | |
881 | } | |
0b86a832 CM |
882 | |
883 | spin_lock(&em_tree->lock); | |
884 | em = lookup_extent_mapping(em_tree, logical, *length); | |
b248a415 | 885 | spin_unlock(&em_tree->lock); |
0b86a832 CM |
886 | BUG_ON(!em); |
887 | ||
888 | BUG_ON(em->start > logical || em->start + em->len < logical); | |
889 | map = (struct map_lookup *)em->bdev; | |
890 | offset = logical - em->start; | |
593060d7 | 891 | |
f188591e CM |
892 | if (mirror_num > map->num_stripes) |
893 | mirror_num = 0; | |
894 | ||
cea9e445 | 895 | /* if our multi bio struct is too small, back off and try again */ |
321aecc6 CM |
896 | if (rw & (1 << BIO_RW)) { |
897 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | | |
898 | BTRFS_BLOCK_GROUP_DUP)) { | |
899 | stripes_required = map->num_stripes; | |
900 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { | |
901 | stripes_required = map->sub_stripes; | |
902 | } | |
903 | } | |
904 | if (multi_ret && rw == WRITE && | |
905 | stripes_allocated < stripes_required) { | |
cea9e445 | 906 | stripes_allocated = map->num_stripes; |
cea9e445 CM |
907 | free_extent_map(em); |
908 | kfree(multi); | |
909 | goto again; | |
910 | } | |
593060d7 CM |
911 | stripe_nr = offset; |
912 | /* | |
913 | * stripe_nr counts the total number of stripes we have to stride | |
914 | * to get to this block | |
915 | */ | |
916 | do_div(stripe_nr, map->stripe_len); | |
917 | ||
918 | stripe_offset = stripe_nr * map->stripe_len; | |
919 | BUG_ON(offset < stripe_offset); | |
920 | ||
921 | /* stripe_offset is the offset of this block in its stripe*/ | |
922 | stripe_offset = offset - stripe_offset; | |
923 | ||
cea9e445 | 924 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
321aecc6 | 925 | BTRFS_BLOCK_GROUP_RAID10 | |
cea9e445 CM |
926 | BTRFS_BLOCK_GROUP_DUP)) { |
927 | /* we limit the length of each bio to what fits in a stripe */ | |
928 | *length = min_t(u64, em->len - offset, | |
929 | map->stripe_len - stripe_offset); | |
930 | } else { | |
931 | *length = em->len - offset; | |
932 | } | |
933 | if (!multi_ret) | |
934 | goto out; | |
935 | ||
936 | multi->num_stripes = 1; | |
937 | stripe_index = 0; | |
8790d502 | 938 | if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
8790d502 | 939 | if (rw & (1 << BIO_RW)) |
cea9e445 | 940 | multi->num_stripes = map->num_stripes; |
f188591e CM |
941 | else if (mirror_num) { |
942 | stripe_index = mirror_num - 1; | |
943 | } else { | |
8790d502 CM |
944 | int i; |
945 | u64 least = (u64)-1; | |
946 | struct btrfs_device *cur; | |
947 | ||
948 | for (i = 0; i < map->num_stripes; i++) { | |
949 | cur = map->stripes[i].dev; | |
950 | spin_lock(&cur->io_lock); | |
951 | if (cur->total_ios < least) { | |
952 | least = cur->total_ios; | |
953 | stripe_index = i; | |
954 | } | |
955 | spin_unlock(&cur->io_lock); | |
956 | } | |
8790d502 | 957 | } |
611f0e00 | 958 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
cea9e445 CM |
959 | if (rw & (1 << BIO_RW)) |
960 | multi->num_stripes = map->num_stripes; | |
f188591e CM |
961 | else if (mirror_num) |
962 | stripe_index = mirror_num - 1; | |
321aecc6 CM |
963 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
964 | int factor = map->num_stripes / map->sub_stripes; | |
965 | int orig_stripe_nr = stripe_nr; | |
966 | ||
967 | stripe_index = do_div(stripe_nr, factor); | |
968 | stripe_index *= map->sub_stripes; | |
969 | ||
970 | if (rw & (1 << BIO_RW)) | |
971 | multi->num_stripes = map->sub_stripes; | |
972 | else if (mirror_num) | |
973 | stripe_index += mirror_num - 1; | |
974 | else | |
975 | stripe_index += orig_stripe_nr % map->sub_stripes; | |
8790d502 CM |
976 | } else { |
977 | /* | |
978 | * after this do_div call, stripe_nr is the number of stripes | |
979 | * on this device we have to walk to find the data, and | |
980 | * stripe_index is the number of our device in the stripe array | |
981 | */ | |
982 | stripe_index = do_div(stripe_nr, map->num_stripes); | |
983 | } | |
593060d7 | 984 | BUG_ON(stripe_index >= map->num_stripes); |
cea9e445 CM |
985 | |
986 | for (i = 0; i < multi->num_stripes; i++) { | |
987 | multi->stripes[i].physical = | |
988 | map->stripes[stripe_index].physical + stripe_offset + | |
989 | stripe_nr * map->stripe_len; | |
990 | multi->stripes[i].dev = map->stripes[stripe_index].dev; | |
991 | stripe_index++; | |
593060d7 | 992 | } |
cea9e445 CM |
993 | *multi_ret = multi; |
994 | out: | |
0b86a832 | 995 | free_extent_map(em); |
0b86a832 CM |
996 | return 0; |
997 | } | |
998 | ||
8790d502 CM |
999 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
1000 | static void end_bio_multi_stripe(struct bio *bio, int err) | |
1001 | #else | |
1002 | static int end_bio_multi_stripe(struct bio *bio, | |
1003 | unsigned int bytes_done, int err) | |
1004 | #endif | |
1005 | { | |
cea9e445 | 1006 | struct btrfs_multi_bio *multi = bio->bi_private; |
8790d502 CM |
1007 | |
1008 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) | |
1009 | if (bio->bi_size) | |
1010 | return 1; | |
1011 | #endif | |
1012 | if (err) | |
1013 | multi->error = err; | |
1014 | ||
cea9e445 | 1015 | if (atomic_dec_and_test(&multi->stripes_pending)) { |
8790d502 CM |
1016 | bio->bi_private = multi->private; |
1017 | bio->bi_end_io = multi->end_io; | |
1018 | ||
1019 | if (!err && multi->error) | |
1020 | err = multi->error; | |
1021 | kfree(multi); | |
1022 | ||
73f61b2a M |
1023 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
1024 | bio_endio(bio, bio->bi_size, err); | |
1025 | #else | |
8790d502 | 1026 | bio_endio(bio, err); |
73f61b2a | 1027 | #endif |
8790d502 CM |
1028 | } else { |
1029 | bio_put(bio); | |
1030 | } | |
1031 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) | |
1032 | return 0; | |
1033 | #endif | |
1034 | } | |
1035 | ||
f188591e CM |
1036 | int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, |
1037 | int mirror_num) | |
0b86a832 CM |
1038 | { |
1039 | struct btrfs_mapping_tree *map_tree; | |
1040 | struct btrfs_device *dev; | |
8790d502 | 1041 | struct bio *first_bio = bio; |
0b86a832 | 1042 | u64 logical = bio->bi_sector << 9; |
0b86a832 CM |
1043 | u64 length = 0; |
1044 | u64 map_length; | |
1045 | struct bio_vec *bvec; | |
cea9e445 | 1046 | struct btrfs_multi_bio *multi = NULL; |
0b86a832 CM |
1047 | int i; |
1048 | int ret; | |
8790d502 CM |
1049 | int dev_nr = 0; |
1050 | int total_devs = 1; | |
0b86a832 CM |
1051 | |
1052 | bio_for_each_segment(bvec, bio, i) { | |
1053 | length += bvec->bv_len; | |
1054 | } | |
8790d502 | 1055 | |
0b86a832 CM |
1056 | map_tree = &root->fs_info->mapping_tree; |
1057 | map_length = length; | |
cea9e445 | 1058 | |
f188591e CM |
1059 | ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi, |
1060 | mirror_num); | |
cea9e445 CM |
1061 | BUG_ON(ret); |
1062 | ||
1063 | total_devs = multi->num_stripes; | |
1064 | if (map_length < length) { | |
1065 | printk("mapping failed logical %Lu bio len %Lu " | |
1066 | "len %Lu\n", logical, length, map_length); | |
1067 | BUG(); | |
1068 | } | |
1069 | multi->end_io = first_bio->bi_end_io; | |
1070 | multi->private = first_bio->bi_private; | |
1071 | atomic_set(&multi->stripes_pending, multi->num_stripes); | |
1072 | ||
8790d502 | 1073 | while(dev_nr < total_devs) { |
8790d502 | 1074 | if (total_devs > 1) { |
8790d502 CM |
1075 | if (dev_nr < total_devs - 1) { |
1076 | bio = bio_clone(first_bio, GFP_NOFS); | |
1077 | BUG_ON(!bio); | |
1078 | } else { | |
1079 | bio = first_bio; | |
1080 | } | |
1081 | bio->bi_private = multi; | |
1082 | bio->bi_end_io = end_bio_multi_stripe; | |
1083 | } | |
cea9e445 CM |
1084 | bio->bi_sector = multi->stripes[dev_nr].physical >> 9; |
1085 | dev = multi->stripes[dev_nr].dev; | |
8790d502 CM |
1086 | bio->bi_bdev = dev->bdev; |
1087 | spin_lock(&dev->io_lock); | |
1088 | dev->total_ios++; | |
1089 | spin_unlock(&dev->io_lock); | |
1090 | submit_bio(rw, bio); | |
1091 | dev_nr++; | |
1092 | } | |
cea9e445 CM |
1093 | if (total_devs == 1) |
1094 | kfree(multi); | |
0b86a832 CM |
1095 | return 0; |
1096 | } | |
1097 | ||
1098 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid) | |
1099 | { | |
8a4b83cc | 1100 | struct list_head *head = &root->fs_info->fs_devices->devices; |
0b86a832 | 1101 | |
8a4b83cc | 1102 | return __find_device(head, devid); |
0b86a832 CM |
1103 | } |
1104 | ||
1105 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, | |
1106 | struct extent_buffer *leaf, | |
1107 | struct btrfs_chunk *chunk) | |
1108 | { | |
1109 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; | |
1110 | struct map_lookup *map; | |
1111 | struct extent_map *em; | |
1112 | u64 logical; | |
1113 | u64 length; | |
1114 | u64 devid; | |
593060d7 | 1115 | int num_stripes; |
0b86a832 | 1116 | int ret; |
593060d7 | 1117 | int i; |
0b86a832 | 1118 | |
e17cade2 CM |
1119 | logical = key->offset; |
1120 | length = btrfs_chunk_length(leaf, chunk); | |
0b86a832 CM |
1121 | spin_lock(&map_tree->map_tree.lock); |
1122 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); | |
b248a415 | 1123 | spin_unlock(&map_tree->map_tree.lock); |
0b86a832 CM |
1124 | |
1125 | /* already mapped? */ | |
1126 | if (em && em->start <= logical && em->start + em->len > logical) { | |
1127 | free_extent_map(em); | |
0b86a832 CM |
1128 | return 0; |
1129 | } else if (em) { | |
1130 | free_extent_map(em); | |
1131 | } | |
0b86a832 CM |
1132 | |
1133 | map = kzalloc(sizeof(*map), GFP_NOFS); | |
1134 | if (!map) | |
1135 | return -ENOMEM; | |
1136 | ||
1137 | em = alloc_extent_map(GFP_NOFS); | |
1138 | if (!em) | |
1139 | return -ENOMEM; | |
593060d7 CM |
1140 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
1141 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
0b86a832 CM |
1142 | if (!map) { |
1143 | free_extent_map(em); | |
1144 | return -ENOMEM; | |
1145 | } | |
1146 | ||
1147 | em->bdev = (struct block_device *)map; | |
1148 | em->start = logical; | |
1149 | em->len = length; | |
1150 | em->block_start = 0; | |
1151 | ||
593060d7 CM |
1152 | map->num_stripes = num_stripes; |
1153 | map->io_width = btrfs_chunk_io_width(leaf, chunk); | |
1154 | map->io_align = btrfs_chunk_io_align(leaf, chunk); | |
1155 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); | |
1156 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); | |
1157 | map->type = btrfs_chunk_type(leaf, chunk); | |
321aecc6 | 1158 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
593060d7 CM |
1159 | for (i = 0; i < num_stripes; i++) { |
1160 | map->stripes[i].physical = | |
1161 | btrfs_stripe_offset_nr(leaf, chunk, i); | |
1162 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); | |
1163 | map->stripes[i].dev = btrfs_find_device(root, devid); | |
1164 | if (!map->stripes[i].dev) { | |
1165 | kfree(map); | |
1166 | free_extent_map(em); | |
1167 | return -EIO; | |
1168 | } | |
0b86a832 CM |
1169 | } |
1170 | ||
1171 | spin_lock(&map_tree->map_tree.lock); | |
1172 | ret = add_extent_mapping(&map_tree->map_tree, em); | |
0b86a832 | 1173 | spin_unlock(&map_tree->map_tree.lock); |
b248a415 | 1174 | BUG_ON(ret); |
0b86a832 CM |
1175 | free_extent_map(em); |
1176 | ||
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | static int fill_device_from_item(struct extent_buffer *leaf, | |
1181 | struct btrfs_dev_item *dev_item, | |
1182 | struct btrfs_device *device) | |
1183 | { | |
1184 | unsigned long ptr; | |
0b86a832 CM |
1185 | |
1186 | device->devid = btrfs_device_id(leaf, dev_item); | |
1187 | device->total_bytes = btrfs_device_total_bytes(leaf, dev_item); | |
1188 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); | |
1189 | device->type = btrfs_device_type(leaf, dev_item); | |
1190 | device->io_align = btrfs_device_io_align(leaf, dev_item); | |
1191 | device->io_width = btrfs_device_io_width(leaf, dev_item); | |
1192 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); | |
0b86a832 CM |
1193 | |
1194 | ptr = (unsigned long)btrfs_device_uuid(dev_item); | |
e17cade2 | 1195 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
0b86a832 | 1196 | |
0b86a832 CM |
1197 | return 0; |
1198 | } | |
1199 | ||
0d81ba5d | 1200 | static int read_one_dev(struct btrfs_root *root, |
0b86a832 CM |
1201 | struct extent_buffer *leaf, |
1202 | struct btrfs_dev_item *dev_item) | |
1203 | { | |
1204 | struct btrfs_device *device; | |
1205 | u64 devid; | |
1206 | int ret; | |
0b86a832 | 1207 | devid = btrfs_device_id(leaf, dev_item); |
6324fbf3 CM |
1208 | device = btrfs_find_device(root, devid); |
1209 | if (!device) { | |
8a4b83cc | 1210 | printk("warning devid %Lu not found already\n", devid); |
f2984462 | 1211 | device = kzalloc(sizeof(*device), GFP_NOFS); |
6324fbf3 CM |
1212 | if (!device) |
1213 | return -ENOMEM; | |
8a4b83cc CM |
1214 | list_add(&device->dev_list, |
1215 | &root->fs_info->fs_devices->devices); | |
b248a415 | 1216 | device->barriers = 1; |
8790d502 | 1217 | spin_lock_init(&device->io_lock); |
6324fbf3 | 1218 | } |
0b86a832 CM |
1219 | |
1220 | fill_device_from_item(leaf, dev_item, device); | |
1221 | device->dev_root = root->fs_info->dev_root; | |
0b86a832 CM |
1222 | ret = 0; |
1223 | #if 0 | |
1224 | ret = btrfs_open_device(device); | |
1225 | if (ret) { | |
1226 | kfree(device); | |
1227 | } | |
1228 | #endif | |
1229 | return ret; | |
1230 | } | |
1231 | ||
0d81ba5d CM |
1232 | int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf) |
1233 | { | |
1234 | struct btrfs_dev_item *dev_item; | |
1235 | ||
1236 | dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block, | |
1237 | dev_item); | |
1238 | return read_one_dev(root, buf, dev_item); | |
1239 | } | |
1240 | ||
0b86a832 CM |
1241 | int btrfs_read_sys_array(struct btrfs_root *root) |
1242 | { | |
1243 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
1244 | struct extent_buffer *sb = root->fs_info->sb_buffer; | |
1245 | struct btrfs_disk_key *disk_key; | |
0b86a832 CM |
1246 | struct btrfs_chunk *chunk; |
1247 | struct btrfs_key key; | |
1248 | u32 num_stripes; | |
1249 | u32 array_size; | |
1250 | u32 len = 0; | |
1251 | u8 *ptr; | |
1252 | unsigned long sb_ptr; | |
1253 | u32 cur; | |
1254 | int ret; | |
0b86a832 CM |
1255 | |
1256 | array_size = btrfs_super_sys_array_size(super_copy); | |
1257 | ||
1258 | /* | |
1259 | * we do this loop twice, once for the device items and | |
1260 | * once for all of the chunks. This way there are device | |
1261 | * structs filled in for every chunk | |
1262 | */ | |
0b86a832 CM |
1263 | ptr = super_copy->sys_chunk_array; |
1264 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); | |
1265 | cur = 0; | |
1266 | ||
1267 | while (cur < array_size) { | |
1268 | disk_key = (struct btrfs_disk_key *)ptr; | |
1269 | btrfs_disk_key_to_cpu(&key, disk_key); | |
1270 | ||
1271 | len = sizeof(*disk_key); | |
1272 | ptr += len; | |
1273 | sb_ptr += len; | |
1274 | cur += len; | |
1275 | ||
0d81ba5d | 1276 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
0b86a832 | 1277 | chunk = (struct btrfs_chunk *)sb_ptr; |
0d81ba5d CM |
1278 | ret = read_one_chunk(root, &key, sb, chunk); |
1279 | BUG_ON(ret); | |
0b86a832 CM |
1280 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
1281 | len = btrfs_chunk_item_size(num_stripes); | |
1282 | } else { | |
1283 | BUG(); | |
1284 | } | |
1285 | ptr += len; | |
1286 | sb_ptr += len; | |
1287 | cur += len; | |
1288 | } | |
0b86a832 CM |
1289 | return 0; |
1290 | } | |
1291 | ||
1292 | int btrfs_read_chunk_tree(struct btrfs_root *root) | |
1293 | { | |
1294 | struct btrfs_path *path; | |
1295 | struct extent_buffer *leaf; | |
1296 | struct btrfs_key key; | |
1297 | struct btrfs_key found_key; | |
1298 | int ret; | |
1299 | int slot; | |
1300 | ||
1301 | root = root->fs_info->chunk_root; | |
1302 | ||
1303 | path = btrfs_alloc_path(); | |
1304 | if (!path) | |
1305 | return -ENOMEM; | |
1306 | ||
1307 | /* first we search for all of the device items, and then we | |
1308 | * read in all of the chunk items. This way we can create chunk | |
1309 | * mappings that reference all of the devices that are afound | |
1310 | */ | |
1311 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1312 | key.offset = 0; | |
1313 | key.type = 0; | |
1314 | again: | |
1315 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1316 | while(1) { | |
1317 | leaf = path->nodes[0]; | |
1318 | slot = path->slots[0]; | |
1319 | if (slot >= btrfs_header_nritems(leaf)) { | |
1320 | ret = btrfs_next_leaf(root, path); | |
1321 | if (ret == 0) | |
1322 | continue; | |
1323 | if (ret < 0) | |
1324 | goto error; | |
1325 | break; | |
1326 | } | |
1327 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
1328 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | |
1329 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) | |
1330 | break; | |
1331 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { | |
1332 | struct btrfs_dev_item *dev_item; | |
1333 | dev_item = btrfs_item_ptr(leaf, slot, | |
1334 | struct btrfs_dev_item); | |
0d81ba5d | 1335 | ret = read_one_dev(root, leaf, dev_item); |
0b86a832 CM |
1336 | BUG_ON(ret); |
1337 | } | |
1338 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { | |
1339 | struct btrfs_chunk *chunk; | |
1340 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); | |
1341 | ret = read_one_chunk(root, &found_key, leaf, chunk); | |
1342 | } | |
1343 | path->slots[0]++; | |
1344 | } | |
1345 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | |
1346 | key.objectid = 0; | |
1347 | btrfs_release_path(root, path); | |
1348 | goto again; | |
1349 | } | |
1350 | ||
1351 | btrfs_free_path(path); | |
1352 | ret = 0; | |
1353 | error: | |
1354 | return ret; | |
1355 | } | |
1356 |