2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
18 #define _XOPEN_SOURCE 600
22 #include <sys/types.h>
24 #include <uuid/uuid.h>
29 #include "transaction.h"
30 #include "print-tree.h"
35 struct btrfs_device
*dev
;
39 static inline int nr_parity_stripes(struct map_lookup
*map
)
41 if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
43 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
49 static inline int nr_data_stripes(struct map_lookup
*map
)
51 return map
->num_stripes
- nr_parity_stripes(map
);
54 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
56 static LIST_HEAD(fs_uuids
);
58 static struct btrfs_device
*__find_device(struct list_head
*head
, u64 devid
,
61 struct btrfs_device
*dev
;
62 struct list_head
*cur
;
64 list_for_each(cur
, head
) {
65 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
66 if (dev
->devid
== devid
&&
67 !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
)) {
74 static struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
76 struct list_head
*cur
;
77 struct btrfs_fs_devices
*fs_devices
;
79 list_for_each(cur
, &fs_uuids
) {
80 fs_devices
= list_entry(cur
, struct btrfs_fs_devices
, list
);
81 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
87 static int device_list_add(const char *path
,
88 struct btrfs_super_block
*disk_super
,
89 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
91 struct btrfs_device
*device
;
92 struct btrfs_fs_devices
*fs_devices
;
93 u64 found_transid
= btrfs_super_generation(disk_super
);
95 fs_devices
= find_fsid(disk_super
->fsid
);
97 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
100 INIT_LIST_HEAD(&fs_devices
->devices
);
101 list_add(&fs_devices
->list
, &fs_uuids
);
102 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
103 fs_devices
->latest_devid
= devid
;
104 fs_devices
->latest_trans
= found_transid
;
105 fs_devices
->lowest_devid
= (u64
)-1;
108 device
= __find_device(&fs_devices
->devices
, devid
,
109 disk_super
->dev_item
.uuid
);
112 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
114 /* we can safely leave the fs_devices entry around */
118 device
->devid
= devid
;
119 device
->generation
= found_transid
;
120 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
122 device
->name
= kstrdup(path
, GFP_NOFS
);
127 device
->label
= kstrdup(disk_super
->label
, GFP_NOFS
);
128 if (!device
->label
) {
133 device
->total_devs
= btrfs_super_num_devices(disk_super
);
134 device
->super_bytes_used
= btrfs_super_bytes_used(disk_super
);
135 device
->total_bytes
=
136 btrfs_stack_device_total_bytes(&disk_super
->dev_item
);
138 btrfs_stack_device_bytes_used(&disk_super
->dev_item
);
139 list_add(&device
->dev_list
, &fs_devices
->devices
);
140 device
->fs_devices
= fs_devices
;
141 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
142 char *name
= strdup(path
);
150 if (found_transid
> fs_devices
->latest_trans
) {
151 fs_devices
->latest_devid
= devid
;
152 fs_devices
->latest_trans
= found_transid
;
154 if (fs_devices
->lowest_devid
> devid
) {
155 fs_devices
->lowest_devid
= devid
;
157 *fs_devices_ret
= fs_devices
;
161 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
163 struct btrfs_fs_devices
*seed_devices
;
164 struct btrfs_device
*device
;
167 while (!list_empty(&fs_devices
->devices
)) {
168 device
= list_entry(fs_devices
->devices
.next
,
169 struct btrfs_device
, dev_list
);
170 if (device
->fd
!= -1) {
172 if (posix_fadvise(device
->fd
, 0, 0, POSIX_FADV_DONTNEED
))
173 fprintf(stderr
, "Warning, could not drop caches\n");
177 device
->writeable
= 0;
178 list_del(&device
->dev_list
);
179 /* free the memory */
185 seed_devices
= fs_devices
->seed
;
186 fs_devices
->seed
= NULL
;
188 struct btrfs_fs_devices
*orig
;
191 fs_devices
= seed_devices
;
192 list_del(&orig
->list
);
196 list_del(&fs_devices
->list
);
203 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
, int flags
)
206 struct list_head
*head
= &fs_devices
->devices
;
207 struct list_head
*cur
;
208 struct btrfs_device
*device
;
211 list_for_each(cur
, head
) {
212 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
214 printk("no name for device %llu, skip it now\n", device
->devid
);
218 fd
= open(device
->name
, flags
);
224 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_DONTNEED
))
225 fprintf(stderr
, "Warning, could not drop caches\n");
227 if (device
->devid
== fs_devices
->latest_devid
)
228 fs_devices
->latest_bdev
= fd
;
229 if (device
->devid
== fs_devices
->lowest_devid
)
230 fs_devices
->lowest_bdev
= fd
;
233 device
->writeable
= 1;
237 btrfs_close_devices(fs_devices
);
241 int btrfs_scan_one_device(int fd
, const char *path
,
242 struct btrfs_fs_devices
**fs_devices_ret
,
243 u64
*total_devs
, u64 super_offset
, int super_recover
)
245 struct btrfs_super_block
*disk_super
;
255 disk_super
= (struct btrfs_super_block
*)buf
;
256 ret
= btrfs_read_dev_super(fd
, disk_super
, super_offset
, super_recover
);
261 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
262 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_METADUMP
)
265 *total_devs
= btrfs_super_num_devices(disk_super
);
267 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
276 * this uses a pretty simple search, the expectation is that it is
277 * called very infrequently and that a given device has a small number
280 static int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
281 struct btrfs_device
*device
,
282 struct btrfs_path
*path
,
283 u64 num_bytes
, u64
*start
)
285 struct btrfs_key key
;
286 struct btrfs_root
*root
= device
->dev_root
;
287 struct btrfs_dev_extent
*dev_extent
= NULL
;
290 u64 search_start
= root
->fs_info
->alloc_start
;
291 u64 search_end
= device
->total_bytes
;
295 struct extent_buffer
*l
;
300 /* FIXME use last free of some kind */
302 /* we don't want to overwrite the superblock on the drive,
303 * so we make sure to start at an offset of at least 1MB
305 search_start
= max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
, search_start
);
307 if (search_start
>= search_end
) {
312 key
.objectid
= device
->devid
;
313 key
.offset
= search_start
;
314 key
.type
= BTRFS_DEV_EXTENT_KEY
;
315 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
318 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
322 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
325 slot
= path
->slots
[0];
326 if (slot
>= btrfs_header_nritems(l
)) {
327 ret
= btrfs_next_leaf(root
, path
);
334 if (search_start
>= search_end
) {
338 *start
= search_start
;
342 *start
= last_byte
> search_start
?
343 last_byte
: search_start
;
344 if (search_end
<= *start
) {
350 btrfs_item_key_to_cpu(l
, &key
, slot
);
352 if (key
.objectid
< device
->devid
)
355 if (key
.objectid
> device
->devid
)
358 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
360 if (last_byte
< search_start
)
361 last_byte
= search_start
;
362 hole_size
= key
.offset
- last_byte
;
363 if (key
.offset
> last_byte
&&
364 hole_size
>= num_bytes
) {
369 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
374 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
375 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
381 /* we have to make sure we didn't find an extent that has already
382 * been allocated by the map tree or the original allocation
384 btrfs_release_path(path
);
385 BUG_ON(*start
< search_start
);
387 if (*start
+ num_bytes
> search_end
) {
391 /* check for pending inserts here */
395 btrfs_release_path(path
);
399 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
400 struct btrfs_device
*device
,
401 u64 chunk_tree
, u64 chunk_objectid
,
403 u64 num_bytes
, u64
*start
)
406 struct btrfs_path
*path
;
407 struct btrfs_root
*root
= device
->dev_root
;
408 struct btrfs_dev_extent
*extent
;
409 struct extent_buffer
*leaf
;
410 struct btrfs_key key
;
412 path
= btrfs_alloc_path();
416 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
, start
);
421 key
.objectid
= device
->devid
;
423 key
.type
= BTRFS_DEV_EXTENT_KEY
;
424 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
428 leaf
= path
->nodes
[0];
429 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
430 struct btrfs_dev_extent
);
431 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
432 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
433 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
435 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
436 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
439 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
440 btrfs_mark_buffer_dirty(leaf
);
442 btrfs_free_path(path
);
446 static int find_next_chunk(struct btrfs_root
*root
, u64 objectid
, u64
*offset
)
448 struct btrfs_path
*path
;
450 struct btrfs_key key
;
451 struct btrfs_chunk
*chunk
;
452 struct btrfs_key found_key
;
454 path
= btrfs_alloc_path();
457 key
.objectid
= objectid
;
458 key
.offset
= (u64
)-1;
459 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
461 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
467 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
471 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
473 if (found_key
.objectid
!= objectid
)
476 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
478 *offset
= found_key
.offset
+
479 btrfs_chunk_length(path
->nodes
[0], chunk
);
484 btrfs_free_path(path
);
488 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
492 struct btrfs_key key
;
493 struct btrfs_key found_key
;
495 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
496 key
.type
= BTRFS_DEV_ITEM_KEY
;
497 key
.offset
= (u64
)-1;
499 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
505 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
510 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
512 *objectid
= found_key
.offset
+ 1;
516 btrfs_release_path(path
);
521 * the device information is stored in the chunk root
522 * the btrfs_device struct should be fully filled in
524 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
525 struct btrfs_root
*root
,
526 struct btrfs_device
*device
)
529 struct btrfs_path
*path
;
530 struct btrfs_dev_item
*dev_item
;
531 struct extent_buffer
*leaf
;
532 struct btrfs_key key
;
536 root
= root
->fs_info
->chunk_root
;
538 path
= btrfs_alloc_path();
542 ret
= find_next_devid(root
, path
, &free_devid
);
546 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
547 key
.type
= BTRFS_DEV_ITEM_KEY
;
548 key
.offset
= free_devid
;
550 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
555 leaf
= path
->nodes
[0];
556 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
558 device
->devid
= free_devid
;
559 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
560 btrfs_set_device_generation(leaf
, dev_item
, 0);
561 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
562 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
563 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
564 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
565 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
566 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
567 btrfs_set_device_group(leaf
, dev_item
, 0);
568 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
569 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
570 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
572 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
573 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
574 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
575 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
576 btrfs_mark_buffer_dirty(leaf
);
580 btrfs_free_path(path
);
584 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
585 struct btrfs_device
*device
)
588 struct btrfs_path
*path
;
589 struct btrfs_root
*root
;
590 struct btrfs_dev_item
*dev_item
;
591 struct extent_buffer
*leaf
;
592 struct btrfs_key key
;
594 root
= device
->dev_root
->fs_info
->chunk_root
;
596 path
= btrfs_alloc_path();
600 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
601 key
.type
= BTRFS_DEV_ITEM_KEY
;
602 key
.offset
= device
->devid
;
604 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
613 leaf
= path
->nodes
[0];
614 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
616 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
617 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
618 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
619 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
620 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
621 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
622 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
623 btrfs_mark_buffer_dirty(leaf
);
626 btrfs_free_path(path
);
630 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
631 struct btrfs_root
*root
,
632 struct btrfs_key
*key
,
633 struct btrfs_chunk
*chunk
, int item_size
)
635 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
636 struct btrfs_disk_key disk_key
;
640 array_size
= btrfs_super_sys_array_size(super_copy
);
641 if (array_size
+ item_size
+ sizeof(disk_key
)
642 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
645 ptr
= super_copy
->sys_chunk_array
+ array_size
;
646 btrfs_cpu_key_to_disk(&disk_key
, key
);
647 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
648 ptr
+= sizeof(disk_key
);
649 memcpy(ptr
, chunk
, item_size
);
650 item_size
+= sizeof(disk_key
);
651 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
655 static u64
chunk_bytes_by_type(u64 type
, u64 calc_size
, int num_stripes
,
658 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
660 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
661 return calc_size
* (num_stripes
/ sub_stripes
);
662 else if (type
& BTRFS_BLOCK_GROUP_RAID5
)
663 return calc_size
* (num_stripes
- 1);
664 else if (type
& BTRFS_BLOCK_GROUP_RAID6
)
665 return calc_size
* (num_stripes
- 2);
667 return calc_size
* num_stripes
;
671 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
673 /* TODO, add a way to store the preferred stripe size */
674 return BTRFS_STRIPE_LEN
;
678 * btrfs_device_avail_bytes - count bytes available for alloc_chunk
680 * It is not equal to "device->total_bytes - device->bytes_used".
681 * We do not allocate any chunk in 1M at beginning of device, and not
682 * allowed to allocate any chunk before alloc_start if it is specified.
683 * So search holes from max(1M, alloc_start) to device->total_bytes.
685 static int btrfs_device_avail_bytes(struct btrfs_trans_handle
*trans
,
686 struct btrfs_device
*device
,
689 struct btrfs_path
*path
;
690 struct btrfs_root
*root
= device
->dev_root
;
691 struct btrfs_key key
;
692 struct btrfs_dev_extent
*dev_extent
= NULL
;
693 struct extent_buffer
*l
;
694 u64 search_start
= root
->fs_info
->alloc_start
;
695 u64 search_end
= device
->total_bytes
;
701 search_start
= max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
, search_start
);
703 path
= btrfs_alloc_path();
707 key
.objectid
= device
->devid
;
708 key
.offset
= root
->fs_info
->alloc_start
;
709 key
.type
= BTRFS_DEV_EXTENT_KEY
;
712 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
715 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
721 slot
= path
->slots
[0];
722 if (slot
>= btrfs_header_nritems(l
)) {
723 ret
= btrfs_next_leaf(root
, path
);
730 btrfs_item_key_to_cpu(l
, &key
, slot
);
732 if (key
.objectid
< device
->devid
)
734 if (key
.objectid
> device
->devid
)
736 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
738 if (key
.offset
> search_end
)
740 if (key
.offset
> search_start
)
741 free_bytes
+= key
.offset
- search_start
;
743 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
744 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
746 if (extent_end
> search_start
)
747 search_start
= extent_end
;
748 if (search_start
> search_end
)
755 if (search_start
< search_end
)
756 free_bytes
+= search_end
- search_start
;
758 *avail_bytes
= free_bytes
;
761 btrfs_free_path(path
);
765 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
766 - sizeof(struct btrfs_item) \
767 - sizeof(struct btrfs_chunk)) \
768 / sizeof(struct btrfs_stripe) + 1)
770 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
771 - 2 * sizeof(struct btrfs_disk_key) \
772 - 2 * sizeof(struct btrfs_chunk)) \
773 / sizeof(struct btrfs_stripe) + 1)
775 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
776 struct btrfs_root
*extent_root
, u64
*start
,
777 u64
*num_bytes
, u64 type
)
780 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
781 struct btrfs_root
*chunk_root
= info
->chunk_root
;
782 struct btrfs_stripe
*stripes
;
783 struct btrfs_device
*device
= NULL
;
784 struct btrfs_chunk
*chunk
;
785 struct list_head private_devs
;
786 struct list_head
*dev_list
= &info
->fs_devices
->devices
;
787 struct list_head
*cur
;
788 struct map_lookup
*map
;
789 int min_stripe_size
= 1 * 1024 * 1024;
790 u64 calc_size
= 8 * 1024 * 1024;
792 u64 max_chunk_size
= 4 * calc_size
;
803 int stripe_len
= BTRFS_STRIPE_LEN
;
804 struct btrfs_key key
;
807 if (list_empty(dev_list
)) {
811 if (type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
812 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
813 BTRFS_BLOCK_GROUP_RAID10
|
814 BTRFS_BLOCK_GROUP_DUP
)) {
815 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
816 calc_size
= 8 * 1024 * 1024;
817 max_chunk_size
= calc_size
* 2;
818 min_stripe_size
= 1 * 1024 * 1024;
819 max_stripes
= BTRFS_MAX_DEVS_SYS_CHUNK
;
820 } else if (type
& BTRFS_BLOCK_GROUP_DATA
) {
821 calc_size
= 1024 * 1024 * 1024;
822 max_chunk_size
= 10 * calc_size
;
823 min_stripe_size
= 64 * 1024 * 1024;
824 max_stripes
= BTRFS_MAX_DEVS(chunk_root
);
825 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
826 calc_size
= 1024 * 1024 * 1024;
827 max_chunk_size
= 4 * calc_size
;
828 min_stripe_size
= 32 * 1024 * 1024;
829 max_stripes
= BTRFS_MAX_DEVS(chunk_root
);
832 if (type
& BTRFS_BLOCK_GROUP_RAID1
) {
833 num_stripes
= min_t(u64
, 2,
834 btrfs_super_num_devices(info
->super_copy
));
839 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
843 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
844 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
845 if (num_stripes
> max_stripes
)
846 num_stripes
= max_stripes
;
849 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
850 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
851 if (num_stripes
> max_stripes
)
852 num_stripes
= max_stripes
;
855 num_stripes
&= ~(u32
)1;
859 if (type
& (BTRFS_BLOCK_GROUP_RAID5
)) {
860 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
861 if (num_stripes
> max_stripes
)
862 num_stripes
= max_stripes
;
866 stripe_len
= find_raid56_stripe_len(num_stripes
- 1,
867 btrfs_super_stripesize(info
->super_copy
));
869 if (type
& (BTRFS_BLOCK_GROUP_RAID6
)) {
870 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
871 if (num_stripes
> max_stripes
)
872 num_stripes
= max_stripes
;
876 stripe_len
= find_raid56_stripe_len(num_stripes
- 2,
877 btrfs_super_stripesize(info
->super_copy
));
880 /* we don't want a chunk larger than 10% of the FS */
881 percent_max
= div_factor(btrfs_super_total_bytes(info
->super_copy
), 1);
882 max_chunk_size
= min(percent_max
, max_chunk_size
);
885 if (chunk_bytes_by_type(type
, calc_size
, num_stripes
, sub_stripes
) >
887 calc_size
= max_chunk_size
;
888 calc_size
/= num_stripes
;
889 calc_size
/= stripe_len
;
890 calc_size
*= stripe_len
;
892 /* we don't want tiny stripes */
893 calc_size
= max_t(u64
, calc_size
, min_stripe_size
);
895 calc_size
/= stripe_len
;
896 calc_size
*= stripe_len
;
897 INIT_LIST_HEAD(&private_devs
);
898 cur
= dev_list
->next
;
901 if (type
& BTRFS_BLOCK_GROUP_DUP
)
902 min_free
= calc_size
* 2;
904 min_free
= calc_size
;
906 /* build a private list of devices we will allocate from */
907 while(index
< num_stripes
) {
908 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
909 ret
= btrfs_device_avail_bytes(trans
, device
, &avail
);
913 if (avail
>= min_free
) {
914 list_move_tail(&device
->dev_list
, &private_devs
);
916 if (type
& BTRFS_BLOCK_GROUP_DUP
)
918 } else if (avail
> max_avail
)
923 if (index
< num_stripes
) {
924 list_splice(&private_devs
, dev_list
);
925 if (index
>= min_stripes
) {
927 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
928 num_stripes
/= sub_stripes
;
929 num_stripes
*= sub_stripes
;
934 if (!looped
&& max_avail
> 0) {
936 calc_size
= max_avail
;
941 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
945 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
946 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
949 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
953 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
959 stripes
= &chunk
->stripe
;
960 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
961 num_stripes
, sub_stripes
);
963 while(index
< num_stripes
) {
964 struct btrfs_stripe
*stripe
;
965 BUG_ON(list_empty(&private_devs
));
966 cur
= private_devs
.next
;
967 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
969 /* loop over this device again if we're doing a dup group */
970 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
971 (index
== num_stripes
- 1))
972 list_move_tail(&device
->dev_list
, dev_list
);
974 ret
= btrfs_alloc_dev_extent(trans
, device
,
975 info
->chunk_root
->root_key
.objectid
,
976 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
977 calc_size
, &dev_offset
);
980 device
->bytes_used
+= calc_size
;
981 ret
= btrfs_update_device(trans
, device
);
984 map
->stripes
[index
].dev
= device
;
985 map
->stripes
[index
].physical
= dev_offset
;
986 stripe
= stripes
+ index
;
987 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
988 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
989 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
992 BUG_ON(!list_empty(&private_devs
));
994 /* key was set above */
995 btrfs_set_stack_chunk_length(chunk
, *num_bytes
);
996 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
997 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
998 btrfs_set_stack_chunk_type(chunk
, type
);
999 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1000 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1001 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1002 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1003 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1004 map
->sector_size
= extent_root
->sectorsize
;
1005 map
->stripe_len
= stripe_len
;
1006 map
->io_align
= stripe_len
;
1007 map
->io_width
= stripe_len
;
1009 map
->num_stripes
= num_stripes
;
1010 map
->sub_stripes
= sub_stripes
;
1012 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1013 btrfs_chunk_item_size(num_stripes
));
1015 *start
= key
.offset
;;
1017 map
->ce
.start
= key
.offset
;
1018 map
->ce
.size
= *num_bytes
;
1020 ret
= insert_cache_extent(&info
->mapping_tree
.cache_tree
, &map
->ce
);
1023 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1024 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
1025 chunk
, btrfs_chunk_item_size(num_stripes
));
1033 int btrfs_alloc_data_chunk(struct btrfs_trans_handle
*trans
,
1034 struct btrfs_root
*extent_root
, u64
*start
,
1035 u64 num_bytes
, u64 type
)
1038 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1039 struct btrfs_root
*chunk_root
= info
->chunk_root
;
1040 struct btrfs_stripe
*stripes
;
1041 struct btrfs_device
*device
= NULL
;
1042 struct btrfs_chunk
*chunk
;
1043 struct list_head
*dev_list
= &info
->fs_devices
->devices
;
1044 struct list_head
*cur
;
1045 struct map_lookup
*map
;
1046 u64 calc_size
= 8 * 1024 * 1024;
1047 int num_stripes
= 1;
1048 int sub_stripes
= 0;
1051 int stripe_len
= BTRFS_STRIPE_LEN
;
1052 struct btrfs_key key
;
1054 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1055 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1056 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1061 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1065 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
1071 stripes
= &chunk
->stripe
;
1072 calc_size
= num_bytes
;
1075 cur
= dev_list
->next
;
1076 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1078 while (index
< num_stripes
) {
1079 struct btrfs_stripe
*stripe
;
1081 ret
= btrfs_alloc_dev_extent(trans
, device
,
1082 info
->chunk_root
->root_key
.objectid
,
1083 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1084 calc_size
, &dev_offset
);
1087 device
->bytes_used
+= calc_size
;
1088 ret
= btrfs_update_device(trans
, device
);
1091 map
->stripes
[index
].dev
= device
;
1092 map
->stripes
[index
].physical
= dev_offset
;
1093 stripe
= stripes
+ index
;
1094 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1095 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1096 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1100 /* key was set above */
1101 btrfs_set_stack_chunk_length(chunk
, num_bytes
);
1102 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1103 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1104 btrfs_set_stack_chunk_type(chunk
, type
);
1105 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1106 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1107 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1108 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1109 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1110 map
->sector_size
= extent_root
->sectorsize
;
1111 map
->stripe_len
= stripe_len
;
1112 map
->io_align
= stripe_len
;
1113 map
->io_width
= stripe_len
;
1115 map
->num_stripes
= num_stripes
;
1116 map
->sub_stripes
= sub_stripes
;
1118 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1119 btrfs_chunk_item_size(num_stripes
));
1121 *start
= key
.offset
;
1123 map
->ce
.start
= key
.offset
;
1124 map
->ce
.size
= num_bytes
;
1126 ret
= insert_cache_extent(&info
->mapping_tree
.cache_tree
, &map
->ce
);
1133 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
1135 struct cache_extent
*ce
;
1136 struct map_lookup
*map
;
1139 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1141 fprintf(stderr
, "No mapping for %llu-%llu\n",
1142 (unsigned long long)logical
,
1143 (unsigned long long)logical
+len
);
1146 if (ce
->start
> logical
|| ce
->start
+ ce
->size
< logical
) {
1147 fprintf(stderr
, "Invalid mapping for %llu-%llu, got "
1148 "%llu-%llu\n", (unsigned long long)logical
,
1149 (unsigned long long)logical
+len
,
1150 (unsigned long long)ce
->start
,
1151 (unsigned long long)ce
->start
+ ce
->size
);
1154 map
= container_of(ce
, struct map_lookup
, ce
);
1156 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
1157 ret
= map
->num_stripes
;
1158 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1159 ret
= map
->sub_stripes
;
1160 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
1162 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
1169 int btrfs_next_metadata(struct btrfs_mapping_tree
*map_tree
, u64
*logical
,
1172 struct cache_extent
*ce
;
1173 struct map_lookup
*map
;
1175 ce
= search_cache_extent(&map_tree
->cache_tree
, *logical
);
1178 ce
= next_cache_extent(ce
);
1182 map
= container_of(ce
, struct map_lookup
, ce
);
1183 if (map
->type
& BTRFS_BLOCK_GROUP_METADATA
) {
1184 *logical
= ce
->start
;
1193 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
1194 u64 chunk_start
, u64 physical
, u64 devid
,
1195 u64
**logical
, int *naddrs
, int *stripe_len
)
1197 struct cache_extent
*ce
;
1198 struct map_lookup
*map
;
1206 ce
= search_cache_extent(&map_tree
->cache_tree
, chunk_start
);
1208 map
= container_of(ce
, struct map_lookup
, ce
);
1211 rmap_len
= map
->stripe_len
;
1212 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1213 length
= ce
->size
/ (map
->num_stripes
/ map
->sub_stripes
);
1214 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
1215 length
= ce
->size
/ map
->num_stripes
;
1216 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
1217 BTRFS_BLOCK_GROUP_RAID6
)) {
1218 length
= ce
->size
/ nr_data_stripes(map
);
1219 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
1222 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
1224 for (i
= 0; i
< map
->num_stripes
; i
++) {
1225 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
1227 if (map
->stripes
[i
].physical
> physical
||
1228 map
->stripes
[i
].physical
+ length
<= physical
)
1231 stripe_nr
= (physical
- map
->stripes
[i
].physical
) /
1234 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1235 stripe_nr
= (stripe_nr
* map
->num_stripes
+ i
) /
1237 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
1238 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
1239 } /* else if RAID[56], multiply by nr_data_stripes().
1240 * Alternatively, just use rmap_len below instead of
1241 * map->stripe_len */
1243 bytenr
= ce
->start
+ stripe_nr
* rmap_len
;
1244 for (j
= 0; j
< nr
; j
++) {
1245 if (buf
[j
] == bytenr
)
1254 *stripe_len
= rmap_len
;
1259 static inline int parity_smaller(u64 a
, u64 b
)
1264 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1265 static void sort_parity_stripes(struct btrfs_multi_bio
*bbio
, u64
*raid_map
)
1267 struct btrfs_bio_stripe s
;
1274 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
1275 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
1276 s
= bbio
->stripes
[i
];
1278 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
1279 raid_map
[i
] = raid_map
[i
+1];
1280 bbio
->stripes
[i
+1] = s
;
1288 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1289 u64 logical
, u64
*length
,
1290 struct btrfs_multi_bio
**multi_ret
, int mirror_num
,
1293 return __btrfs_map_block(map_tree
, rw
, logical
, length
, NULL
,
1294 multi_ret
, mirror_num
, raid_map_ret
);
1297 int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1298 u64 logical
, u64
*length
, u64
*type
,
1299 struct btrfs_multi_bio
**multi_ret
, int mirror_num
,
1302 struct cache_extent
*ce
;
1303 struct map_lookup
*map
;
1307 u64
*raid_map
= NULL
;
1308 int stripes_allocated
= 8;
1309 int stripes_required
= 1;
1312 struct btrfs_multi_bio
*multi
= NULL
;
1314 if (multi_ret
&& rw
== READ
) {
1315 stripes_allocated
= 1;
1318 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1323 if (ce
->start
> logical
|| ce
->start
+ ce
->size
< logical
) {
1329 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
1334 map
= container_of(ce
, struct map_lookup
, ce
);
1335 offset
= logical
- ce
->start
;
1338 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
1339 BTRFS_BLOCK_GROUP_DUP
)) {
1340 stripes_required
= map
->num_stripes
;
1341 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1342 stripes_required
= map
->sub_stripes
;
1345 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)
1346 && multi_ret
&& ((rw
& WRITE
) || mirror_num
> 1) && raid_map_ret
) {
1347 /* RAID[56] write or recovery. Return all stripes */
1348 stripes_required
= map
->num_stripes
;
1350 /* Only allocate the map if we've already got a large enough multi_ret */
1351 if (stripes_allocated
>= stripes_required
) {
1352 raid_map
= kmalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
1360 /* if our multi bio struct is too small, back off and try again */
1361 if (multi_ret
&& stripes_allocated
< stripes_required
) {
1362 stripes_allocated
= stripes_required
;
1369 * stripe_nr counts the total number of stripes we have to stride
1370 * to get to this block
1372 stripe_nr
= stripe_nr
/ map
->stripe_len
;
1374 stripe_offset
= stripe_nr
* map
->stripe_len
;
1375 BUG_ON(offset
< stripe_offset
);
1377 /* stripe_offset is the offset of this block in its stripe*/
1378 stripe_offset
= offset
- stripe_offset
;
1380 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
1381 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
1382 BTRFS_BLOCK_GROUP_RAID10
|
1383 BTRFS_BLOCK_GROUP_DUP
)) {
1384 /* we limit the length of each bio to what fits in a stripe */
1385 *length
= min_t(u64
, ce
->size
- offset
,
1386 map
->stripe_len
- stripe_offset
);
1388 *length
= ce
->size
- offset
;
1394 multi
->num_stripes
= 1;
1396 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1398 multi
->num_stripes
= map
->num_stripes
;
1399 else if (mirror_num
)
1400 stripe_index
= mirror_num
- 1;
1402 stripe_index
= stripe_nr
% map
->num_stripes
;
1403 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1404 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1406 stripe_index
= stripe_nr
% factor
;
1407 stripe_index
*= map
->sub_stripes
;
1410 multi
->num_stripes
= map
->sub_stripes
;
1411 else if (mirror_num
)
1412 stripe_index
+= mirror_num
- 1;
1414 stripe_nr
= stripe_nr
/ factor
;
1415 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1417 multi
->num_stripes
= map
->num_stripes
;
1418 else if (mirror_num
)
1419 stripe_index
= mirror_num
- 1;
1420 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
1421 BTRFS_BLOCK_GROUP_RAID6
)) {
1426 u64 raid56_full_stripe_start
;
1427 u64 full_stripe_len
= nr_data_stripes(map
) * map
->stripe_len
;
1430 * align the start of our data stripe in the logical
1433 raid56_full_stripe_start
= offset
/ full_stripe_len
;
1434 raid56_full_stripe_start
*= full_stripe_len
;
1436 /* get the data stripe number */
1437 stripe_nr
= raid56_full_stripe_start
/ map
->stripe_len
;
1438 stripe_nr
= stripe_nr
/ nr_data_stripes(map
);
1440 /* Work out the disk rotation on this stripe-set */
1441 rot
= stripe_nr
% map
->num_stripes
;
1443 /* Fill in the logical address of each stripe */
1444 tmp
= stripe_nr
* nr_data_stripes(map
);
1446 for (i
= 0; i
< nr_data_stripes(map
); i
++)
1447 raid_map
[(i
+rot
) % map
->num_stripes
] =
1448 ce
->start
+ (tmp
+ i
) * map
->stripe_len
;
1450 raid_map
[(i
+rot
) % map
->num_stripes
] = BTRFS_RAID5_P_STRIPE
;
1451 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
1452 raid_map
[(i
+rot
+1) % map
->num_stripes
] = BTRFS_RAID6_Q_STRIPE
;
1454 *length
= map
->stripe_len
;
1457 multi
->num_stripes
= map
->num_stripes
;
1459 stripe_index
= stripe_nr
% nr_data_stripes(map
);
1460 stripe_nr
= stripe_nr
/ nr_data_stripes(map
);
1463 * Mirror #0 or #1 means the original data block.
1464 * Mirror #2 is RAID5 parity block.
1465 * Mirror #3 is RAID6 Q block.
1468 stripe_index
= nr_data_stripes(map
) + mirror_num
- 2;
1470 /* We distribute the parity blocks across stripes */
1471 stripe_index
= (stripe_nr
+ stripe_index
) % map
->num_stripes
;
1475 * after this do_div call, stripe_nr is the number of stripes
1476 * on this device we have to walk to find the data, and
1477 * stripe_index is the number of our device in the stripe array
1479 stripe_index
= stripe_nr
% map
->num_stripes
;
1480 stripe_nr
= stripe_nr
/ map
->num_stripes
;
1482 BUG_ON(stripe_index
>= map
->num_stripes
);
1484 for (i
= 0; i
< multi
->num_stripes
; i
++) {
1485 multi
->stripes
[i
].physical
=
1486 map
->stripes
[stripe_index
].physical
+ stripe_offset
+
1487 stripe_nr
* map
->stripe_len
;
1488 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
1497 sort_parity_stripes(multi
, raid_map
);
1498 *raid_map_ret
= raid_map
;
1504 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
1507 struct btrfs_device
*device
;
1508 struct btrfs_fs_devices
*cur_devices
;
1510 cur_devices
= root
->fs_info
->fs_devices
;
1511 while (cur_devices
) {
1513 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
1514 device
= __find_device(&cur_devices
->devices
,
1519 cur_devices
= cur_devices
->seed
;
1524 struct btrfs_device
*
1525 btrfs_find_device_by_devid(struct btrfs_fs_devices
*fs_devices
,
1526 u64 devid
, int instance
)
1528 struct list_head
*head
= &fs_devices
->devices
;
1529 struct btrfs_device
*dev
;
1532 list_for_each_entry(dev
, head
, dev_list
) {
1533 if (dev
->devid
== devid
&& num_found
++ == instance
)
1539 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
1541 struct cache_extent
*ce
;
1542 struct map_lookup
*map
;
1543 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1548 * During chunk recovering, we may fail to find block group's
1549 * corresponding chunk, we will rebuild it later
1551 ce
= search_cache_extent(&map_tree
->cache_tree
, chunk_offset
);
1552 if (!root
->fs_info
->is_chunk_recover
)
1557 map
= container_of(ce
, struct map_lookup
, ce
);
1558 for (i
= 0; i
< map
->num_stripes
; i
++) {
1559 if (!map
->stripes
[i
].dev
->writeable
) {
1568 static struct btrfs_device
*fill_missing_device(u64 devid
)
1570 struct btrfs_device
*device
;
1572 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1573 device
->devid
= devid
;
1578 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
1579 struct extent_buffer
*leaf
,
1580 struct btrfs_chunk
*chunk
)
1582 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1583 struct map_lookup
*map
;
1584 struct cache_extent
*ce
;
1588 u8 uuid
[BTRFS_UUID_SIZE
];
1593 logical
= key
->offset
;
1594 length
= btrfs_chunk_length(leaf
, chunk
);
1596 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1598 /* already mapped? */
1599 if (ce
&& ce
->start
<= logical
&& ce
->start
+ ce
->size
> logical
) {
1603 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
1604 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
1608 map
->ce
.start
= logical
;
1609 map
->ce
.size
= length
;
1610 map
->num_stripes
= num_stripes
;
1611 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
1612 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
1613 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
1614 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
1615 map
->type
= btrfs_chunk_type(leaf
, chunk
);
1616 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
1618 for (i
= 0; i
< num_stripes
; i
++) {
1619 map
->stripes
[i
].physical
=
1620 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
1621 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
1622 read_extent_buffer(leaf
, uuid
, (unsigned long)
1623 btrfs_stripe_dev_uuid_nr(chunk
, i
),
1625 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
1627 if (!map
->stripes
[i
].dev
) {
1628 map
->stripes
[i
].dev
= fill_missing_device(devid
);
1629 printf("warning, device %llu is missing\n",
1630 (unsigned long long)devid
);
1634 ret
= insert_cache_extent(&map_tree
->cache_tree
, &map
->ce
);
1640 static int fill_device_from_item(struct extent_buffer
*leaf
,
1641 struct btrfs_dev_item
*dev_item
,
1642 struct btrfs_device
*device
)
1646 device
->devid
= btrfs_device_id(leaf
, dev_item
);
1647 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
1648 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
1649 device
->type
= btrfs_device_type(leaf
, dev_item
);
1650 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
1651 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
1652 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
1654 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1655 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1660 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
1662 struct btrfs_fs_devices
*fs_devices
;
1665 fs_devices
= root
->fs_info
->fs_devices
->seed
;
1666 while (fs_devices
) {
1667 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
1671 fs_devices
= fs_devices
->seed
;
1674 fs_devices
= find_fsid(fsid
);
1676 /* missing all seed devices */
1677 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1682 INIT_LIST_HEAD(&fs_devices
->devices
);
1683 list_add(&fs_devices
->list
, &fs_uuids
);
1684 memcpy(fs_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
);
1687 ret
= btrfs_open_devices(fs_devices
, O_RDONLY
);
1691 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
1692 root
->fs_info
->fs_devices
->seed
= fs_devices
;
1697 static int read_one_dev(struct btrfs_root
*root
,
1698 struct extent_buffer
*leaf
,
1699 struct btrfs_dev_item
*dev_item
)
1701 struct btrfs_device
*device
;
1704 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1705 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1707 devid
= btrfs_device_id(leaf
, dev_item
);
1708 read_extent_buffer(leaf
, dev_uuid
,
1709 (unsigned long)btrfs_device_uuid(dev_item
),
1711 read_extent_buffer(leaf
, fs_uuid
,
1712 (unsigned long)btrfs_device_fsid(dev_item
),
1715 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
1716 ret
= open_seed_devices(root
, fs_uuid
);
1721 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1723 printk("warning devid %llu not found already\n",
1724 (unsigned long long)devid
);
1725 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1729 list_add(&device
->dev_list
,
1730 &root
->fs_info
->fs_devices
->devices
);
1733 fill_device_from_item(leaf
, dev_item
, device
);
1734 device
->dev_root
= root
->fs_info
->dev_root
;
1738 int btrfs_read_sys_array(struct btrfs_root
*root
)
1740 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1741 struct extent_buffer
*sb
;
1742 struct btrfs_disk_key
*disk_key
;
1743 struct btrfs_chunk
*chunk
;
1744 struct btrfs_key key
;
1751 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
1752 BTRFS_SUPER_INFO_SIZE
);
1755 btrfs_set_buffer_uptodate(sb
);
1756 write_extent_buffer(sb
, super_copy
, 0, sizeof(*super_copy
));
1757 array_end
= ((u8
*)super_copy
->sys_chunk_array
) +
1758 btrfs_super_sys_array_size(super_copy
);
1761 * we do this loop twice, once for the device items and
1762 * once for all of the chunks. This way there are device
1763 * structs filled in for every chunk
1765 ptr
= super_copy
->sys_chunk_array
;
1767 while (ptr
< array_end
) {
1768 disk_key
= (struct btrfs_disk_key
*)ptr
;
1769 btrfs_disk_key_to_cpu(&key
, disk_key
);
1771 len
= sizeof(*disk_key
);
1774 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1775 chunk
= (struct btrfs_chunk
*)(ptr
- (u8
*)super_copy
);
1776 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
1779 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
1780 len
= btrfs_chunk_item_size(num_stripes
);
1786 free_extent_buffer(sb
);
1790 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
1792 struct btrfs_path
*path
;
1793 struct extent_buffer
*leaf
;
1794 struct btrfs_key key
;
1795 struct btrfs_key found_key
;
1799 root
= root
->fs_info
->chunk_root
;
1801 path
= btrfs_alloc_path();
1806 * Read all device items, and then all the chunk items. All
1807 * device items are found before any chunk item (their object id
1808 * is smaller than the lowest possible object id for a chunk
1809 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
1811 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1814 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1818 leaf
= path
->nodes
[0];
1819 slot
= path
->slots
[0];
1820 if (slot
>= btrfs_header_nritems(leaf
)) {
1821 ret
= btrfs_next_leaf(root
, path
);
1828 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
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
);
1833 ret
= read_one_dev(root
, leaf
, dev_item
);
1835 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1836 struct btrfs_chunk
*chunk
;
1837 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
1838 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
1846 btrfs_free_path(path
);
1850 struct list_head
*btrfs_scanned_uuids(void)
1855 static int rmw_eb(struct btrfs_fs_info
*info
,
1856 struct extent_buffer
*eb
, struct extent_buffer
*orig_eb
)
1859 unsigned long orig_off
= 0;
1860 unsigned long dest_off
= 0;
1861 unsigned long copy_len
= eb
->len
;
1863 ret
= read_whole_eb(info
, eb
, 0);
1867 if (eb
->start
+ eb
->len
<= orig_eb
->start
||
1868 eb
->start
>= orig_eb
->start
+ orig_eb
->len
)
1871 * | ----- orig_eb ------- |
1872 * | ----- stripe ------- |
1873 * | ----- orig_eb ------- |
1874 * | ----- orig_eb ------- |
1876 if (eb
->start
> orig_eb
->start
)
1877 orig_off
= eb
->start
- orig_eb
->start
;
1878 if (orig_eb
->start
> eb
->start
)
1879 dest_off
= orig_eb
->start
- eb
->start
;
1881 if (copy_len
> orig_eb
->len
- orig_off
)
1882 copy_len
= orig_eb
->len
- orig_off
;
1883 if (copy_len
> eb
->len
- dest_off
)
1884 copy_len
= eb
->len
- dest_off
;
1886 memcpy(eb
->data
+ dest_off
, orig_eb
->data
+ orig_off
, copy_len
);
1890 static void split_eb_for_raid56(struct btrfs_fs_info
*info
,
1891 struct extent_buffer
*orig_eb
,
1892 struct extent_buffer
**ebs
,
1893 u64 stripe_len
, u64
*raid_map
,
1896 struct extent_buffer
*eb
;
1897 u64 start
= orig_eb
->start
;
1902 for (i
= 0; i
< num_stripes
; i
++) {
1903 if (raid_map
[i
] >= BTRFS_RAID5_P_STRIPE
)
1906 eb
= malloc(sizeof(struct extent_buffer
) + stripe_len
);
1909 memset(eb
, 0, sizeof(struct extent_buffer
) + stripe_len
);
1911 eb
->start
= raid_map
[i
];
1912 eb
->len
= stripe_len
;
1916 eb
->dev_bytenr
= (u64
)-1;
1918 this_eb_start
= raid_map
[i
];
1920 if (start
> this_eb_start
||
1921 start
+ orig_eb
->len
< this_eb_start
+ stripe_len
) {
1922 ret
= rmw_eb(info
, eb
, orig_eb
);
1925 memcpy(eb
->data
, orig_eb
->data
+ eb
->start
- start
, stripe_len
);
1931 int write_raid56_with_parity(struct btrfs_fs_info
*info
,
1932 struct extent_buffer
*eb
,
1933 struct btrfs_multi_bio
*multi
,
1934 u64 stripe_len
, u64
*raid_map
)
1936 struct extent_buffer
**ebs
, *p_eb
= NULL
, *q_eb
= NULL
;
1940 int alloc_size
= eb
->len
;
1942 ebs
= kmalloc(sizeof(*ebs
) * multi
->num_stripes
, GFP_NOFS
);
1945 if (stripe_len
> alloc_size
)
1946 alloc_size
= stripe_len
;
1948 split_eb_for_raid56(info
, eb
, ebs
, stripe_len
, raid_map
,
1949 multi
->num_stripes
);
1951 for (i
= 0; i
< multi
->num_stripes
; i
++) {
1952 struct extent_buffer
*new_eb
;
1953 if (raid_map
[i
] < BTRFS_RAID5_P_STRIPE
) {
1954 ebs
[i
]->dev_bytenr
= multi
->stripes
[i
].physical
;
1955 ebs
[i
]->fd
= multi
->stripes
[i
].dev
->fd
;
1956 multi
->stripes
[i
].dev
->total_ios
++;
1957 BUG_ON(ebs
[i
]->start
!= raid_map
[i
]);
1960 new_eb
= kmalloc(sizeof(*eb
) + alloc_size
, GFP_NOFS
);
1962 new_eb
->dev_bytenr
= multi
->stripes
[i
].physical
;
1963 new_eb
->fd
= multi
->stripes
[i
].dev
->fd
;
1964 multi
->stripes
[i
].dev
->total_ios
++;
1965 new_eb
->len
= stripe_len
;
1967 if (raid_map
[i
] == BTRFS_RAID5_P_STRIPE
)
1969 else if (raid_map
[i
] == BTRFS_RAID6_Q_STRIPE
)
1975 pointers
= kmalloc(sizeof(*pointers
) * multi
->num_stripes
,
1979 ebs
[multi
->num_stripes
- 2] = p_eb
;
1980 ebs
[multi
->num_stripes
- 1] = q_eb
;
1982 for (i
= 0; i
< multi
->num_stripes
; i
++)
1983 pointers
[i
] = ebs
[i
]->data
;
1985 raid6_gen_syndrome(multi
->num_stripes
, stripe_len
, pointers
);
1988 ebs
[multi
->num_stripes
- 1] = p_eb
;
1989 memcpy(p_eb
->data
, ebs
[0]->data
, stripe_len
);
1990 for (j
= 1; j
< multi
->num_stripes
- 1; j
++) {
1991 for (i
= 0; i
< stripe_len
; i
+= sizeof(unsigned long)) {
1992 *(unsigned long *)(p_eb
->data
+ i
) ^=
1993 *(unsigned long *)(ebs
[j
]->data
+ i
);
1998 for (i
= 0; i
< multi
->num_stripes
; i
++) {
1999 ret
= write_extent_to_disk(ebs
[i
]);