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.
20 #include <sys/types.h>
22 #include <uuid/uuid.h>
27 #include "transaction.h"
28 #include "print-tree.h"
33 struct btrfs_device
*dev
;
37 static inline int nr_parity_stripes(struct map_lookup
*map
)
39 if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
41 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
47 static inline int nr_data_stripes(struct map_lookup
*map
)
49 return map
->num_stripes
- nr_parity_stripes(map
);
52 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
54 static LIST_HEAD(fs_uuids
);
56 static struct btrfs_device
*__find_device(struct list_head
*head
, u64 devid
,
59 struct btrfs_device
*dev
;
60 struct list_head
*cur
;
62 list_for_each(cur
, head
) {
63 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
64 if (dev
->devid
== devid
&&
65 !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
)) {
72 static struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
74 struct list_head
*cur
;
75 struct btrfs_fs_devices
*fs_devices
;
77 list_for_each(cur
, &fs_uuids
) {
78 fs_devices
= list_entry(cur
, struct btrfs_fs_devices
, list
);
79 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
85 static int device_list_add(const char *path
,
86 struct btrfs_super_block
*disk_super
,
87 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
89 struct btrfs_device
*device
;
90 struct btrfs_fs_devices
*fs_devices
;
91 u64 found_transid
= btrfs_super_generation(disk_super
);
93 fs_devices
= find_fsid(disk_super
->fsid
);
95 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
98 INIT_LIST_HEAD(&fs_devices
->devices
);
99 list_add(&fs_devices
->list
, &fs_uuids
);
100 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
101 fs_devices
->latest_devid
= devid
;
102 fs_devices
->latest_trans
= found_transid
;
103 fs_devices
->lowest_devid
= (u64
)-1;
106 device
= __find_device(&fs_devices
->devices
, devid
,
107 disk_super
->dev_item
.uuid
);
110 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
112 /* we can safely leave the fs_devices entry around */
116 device
->devid
= devid
;
117 device
->generation
= found_transid
;
118 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
120 device
->name
= kstrdup(path
, GFP_NOFS
);
125 device
->label
= kstrdup(disk_super
->label
, GFP_NOFS
);
126 if (!device
->label
) {
131 device
->total_devs
= btrfs_super_num_devices(disk_super
);
132 device
->super_bytes_used
= btrfs_super_bytes_used(disk_super
);
133 device
->total_bytes
=
134 btrfs_stack_device_total_bytes(&disk_super
->dev_item
);
136 btrfs_stack_device_bytes_used(&disk_super
->dev_item
);
137 list_add(&device
->dev_list
, &fs_devices
->devices
);
138 device
->fs_devices
= fs_devices
;
139 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
140 char *name
= strdup(path
);
148 if (found_transid
> fs_devices
->latest_trans
) {
149 fs_devices
->latest_devid
= devid
;
150 fs_devices
->latest_trans
= found_transid
;
152 if (fs_devices
->lowest_devid
> devid
) {
153 fs_devices
->lowest_devid
= devid
;
155 *fs_devices_ret
= fs_devices
;
159 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
161 struct btrfs_fs_devices
*seed_devices
;
162 struct btrfs_device
*device
;
165 while (!list_empty(&fs_devices
->devices
)) {
166 device
= list_entry(fs_devices
->devices
.next
,
167 struct btrfs_device
, dev_list
);
168 if (device
->fd
!= -1) {
170 if (posix_fadvise(device
->fd
, 0, 0, POSIX_FADV_DONTNEED
))
171 fprintf(stderr
, "Warning, could not drop caches\n");
175 device
->writeable
= 0;
176 list_del(&device
->dev_list
);
177 /* free the memory */
183 seed_devices
= fs_devices
->seed
;
184 fs_devices
->seed
= NULL
;
186 struct btrfs_fs_devices
*orig
;
189 fs_devices
= seed_devices
;
190 list_del(&orig
->list
);
194 list_del(&fs_devices
->list
);
201 void btrfs_close_all_devices(void)
203 struct btrfs_fs_devices
*fs_devices
;
205 while (!list_empty(&fs_uuids
)) {
206 fs_devices
= list_entry(fs_uuids
.next
, struct btrfs_fs_devices
,
208 btrfs_close_devices(fs_devices
);
212 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
, int flags
)
215 struct list_head
*head
= &fs_devices
->devices
;
216 struct list_head
*cur
;
217 struct btrfs_device
*device
;
220 list_for_each(cur
, head
) {
221 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
223 printk("no name for device %llu, skip it now\n", device
->devid
);
227 fd
= open(device
->name
, flags
);
233 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_DONTNEED
))
234 fprintf(stderr
, "Warning, could not drop caches\n");
236 if (device
->devid
== fs_devices
->latest_devid
)
237 fs_devices
->latest_bdev
= fd
;
238 if (device
->devid
== fs_devices
->lowest_devid
)
239 fs_devices
->lowest_bdev
= fd
;
242 device
->writeable
= 1;
246 btrfs_close_devices(fs_devices
);
250 int btrfs_scan_one_device(int fd
, const char *path
,
251 struct btrfs_fs_devices
**fs_devices_ret
,
252 u64
*total_devs
, u64 super_offset
, int super_recover
)
254 struct btrfs_super_block
*disk_super
;
255 char buf
[BTRFS_SUPER_INFO_SIZE
];
259 disk_super
= (struct btrfs_super_block
*)buf
;
260 ret
= btrfs_read_dev_super(fd
, disk_super
, super_offset
, super_recover
);
263 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
264 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_METADUMP
)
267 *total_devs
= btrfs_super_num_devices(disk_super
);
269 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
275 * this uses a pretty simple search, the expectation is that it is
276 * called very infrequently and that a given device has a small number
279 static int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
280 struct btrfs_device
*device
,
281 struct btrfs_path
*path
,
282 u64 num_bytes
, u64
*start
)
284 struct btrfs_key key
;
285 struct btrfs_root
*root
= device
->dev_root
;
286 struct btrfs_dev_extent
*dev_extent
= NULL
;
289 u64 search_start
= root
->fs_info
->alloc_start
;
290 u64 search_end
= device
->total_bytes
;
294 struct extent_buffer
*l
;
299 /* FIXME use last free of some kind */
301 /* we don't want to overwrite the superblock on the drive,
302 * so we make sure to start at an offset of at least 1MB
304 search_start
= max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
, search_start
);
306 if (search_start
>= search_end
) {
311 key
.objectid
= device
->devid
;
312 key
.offset
= search_start
;
313 key
.type
= BTRFS_DEV_EXTENT_KEY
;
314 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
317 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
321 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
324 slot
= path
->slots
[0];
325 if (slot
>= btrfs_header_nritems(l
)) {
326 ret
= btrfs_next_leaf(root
, path
);
333 if (search_start
>= search_end
) {
337 *start
= search_start
;
341 *start
= last_byte
> search_start
?
342 last_byte
: search_start
;
343 if (search_end
<= *start
) {
349 btrfs_item_key_to_cpu(l
, &key
, slot
);
351 if (key
.objectid
< device
->devid
)
354 if (key
.objectid
> device
->devid
)
357 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
359 if (last_byte
< search_start
)
360 last_byte
= search_start
;
361 hole_size
= key
.offset
- last_byte
;
362 if (key
.offset
> last_byte
&&
363 hole_size
>= num_bytes
) {
368 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
373 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
374 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
380 /* we have to make sure we didn't find an extent that has already
381 * been allocated by the map tree or the original allocation
383 btrfs_release_path(path
);
384 BUG_ON(*start
< search_start
);
386 if (*start
+ num_bytes
> search_end
) {
390 /* check for pending inserts here */
394 btrfs_release_path(path
);
398 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
399 struct btrfs_device
*device
,
400 u64 chunk_tree
, u64 chunk_objectid
,
402 u64 num_bytes
, u64
*start
, int convert
)
405 struct btrfs_path
*path
;
406 struct btrfs_root
*root
= device
->dev_root
;
407 struct btrfs_dev_extent
*extent
;
408 struct extent_buffer
*leaf
;
409 struct btrfs_key key
;
411 path
= btrfs_alloc_path();
416 * For convert case, just skip search free dev_extent, as caller
417 * is responsible to make sure it's free.
420 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
,
426 key
.objectid
= device
->devid
;
428 key
.type
= BTRFS_DEV_EXTENT_KEY
;
429 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
433 leaf
= path
->nodes
[0];
434 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
435 struct btrfs_dev_extent
);
436 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
437 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
438 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
440 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
441 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
444 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
445 btrfs_mark_buffer_dirty(leaf
);
447 btrfs_free_path(path
);
451 static int find_next_chunk(struct btrfs_root
*root
, u64 objectid
, u64
*offset
)
453 struct btrfs_path
*path
;
455 struct btrfs_key key
;
456 struct btrfs_chunk
*chunk
;
457 struct btrfs_key found_key
;
459 path
= btrfs_alloc_path();
462 key
.objectid
= objectid
;
463 key
.offset
= (u64
)-1;
464 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
466 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
472 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
476 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
478 if (found_key
.objectid
!= objectid
)
481 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
483 *offset
= found_key
.offset
+
484 btrfs_chunk_length(path
->nodes
[0], chunk
);
489 btrfs_free_path(path
);
493 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
497 struct btrfs_key key
;
498 struct btrfs_key found_key
;
500 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
501 key
.type
= BTRFS_DEV_ITEM_KEY
;
502 key
.offset
= (u64
)-1;
504 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
510 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
515 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
517 *objectid
= found_key
.offset
+ 1;
521 btrfs_release_path(path
);
526 * the device information is stored in the chunk root
527 * the btrfs_device struct should be fully filled in
529 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
530 struct btrfs_root
*root
,
531 struct btrfs_device
*device
)
534 struct btrfs_path
*path
;
535 struct btrfs_dev_item
*dev_item
;
536 struct extent_buffer
*leaf
;
537 struct btrfs_key key
;
541 root
= root
->fs_info
->chunk_root
;
543 path
= btrfs_alloc_path();
547 ret
= find_next_devid(root
, path
, &free_devid
);
551 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
552 key
.type
= BTRFS_DEV_ITEM_KEY
;
553 key
.offset
= free_devid
;
555 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
560 leaf
= path
->nodes
[0];
561 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
563 device
->devid
= free_devid
;
564 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
565 btrfs_set_device_generation(leaf
, dev_item
, 0);
566 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
567 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
568 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
569 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
570 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
571 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
572 btrfs_set_device_group(leaf
, dev_item
, 0);
573 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
574 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
575 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
577 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
578 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
579 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
580 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
581 btrfs_mark_buffer_dirty(leaf
);
585 btrfs_free_path(path
);
589 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
590 struct btrfs_device
*device
)
593 struct btrfs_path
*path
;
594 struct btrfs_root
*root
;
595 struct btrfs_dev_item
*dev_item
;
596 struct extent_buffer
*leaf
;
597 struct btrfs_key key
;
599 root
= device
->dev_root
->fs_info
->chunk_root
;
601 path
= btrfs_alloc_path();
605 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
606 key
.type
= BTRFS_DEV_ITEM_KEY
;
607 key
.offset
= device
->devid
;
609 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
618 leaf
= path
->nodes
[0];
619 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
621 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
622 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
623 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
624 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
625 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
626 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
627 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
628 btrfs_mark_buffer_dirty(leaf
);
631 btrfs_free_path(path
);
635 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
636 struct btrfs_root
*root
,
637 struct btrfs_key
*key
,
638 struct btrfs_chunk
*chunk
, int item_size
)
640 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
641 struct btrfs_disk_key disk_key
;
645 array_size
= btrfs_super_sys_array_size(super_copy
);
646 if (array_size
+ item_size
+ sizeof(disk_key
)
647 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
650 ptr
= super_copy
->sys_chunk_array
+ array_size
;
651 btrfs_cpu_key_to_disk(&disk_key
, key
);
652 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
653 ptr
+= sizeof(disk_key
);
654 memcpy(ptr
, chunk
, item_size
);
655 item_size
+= sizeof(disk_key
);
656 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
660 static u64
chunk_bytes_by_type(u64 type
, u64 calc_size
, int num_stripes
,
663 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
665 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
666 return calc_size
* (num_stripes
/ sub_stripes
);
667 else if (type
& BTRFS_BLOCK_GROUP_RAID5
)
668 return calc_size
* (num_stripes
- 1);
669 else if (type
& BTRFS_BLOCK_GROUP_RAID6
)
670 return calc_size
* (num_stripes
- 2);
672 return calc_size
* num_stripes
;
676 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
678 /* TODO, add a way to store the preferred stripe size */
679 return BTRFS_STRIPE_LEN
;
683 * btrfs_device_avail_bytes - count bytes available for alloc_chunk
685 * It is not equal to "device->total_bytes - device->bytes_used".
686 * We do not allocate any chunk in 1M at beginning of device, and not
687 * allowed to allocate any chunk before alloc_start if it is specified.
688 * So search holes from max(1M, alloc_start) to device->total_bytes.
690 static int btrfs_device_avail_bytes(struct btrfs_trans_handle
*trans
,
691 struct btrfs_device
*device
,
694 struct btrfs_path
*path
;
695 struct btrfs_root
*root
= device
->dev_root
;
696 struct btrfs_key key
;
697 struct btrfs_dev_extent
*dev_extent
= NULL
;
698 struct extent_buffer
*l
;
699 u64 search_start
= root
->fs_info
->alloc_start
;
700 u64 search_end
= device
->total_bytes
;
706 search_start
= max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
, search_start
);
708 path
= btrfs_alloc_path();
712 key
.objectid
= device
->devid
;
713 key
.offset
= root
->fs_info
->alloc_start
;
714 key
.type
= BTRFS_DEV_EXTENT_KEY
;
717 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
720 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
726 slot
= path
->slots
[0];
727 if (slot
>= btrfs_header_nritems(l
)) {
728 ret
= btrfs_next_leaf(root
, path
);
735 btrfs_item_key_to_cpu(l
, &key
, slot
);
737 if (key
.objectid
< device
->devid
)
739 if (key
.objectid
> device
->devid
)
741 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
743 if (key
.offset
> search_end
)
745 if (key
.offset
> search_start
)
746 free_bytes
+= key
.offset
- search_start
;
748 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
749 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
751 if (extent_end
> search_start
)
752 search_start
= extent_end
;
753 if (search_start
> search_end
)
760 if (search_start
< search_end
)
761 free_bytes
+= search_end
- search_start
;
763 *avail_bytes
= free_bytes
;
766 btrfs_free_path(path
);
770 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
771 - sizeof(struct btrfs_item) \
772 - sizeof(struct btrfs_chunk)) \
773 / sizeof(struct btrfs_stripe) + 1)
775 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
776 - 2 * sizeof(struct btrfs_disk_key) \
777 - 2 * sizeof(struct btrfs_chunk)) \
778 / sizeof(struct btrfs_stripe) + 1)
780 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
781 struct btrfs_root
*extent_root
, u64
*start
,
782 u64
*num_bytes
, u64 type
)
785 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
786 struct btrfs_root
*chunk_root
= info
->chunk_root
;
787 struct btrfs_stripe
*stripes
;
788 struct btrfs_device
*device
= NULL
;
789 struct btrfs_chunk
*chunk
;
790 struct list_head private_devs
;
791 struct list_head
*dev_list
= &info
->fs_devices
->devices
;
792 struct list_head
*cur
;
793 struct map_lookup
*map
;
794 int min_stripe_size
= 1 * 1024 * 1024;
795 u64 calc_size
= 8 * 1024 * 1024;
797 u64 max_chunk_size
= 4 * calc_size
;
808 int stripe_len
= BTRFS_STRIPE_LEN
;
809 struct btrfs_key key
;
812 if (list_empty(dev_list
)) {
816 if (type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
817 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
818 BTRFS_BLOCK_GROUP_RAID10
|
819 BTRFS_BLOCK_GROUP_DUP
)) {
820 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
821 calc_size
= 8 * 1024 * 1024;
822 max_chunk_size
= calc_size
* 2;
823 min_stripe_size
= 1 * 1024 * 1024;
824 max_stripes
= BTRFS_MAX_DEVS_SYS_CHUNK
;
825 } else if (type
& BTRFS_BLOCK_GROUP_DATA
) {
826 calc_size
= 1024 * 1024 * 1024;
827 max_chunk_size
= 10 * calc_size
;
828 min_stripe_size
= 64 * 1024 * 1024;
829 max_stripes
= BTRFS_MAX_DEVS(chunk_root
);
830 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
831 calc_size
= 1024 * 1024 * 1024;
832 max_chunk_size
= 4 * calc_size
;
833 min_stripe_size
= 32 * 1024 * 1024;
834 max_stripes
= BTRFS_MAX_DEVS(chunk_root
);
837 if (type
& BTRFS_BLOCK_GROUP_RAID1
) {
838 num_stripes
= min_t(u64
, 2,
839 btrfs_super_num_devices(info
->super_copy
));
844 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
848 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
849 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
850 if (num_stripes
> max_stripes
)
851 num_stripes
= max_stripes
;
854 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
855 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
856 if (num_stripes
> max_stripes
)
857 num_stripes
= max_stripes
;
860 num_stripes
&= ~(u32
)1;
864 if (type
& (BTRFS_BLOCK_GROUP_RAID5
)) {
865 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
866 if (num_stripes
> max_stripes
)
867 num_stripes
= max_stripes
;
871 stripe_len
= find_raid56_stripe_len(num_stripes
- 1,
872 btrfs_super_stripesize(info
->super_copy
));
874 if (type
& (BTRFS_BLOCK_GROUP_RAID6
)) {
875 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
876 if (num_stripes
> max_stripes
)
877 num_stripes
= max_stripes
;
881 stripe_len
= find_raid56_stripe_len(num_stripes
- 2,
882 btrfs_super_stripesize(info
->super_copy
));
885 /* we don't want a chunk larger than 10% of the FS */
886 percent_max
= div_factor(btrfs_super_total_bytes(info
->super_copy
), 1);
887 max_chunk_size
= min(percent_max
, max_chunk_size
);
890 if (chunk_bytes_by_type(type
, calc_size
, num_stripes
, sub_stripes
) >
892 calc_size
= max_chunk_size
;
893 calc_size
/= num_stripes
;
894 calc_size
/= stripe_len
;
895 calc_size
*= stripe_len
;
897 /* we don't want tiny stripes */
898 calc_size
= max_t(u64
, calc_size
, min_stripe_size
);
900 calc_size
/= stripe_len
;
901 calc_size
*= stripe_len
;
902 INIT_LIST_HEAD(&private_devs
);
903 cur
= dev_list
->next
;
906 if (type
& BTRFS_BLOCK_GROUP_DUP
)
907 min_free
= calc_size
* 2;
909 min_free
= calc_size
;
911 /* build a private list of devices we will allocate from */
912 while(index
< num_stripes
) {
913 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
914 ret
= btrfs_device_avail_bytes(trans
, device
, &avail
);
918 if (avail
>= min_free
) {
919 list_move_tail(&device
->dev_list
, &private_devs
);
921 if (type
& BTRFS_BLOCK_GROUP_DUP
)
923 } else if (avail
> max_avail
)
928 if (index
< num_stripes
) {
929 list_splice(&private_devs
, dev_list
);
930 if (index
>= min_stripes
) {
932 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
933 num_stripes
/= sub_stripes
;
934 num_stripes
*= sub_stripes
;
939 if (!looped
&& max_avail
> 0) {
941 calc_size
= max_avail
;
946 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
950 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
951 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
954 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
958 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
964 stripes
= &chunk
->stripe
;
965 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
966 num_stripes
, sub_stripes
);
968 while(index
< num_stripes
) {
969 struct btrfs_stripe
*stripe
;
970 BUG_ON(list_empty(&private_devs
));
971 cur
= private_devs
.next
;
972 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
974 /* loop over this device again if we're doing a dup group */
975 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
976 (index
== num_stripes
- 1))
977 list_move_tail(&device
->dev_list
, dev_list
);
979 ret
= btrfs_alloc_dev_extent(trans
, device
,
980 info
->chunk_root
->root_key
.objectid
,
981 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
982 calc_size
, &dev_offset
, 0);
985 device
->bytes_used
+= calc_size
;
986 ret
= btrfs_update_device(trans
, device
);
989 map
->stripes
[index
].dev
= device
;
990 map
->stripes
[index
].physical
= dev_offset
;
991 stripe
= stripes
+ index
;
992 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
993 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
994 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
997 BUG_ON(!list_empty(&private_devs
));
999 /* key was set above */
1000 btrfs_set_stack_chunk_length(chunk
, *num_bytes
);
1001 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1002 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1003 btrfs_set_stack_chunk_type(chunk
, type
);
1004 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1005 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1006 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1007 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1008 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1009 map
->sector_size
= extent_root
->sectorsize
;
1010 map
->stripe_len
= stripe_len
;
1011 map
->io_align
= stripe_len
;
1012 map
->io_width
= stripe_len
;
1014 map
->num_stripes
= num_stripes
;
1015 map
->sub_stripes
= sub_stripes
;
1017 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1018 btrfs_chunk_item_size(num_stripes
));
1020 *start
= key
.offset
;;
1022 map
->ce
.start
= key
.offset
;
1023 map
->ce
.size
= *num_bytes
;
1025 ret
= insert_cache_extent(&info
->mapping_tree
.cache_tree
, &map
->ce
);
1028 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1029 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
1030 chunk
, btrfs_chunk_item_size(num_stripes
));
1039 * Alloc a DATA chunk with SINGLE profile.
1041 * If 'convert' is set, it will alloc a chunk with 1:1 mapping
1042 * (btrfs logical bytenr == on-disk bytenr)
1043 * For that case, caller must make sure the chunk and dev_extent are not
1046 int btrfs_alloc_data_chunk(struct btrfs_trans_handle
*trans
,
1047 struct btrfs_root
*extent_root
, u64
*start
,
1048 u64 num_bytes
, u64 type
, int convert
)
1051 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1052 struct btrfs_root
*chunk_root
= info
->chunk_root
;
1053 struct btrfs_stripe
*stripes
;
1054 struct btrfs_device
*device
= NULL
;
1055 struct btrfs_chunk
*chunk
;
1056 struct list_head
*dev_list
= &info
->fs_devices
->devices
;
1057 struct list_head
*cur
;
1058 struct map_lookup
*map
;
1059 u64 calc_size
= 8 * 1024 * 1024;
1060 int num_stripes
= 1;
1061 int sub_stripes
= 0;
1064 int stripe_len
= BTRFS_STRIPE_LEN
;
1065 struct btrfs_key key
;
1067 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1068 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1070 BUG_ON(*start
!= round_down(*start
, extent_root
->sectorsize
));
1071 key
.offset
= *start
;
1072 dev_offset
= *start
;
1074 ret
= find_next_chunk(chunk_root
,
1075 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1081 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1085 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
1091 stripes
= &chunk
->stripe
;
1092 calc_size
= num_bytes
;
1095 cur
= dev_list
->next
;
1096 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1098 while (index
< num_stripes
) {
1099 struct btrfs_stripe
*stripe
;
1101 ret
= btrfs_alloc_dev_extent(trans
, device
,
1102 info
->chunk_root
->root_key
.objectid
,
1103 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1104 calc_size
, &dev_offset
, convert
);
1107 device
->bytes_used
+= calc_size
;
1108 ret
= btrfs_update_device(trans
, device
);
1111 map
->stripes
[index
].dev
= device
;
1112 map
->stripes
[index
].physical
= dev_offset
;
1113 stripe
= stripes
+ index
;
1114 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1115 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1116 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1120 /* key was set above */
1121 btrfs_set_stack_chunk_length(chunk
, num_bytes
);
1122 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1123 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1124 btrfs_set_stack_chunk_type(chunk
, type
);
1125 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1126 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1127 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1128 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1129 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1130 map
->sector_size
= extent_root
->sectorsize
;
1131 map
->stripe_len
= stripe_len
;
1132 map
->io_align
= stripe_len
;
1133 map
->io_width
= stripe_len
;
1135 map
->num_stripes
= num_stripes
;
1136 map
->sub_stripes
= sub_stripes
;
1138 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1139 btrfs_chunk_item_size(num_stripes
));
1142 *start
= key
.offset
;
1144 map
->ce
.start
= key
.offset
;
1145 map
->ce
.size
= num_bytes
;
1147 ret
= insert_cache_extent(&info
->mapping_tree
.cache_tree
, &map
->ce
);
1154 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
1156 struct cache_extent
*ce
;
1157 struct map_lookup
*map
;
1160 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1162 fprintf(stderr
, "No mapping for %llu-%llu\n",
1163 (unsigned long long)logical
,
1164 (unsigned long long)logical
+len
);
1167 if (ce
->start
> logical
|| ce
->start
+ ce
->size
< logical
) {
1168 fprintf(stderr
, "Invalid mapping for %llu-%llu, got "
1169 "%llu-%llu\n", (unsigned long long)logical
,
1170 (unsigned long long)logical
+len
,
1171 (unsigned long long)ce
->start
,
1172 (unsigned long long)ce
->start
+ ce
->size
);
1175 map
= container_of(ce
, struct map_lookup
, ce
);
1177 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
1178 ret
= map
->num_stripes
;
1179 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1180 ret
= map
->sub_stripes
;
1181 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
1183 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
1190 int btrfs_next_bg(struct btrfs_mapping_tree
*map_tree
, u64
*logical
,
1191 u64
*size
, u64 type
)
1193 struct cache_extent
*ce
;
1194 struct map_lookup
*map
;
1197 ce
= search_cache_extent(&map_tree
->cache_tree
, cur
);
1201 * only jump to next bg if our cur is not 0
1202 * As the initial logical for btrfs_next_bg() is 0, and
1203 * if we jump to next bg, we skipped a valid bg.
1206 ce
= next_cache_extent(ce
);
1212 map
= container_of(ce
, struct map_lookup
, ce
);
1213 if (map
->type
& type
) {
1214 *logical
= ce
->start
;
1223 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
1224 u64 chunk_start
, u64 physical
, u64 devid
,
1225 u64
**logical
, int *naddrs
, int *stripe_len
)
1227 struct cache_extent
*ce
;
1228 struct map_lookup
*map
;
1236 ce
= search_cache_extent(&map_tree
->cache_tree
, chunk_start
);
1238 map
= container_of(ce
, struct map_lookup
, ce
);
1241 rmap_len
= map
->stripe_len
;
1242 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1243 length
= ce
->size
/ (map
->num_stripes
/ map
->sub_stripes
);
1244 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
1245 length
= ce
->size
/ map
->num_stripes
;
1246 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
1247 BTRFS_BLOCK_GROUP_RAID6
)) {
1248 length
= ce
->size
/ nr_data_stripes(map
);
1249 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
1252 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
1254 for (i
= 0; i
< map
->num_stripes
; i
++) {
1255 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
1257 if (map
->stripes
[i
].physical
> physical
||
1258 map
->stripes
[i
].physical
+ length
<= physical
)
1261 stripe_nr
= (physical
- map
->stripes
[i
].physical
) /
1264 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1265 stripe_nr
= (stripe_nr
* map
->num_stripes
+ i
) /
1267 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
1268 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
1269 } /* else if RAID[56], multiply by nr_data_stripes().
1270 * Alternatively, just use rmap_len below instead of
1271 * map->stripe_len */
1273 bytenr
= ce
->start
+ stripe_nr
* rmap_len
;
1274 for (j
= 0; j
< nr
; j
++) {
1275 if (buf
[j
] == bytenr
)
1284 *stripe_len
= rmap_len
;
1289 static inline int parity_smaller(u64 a
, u64 b
)
1294 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1295 static void sort_parity_stripes(struct btrfs_multi_bio
*bbio
, u64
*raid_map
)
1297 struct btrfs_bio_stripe s
;
1304 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
1305 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
1306 s
= bbio
->stripes
[i
];
1308 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
1309 raid_map
[i
] = raid_map
[i
+1];
1310 bbio
->stripes
[i
+1] = s
;
1318 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1319 u64 logical
, u64
*length
,
1320 struct btrfs_multi_bio
**multi_ret
, int mirror_num
,
1323 return __btrfs_map_block(map_tree
, rw
, logical
, length
, NULL
,
1324 multi_ret
, mirror_num
, raid_map_ret
);
1327 int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1328 u64 logical
, u64
*length
, u64
*type
,
1329 struct btrfs_multi_bio
**multi_ret
, int mirror_num
,
1332 struct cache_extent
*ce
;
1333 struct map_lookup
*map
;
1337 u64
*raid_map
= NULL
;
1338 int stripes_allocated
= 8;
1339 int stripes_required
= 1;
1342 struct btrfs_multi_bio
*multi
= NULL
;
1344 if (multi_ret
&& rw
== READ
) {
1345 stripes_allocated
= 1;
1348 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1354 if (ce
->start
> logical
) {
1356 *length
= ce
->start
- logical
;
1361 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
1366 map
= container_of(ce
, struct map_lookup
, ce
);
1367 offset
= logical
- ce
->start
;
1370 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
1371 BTRFS_BLOCK_GROUP_DUP
)) {
1372 stripes_required
= map
->num_stripes
;
1373 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1374 stripes_required
= map
->sub_stripes
;
1377 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)
1378 && multi_ret
&& ((rw
& WRITE
) || mirror_num
> 1) && raid_map_ret
) {
1379 /* RAID[56] write or recovery. Return all stripes */
1380 stripes_required
= map
->num_stripes
;
1382 /* Only allocate the map if we've already got a large enough multi_ret */
1383 if (stripes_allocated
>= stripes_required
) {
1384 raid_map
= kmalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
1392 /* if our multi bio struct is too small, back off and try again */
1393 if (multi_ret
&& stripes_allocated
< stripes_required
) {
1394 stripes_allocated
= stripes_required
;
1401 * stripe_nr counts the total number of stripes we have to stride
1402 * to get to this block
1404 stripe_nr
= stripe_nr
/ map
->stripe_len
;
1406 stripe_offset
= stripe_nr
* map
->stripe_len
;
1407 BUG_ON(offset
< stripe_offset
);
1409 /* stripe_offset is the offset of this block in its stripe*/
1410 stripe_offset
= offset
- stripe_offset
;
1412 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
1413 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
1414 BTRFS_BLOCK_GROUP_RAID10
|
1415 BTRFS_BLOCK_GROUP_DUP
)) {
1416 /* we limit the length of each bio to what fits in a stripe */
1417 *length
= min_t(u64
, ce
->size
- offset
,
1418 map
->stripe_len
- stripe_offset
);
1420 *length
= ce
->size
- offset
;
1426 multi
->num_stripes
= 1;
1428 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1430 multi
->num_stripes
= map
->num_stripes
;
1431 else if (mirror_num
)
1432 stripe_index
= mirror_num
- 1;
1434 stripe_index
= stripe_nr
% map
->num_stripes
;
1435 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1436 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1438 stripe_index
= stripe_nr
% factor
;
1439 stripe_index
*= map
->sub_stripes
;
1442 multi
->num_stripes
= map
->sub_stripes
;
1443 else if (mirror_num
)
1444 stripe_index
+= mirror_num
- 1;
1446 stripe_nr
= stripe_nr
/ factor
;
1447 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1449 multi
->num_stripes
= map
->num_stripes
;
1450 else if (mirror_num
)
1451 stripe_index
= mirror_num
- 1;
1452 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
1453 BTRFS_BLOCK_GROUP_RAID6
)) {
1458 u64 raid56_full_stripe_start
;
1459 u64 full_stripe_len
= nr_data_stripes(map
) * map
->stripe_len
;
1462 * align the start of our data stripe in the logical
1465 raid56_full_stripe_start
= offset
/ full_stripe_len
;
1466 raid56_full_stripe_start
*= full_stripe_len
;
1468 /* get the data stripe number */
1469 stripe_nr
= raid56_full_stripe_start
/ map
->stripe_len
;
1470 stripe_nr
= stripe_nr
/ nr_data_stripes(map
);
1472 /* Work out the disk rotation on this stripe-set */
1473 rot
= stripe_nr
% map
->num_stripes
;
1475 /* Fill in the logical address of each stripe */
1476 tmp
= stripe_nr
* nr_data_stripes(map
);
1478 for (i
= 0; i
< nr_data_stripes(map
); i
++)
1479 raid_map
[(i
+rot
) % map
->num_stripes
] =
1480 ce
->start
+ (tmp
+ i
) * map
->stripe_len
;
1482 raid_map
[(i
+rot
) % map
->num_stripes
] = BTRFS_RAID5_P_STRIPE
;
1483 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
1484 raid_map
[(i
+rot
+1) % map
->num_stripes
] = BTRFS_RAID6_Q_STRIPE
;
1486 *length
= map
->stripe_len
;
1489 multi
->num_stripes
= map
->num_stripes
;
1491 stripe_index
= stripe_nr
% nr_data_stripes(map
);
1492 stripe_nr
= stripe_nr
/ nr_data_stripes(map
);
1495 * Mirror #0 or #1 means the original data block.
1496 * Mirror #2 is RAID5 parity block.
1497 * Mirror #3 is RAID6 Q block.
1500 stripe_index
= nr_data_stripes(map
) + mirror_num
- 2;
1502 /* We distribute the parity blocks across stripes */
1503 stripe_index
= (stripe_nr
+ stripe_index
) % map
->num_stripes
;
1507 * after this do_div call, stripe_nr is the number of stripes
1508 * on this device we have to walk to find the data, and
1509 * stripe_index is the number of our device in the stripe array
1511 stripe_index
= stripe_nr
% map
->num_stripes
;
1512 stripe_nr
= stripe_nr
/ map
->num_stripes
;
1514 BUG_ON(stripe_index
>= map
->num_stripes
);
1516 for (i
= 0; i
< multi
->num_stripes
; i
++) {
1517 multi
->stripes
[i
].physical
=
1518 map
->stripes
[stripe_index
].physical
+ stripe_offset
+
1519 stripe_nr
* map
->stripe_len
;
1520 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
1529 sort_parity_stripes(multi
, raid_map
);
1530 *raid_map_ret
= raid_map
;
1536 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
1539 struct btrfs_device
*device
;
1540 struct btrfs_fs_devices
*cur_devices
;
1542 cur_devices
= root
->fs_info
->fs_devices
;
1543 while (cur_devices
) {
1545 (!memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
) ||
1546 root
->fs_info
->ignore_fsid_mismatch
)) {
1547 device
= __find_device(&cur_devices
->devices
,
1552 cur_devices
= cur_devices
->seed
;
1557 struct btrfs_device
*
1558 btrfs_find_device_by_devid(struct btrfs_fs_devices
*fs_devices
,
1559 u64 devid
, int instance
)
1561 struct list_head
*head
= &fs_devices
->devices
;
1562 struct btrfs_device
*dev
;
1565 list_for_each_entry(dev
, head
, dev_list
) {
1566 if (dev
->devid
== devid
&& num_found
++ == instance
)
1572 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
1574 struct cache_extent
*ce
;
1575 struct map_lookup
*map
;
1576 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1581 * During chunk recovering, we may fail to find block group's
1582 * corresponding chunk, we will rebuild it later
1584 ce
= search_cache_extent(&map_tree
->cache_tree
, chunk_offset
);
1585 if (!root
->fs_info
->is_chunk_recover
)
1590 map
= container_of(ce
, struct map_lookup
, ce
);
1591 for (i
= 0; i
< map
->num_stripes
; i
++) {
1592 if (!map
->stripes
[i
].dev
->writeable
) {
1601 static struct btrfs_device
*fill_missing_device(u64 devid
)
1603 struct btrfs_device
*device
;
1605 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1606 device
->devid
= devid
;
1612 * slot == -1: SYSTEM chunk
1613 * return -EIO on error, otherwise return 0
1615 static int btrfs_check_chunk_valid(struct btrfs_root
*root
,
1616 struct extent_buffer
*leaf
,
1617 struct btrfs_chunk
*chunk
,
1618 int slot
, u64 logical
)
1626 length
= btrfs_chunk_length(leaf
, chunk
);
1627 stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
1628 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
1629 sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
1630 type
= btrfs_chunk_type(leaf
, chunk
);
1633 * These valid checks may be insufficient to cover every corner cases.
1635 if (!IS_ALIGNED(logical
, root
->sectorsize
)) {
1636 error("invalid chunk logical %llu", logical
);
1639 if (btrfs_chunk_sector_size(leaf
, chunk
) != root
->sectorsize
) {
1640 error("invalid chunk sectorsize %llu",
1641 (unsigned long long)btrfs_chunk_sector_size(leaf
, chunk
));
1644 if (!length
|| !IS_ALIGNED(length
, root
->sectorsize
)) {
1645 error("invalid chunk length %llu", length
);
1648 if (stripe_len
!= BTRFS_STRIPE_LEN
) {
1649 error("invalid chunk stripe length: %llu", stripe_len
);
1652 /* Check on chunk item type */
1653 if (slot
== -1 && (type
& BTRFS_BLOCK_GROUP_SYSTEM
) == 0) {
1654 error("invalid chunk type %llu", type
);
1657 if (type
& ~(BTRFS_BLOCK_GROUP_TYPE_MASK
|
1658 BTRFS_BLOCK_GROUP_PROFILE_MASK
)) {
1659 error("unrecognized chunk type: %llu",
1660 ~(BTRFS_BLOCK_GROUP_TYPE_MASK
|
1661 BTRFS_BLOCK_GROUP_PROFILE_MASK
) & type
);
1665 * Btrfs_chunk contains at least one stripe, and for sys_chunk
1666 * it can't exceed the system chunk array size
1667 * For normal chunk, it should match its chunk item size.
1669 if (num_stripes
< 1 ||
1670 (slot
== -1 && sizeof(struct btrfs_stripe
) * num_stripes
>
1671 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) ||
1672 (slot
>= 0 && sizeof(struct btrfs_stripe
) * (num_stripes
- 1) >
1673 btrfs_item_size_nr(leaf
, slot
))) {
1674 error("invalid num_stripes: %u", num_stripes
);
1678 * Device number check against profile
1680 if ((type
& BTRFS_BLOCK_GROUP_RAID10
&& sub_stripes
== 0) ||
1681 (type
& BTRFS_BLOCK_GROUP_RAID1
&& num_stripes
< 1) ||
1682 (type
& BTRFS_BLOCK_GROUP_RAID5
&& num_stripes
< 2) ||
1683 (type
& BTRFS_BLOCK_GROUP_RAID6
&& num_stripes
< 3) ||
1684 (type
& BTRFS_BLOCK_GROUP_DUP
&& num_stripes
> 2) ||
1685 ((type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) == 0 &&
1686 num_stripes
!= 1)) {
1687 error("Invalid num_stripes:sub_stripes %u:%u for profile %llu",
1688 num_stripes
, sub_stripes
,
1689 type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
);
1697 * Slot is used to verify the chunk item is valid
1699 * For sys chunk in superblock, pass -1 to indicate sys chunk.
1701 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
1702 struct extent_buffer
*leaf
,
1703 struct btrfs_chunk
*chunk
, int slot
)
1705 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1706 struct map_lookup
*map
;
1707 struct cache_extent
*ce
;
1711 u8 uuid
[BTRFS_UUID_SIZE
];
1716 logical
= key
->offset
;
1717 length
= btrfs_chunk_length(leaf
, chunk
);
1718 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
1719 /* Validation check */
1720 ret
= btrfs_check_chunk_valid(root
, leaf
, chunk
, slot
, logical
);
1722 error("%s checksums match, but it has an invalid chunk, %s",
1723 (slot
== -1) ? "Superblock" : "Metadata",
1724 (slot
== -1) ? "try btrfsck --repair -s <superblock> ie, 0,1,2" : "");
1728 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1730 /* already mapped? */
1731 if (ce
&& ce
->start
<= logical
&& ce
->start
+ ce
->size
> logical
) {
1735 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
1739 map
->ce
.start
= logical
;
1740 map
->ce
.size
= length
;
1741 map
->num_stripes
= num_stripes
;
1742 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
1743 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
1744 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
1745 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
1746 map
->type
= btrfs_chunk_type(leaf
, chunk
);
1747 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
1749 for (i
= 0; i
< num_stripes
; i
++) {
1750 map
->stripes
[i
].physical
=
1751 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
1752 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
1753 read_extent_buffer(leaf
, uuid
, (unsigned long)
1754 btrfs_stripe_dev_uuid_nr(chunk
, i
),
1756 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
1758 if (!map
->stripes
[i
].dev
) {
1759 map
->stripes
[i
].dev
= fill_missing_device(devid
);
1760 printf("warning, device %llu is missing\n",
1761 (unsigned long long)devid
);
1765 ret
= insert_cache_extent(&map_tree
->cache_tree
, &map
->ce
);
1771 static int fill_device_from_item(struct extent_buffer
*leaf
,
1772 struct btrfs_dev_item
*dev_item
,
1773 struct btrfs_device
*device
)
1777 device
->devid
= btrfs_device_id(leaf
, dev_item
);
1778 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
1779 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
1780 device
->type
= btrfs_device_type(leaf
, dev_item
);
1781 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
1782 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
1783 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
1785 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1786 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1791 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
1793 struct btrfs_fs_devices
*fs_devices
;
1796 fs_devices
= root
->fs_info
->fs_devices
->seed
;
1797 while (fs_devices
) {
1798 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
1802 fs_devices
= fs_devices
->seed
;
1805 fs_devices
= find_fsid(fsid
);
1807 /* missing all seed devices */
1808 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1813 INIT_LIST_HEAD(&fs_devices
->devices
);
1814 list_add(&fs_devices
->list
, &fs_uuids
);
1815 memcpy(fs_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
);
1818 ret
= btrfs_open_devices(fs_devices
, O_RDONLY
);
1822 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
1823 root
->fs_info
->fs_devices
->seed
= fs_devices
;
1828 static int read_one_dev(struct btrfs_root
*root
,
1829 struct extent_buffer
*leaf
,
1830 struct btrfs_dev_item
*dev_item
)
1832 struct btrfs_device
*device
;
1835 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1836 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1838 devid
= btrfs_device_id(leaf
, dev_item
);
1839 read_extent_buffer(leaf
, dev_uuid
,
1840 (unsigned long)btrfs_device_uuid(dev_item
),
1842 read_extent_buffer(leaf
, fs_uuid
,
1843 (unsigned long)btrfs_device_fsid(dev_item
),
1846 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
1847 ret
= open_seed_devices(root
, fs_uuid
);
1852 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1854 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1858 list_add(&device
->dev_list
,
1859 &root
->fs_info
->fs_devices
->devices
);
1862 fill_device_from_item(leaf
, dev_item
, device
);
1863 device
->dev_root
= root
->fs_info
->dev_root
;
1867 int btrfs_read_sys_array(struct btrfs_root
*root
)
1869 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1870 struct extent_buffer
*sb
;
1871 struct btrfs_disk_key
*disk_key
;
1872 struct btrfs_chunk
*chunk
;
1874 unsigned long sb_array_offset
;
1880 struct btrfs_key key
;
1882 sb
= btrfs_find_create_tree_block(root
->fs_info
,
1883 BTRFS_SUPER_INFO_OFFSET
,
1884 BTRFS_SUPER_INFO_SIZE
);
1887 btrfs_set_buffer_uptodate(sb
);
1888 write_extent_buffer(sb
, super_copy
, 0, sizeof(*super_copy
));
1889 array_size
= btrfs_super_sys_array_size(super_copy
);
1891 array_ptr
= super_copy
->sys_chunk_array
;
1892 sb_array_offset
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
1895 while (cur_offset
< array_size
) {
1896 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
1897 len
= sizeof(*disk_key
);
1898 if (cur_offset
+ len
> array_size
)
1899 goto out_short_read
;
1901 btrfs_disk_key_to_cpu(&key
, disk_key
);
1904 sb_array_offset
+= len
;
1907 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1908 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
1910 * At least one btrfs_chunk with one stripe must be
1911 * present, exact stripe count check comes afterwards
1913 len
= btrfs_chunk_item_size(1);
1914 if (cur_offset
+ len
> array_size
)
1915 goto out_short_read
;
1917 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
1920 "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
1921 num_stripes
, cur_offset
);
1926 len
= btrfs_chunk_item_size(num_stripes
);
1927 if (cur_offset
+ len
> array_size
)
1928 goto out_short_read
;
1930 ret
= read_one_chunk(root
, &key
, sb
, chunk
, -1);
1935 "ERROR: unexpected item type %u in sys_array at offset %u\n",
1936 (u32
)key
.type
, cur_offset
);
1941 sb_array_offset
+= len
;
1944 free_extent_buffer(sb
);
1948 printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
1950 free_extent_buffer(sb
);
1954 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
1956 struct btrfs_path
*path
;
1957 struct extent_buffer
*leaf
;
1958 struct btrfs_key key
;
1959 struct btrfs_key found_key
;
1963 root
= root
->fs_info
->chunk_root
;
1965 path
= btrfs_alloc_path();
1970 * Read all device items, and then all the chunk items. All
1971 * device items are found before any chunk item (their object id
1972 * is smaller than the lowest possible object id for a chunk
1973 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
1975 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1978 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1982 leaf
= path
->nodes
[0];
1983 slot
= path
->slots
[0];
1984 if (slot
>= btrfs_header_nritems(leaf
)) {
1985 ret
= btrfs_next_leaf(root
, path
);
1992 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1993 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
1994 struct btrfs_dev_item
*dev_item
;
1995 dev_item
= btrfs_item_ptr(leaf
, slot
,
1996 struct btrfs_dev_item
);
1997 ret
= read_one_dev(root
, leaf
, dev_item
);
1999 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2000 struct btrfs_chunk
*chunk
;
2001 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2002 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
,
2011 btrfs_free_path(path
);
2015 struct list_head
*btrfs_scanned_uuids(void)
2020 static int rmw_eb(struct btrfs_fs_info
*info
,
2021 struct extent_buffer
*eb
, struct extent_buffer
*orig_eb
)
2024 unsigned long orig_off
= 0;
2025 unsigned long dest_off
= 0;
2026 unsigned long copy_len
= eb
->len
;
2028 ret
= read_whole_eb(info
, eb
, 0);
2032 if (eb
->start
+ eb
->len
<= orig_eb
->start
||
2033 eb
->start
>= orig_eb
->start
+ orig_eb
->len
)
2036 * | ----- orig_eb ------- |
2037 * | ----- stripe ------- |
2038 * | ----- orig_eb ------- |
2039 * | ----- orig_eb ------- |
2041 if (eb
->start
> orig_eb
->start
)
2042 orig_off
= eb
->start
- orig_eb
->start
;
2043 if (orig_eb
->start
> eb
->start
)
2044 dest_off
= orig_eb
->start
- eb
->start
;
2046 if (copy_len
> orig_eb
->len
- orig_off
)
2047 copy_len
= orig_eb
->len
- orig_off
;
2048 if (copy_len
> eb
->len
- dest_off
)
2049 copy_len
= eb
->len
- dest_off
;
2051 memcpy(eb
->data
+ dest_off
, orig_eb
->data
+ orig_off
, copy_len
);
2055 static void split_eb_for_raid56(struct btrfs_fs_info
*info
,
2056 struct extent_buffer
*orig_eb
,
2057 struct extent_buffer
**ebs
,
2058 u64 stripe_len
, u64
*raid_map
,
2061 struct extent_buffer
*eb
;
2062 u64 start
= orig_eb
->start
;
2067 for (i
= 0; i
< num_stripes
; i
++) {
2068 if (raid_map
[i
] >= BTRFS_RAID5_P_STRIPE
)
2071 eb
= calloc(1, sizeof(struct extent_buffer
) + stripe_len
);
2075 eb
->start
= raid_map
[i
];
2076 eb
->len
= stripe_len
;
2080 eb
->dev_bytenr
= (u64
)-1;
2082 this_eb_start
= raid_map
[i
];
2084 if (start
> this_eb_start
||
2085 start
+ orig_eb
->len
< this_eb_start
+ stripe_len
) {
2086 ret
= rmw_eb(info
, eb
, orig_eb
);
2089 memcpy(eb
->data
, orig_eb
->data
+ eb
->start
- start
, stripe_len
);
2095 int write_raid56_with_parity(struct btrfs_fs_info
*info
,
2096 struct extent_buffer
*eb
,
2097 struct btrfs_multi_bio
*multi
,
2098 u64 stripe_len
, u64
*raid_map
)
2100 struct extent_buffer
**ebs
, *p_eb
= NULL
, *q_eb
= NULL
;
2104 int alloc_size
= eb
->len
;
2106 ebs
= kmalloc(sizeof(*ebs
) * multi
->num_stripes
, GFP_NOFS
);
2109 if (stripe_len
> alloc_size
)
2110 alloc_size
= stripe_len
;
2112 split_eb_for_raid56(info
, eb
, ebs
, stripe_len
, raid_map
,
2113 multi
->num_stripes
);
2115 for (i
= 0; i
< multi
->num_stripes
; i
++) {
2116 struct extent_buffer
*new_eb
;
2117 if (raid_map
[i
] < BTRFS_RAID5_P_STRIPE
) {
2118 ebs
[i
]->dev_bytenr
= multi
->stripes
[i
].physical
;
2119 ebs
[i
]->fd
= multi
->stripes
[i
].dev
->fd
;
2120 multi
->stripes
[i
].dev
->total_ios
++;
2121 BUG_ON(ebs
[i
]->start
!= raid_map
[i
]);
2124 new_eb
= kmalloc(sizeof(*eb
) + alloc_size
, GFP_NOFS
);
2126 new_eb
->dev_bytenr
= multi
->stripes
[i
].physical
;
2127 new_eb
->fd
= multi
->stripes
[i
].dev
->fd
;
2128 multi
->stripes
[i
].dev
->total_ios
++;
2129 new_eb
->len
= stripe_len
;
2131 if (raid_map
[i
] == BTRFS_RAID5_P_STRIPE
)
2133 else if (raid_map
[i
] == BTRFS_RAID6_Q_STRIPE
)
2139 pointers
= kmalloc(sizeof(*pointers
) * multi
->num_stripes
,
2143 ebs
[multi
->num_stripes
- 2] = p_eb
;
2144 ebs
[multi
->num_stripes
- 1] = q_eb
;
2146 for (i
= 0; i
< multi
->num_stripes
; i
++)
2147 pointers
[i
] = ebs
[i
]->data
;
2149 raid6_gen_syndrome(multi
->num_stripes
, stripe_len
, pointers
);
2152 ebs
[multi
->num_stripes
- 1] = p_eb
;
2153 memcpy(p_eb
->data
, ebs
[0]->data
, stripe_len
);
2154 for (j
= 1; j
< multi
->num_stripes
- 1; j
++) {
2155 for (i
= 0; i
< stripe_len
; i
+= sizeof(unsigned long)) {
2156 *(unsigned long *)(p_eb
->data
+ i
) ^=
2157 *(unsigned long *)(ebs
[j
]->data
+ i
);
2162 for (i
= 0; i
< multi
->num_stripes
; i
++) {
2163 ret
= write_extent_to_disk(ebs
[i
]);