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
;
168 while (!list_empty(&fs_devices
->devices
)) {
169 device
= list_entry(fs_devices
->devices
.next
,
170 struct btrfs_device
, dev_list
);
171 if (device
->fd
!= -1) {
172 if (fsync(device
->fd
) == -1) {
173 warning("fsync on device %llu failed: %s",
174 device
->devid
, strerror(errno
));
177 if (posix_fadvise(device
->fd
, 0, 0, POSIX_FADV_DONTNEED
))
178 fprintf(stderr
, "Warning, could not drop caches\n");
182 device
->writeable
= 0;
183 list_del(&device
->dev_list
);
184 /* free the memory */
190 seed_devices
= fs_devices
->seed
;
191 fs_devices
->seed
= NULL
;
193 struct btrfs_fs_devices
*orig
;
196 fs_devices
= seed_devices
;
197 list_del(&orig
->list
);
201 list_del(&fs_devices
->list
);
208 void btrfs_close_all_devices(void)
210 struct btrfs_fs_devices
*fs_devices
;
212 while (!list_empty(&fs_uuids
)) {
213 fs_devices
= list_entry(fs_uuids
.next
, struct btrfs_fs_devices
,
215 btrfs_close_devices(fs_devices
);
219 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
, int flags
)
222 struct list_head
*head
= &fs_devices
->devices
;
223 struct list_head
*cur
;
224 struct btrfs_device
*device
;
227 list_for_each(cur
, head
) {
228 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
230 printk("no name for device %llu, skip it now\n", device
->devid
);
234 fd
= open(device
->name
, flags
);
237 error("cannot open device '%s': %s", device
->name
,
242 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_DONTNEED
))
243 fprintf(stderr
, "Warning, could not drop caches\n");
245 if (device
->devid
== fs_devices
->latest_devid
)
246 fs_devices
->latest_bdev
= fd
;
247 if (device
->devid
== fs_devices
->lowest_devid
)
248 fs_devices
->lowest_bdev
= fd
;
251 device
->writeable
= 1;
255 btrfs_close_devices(fs_devices
);
259 int btrfs_scan_one_device(int fd
, const char *path
,
260 struct btrfs_fs_devices
**fs_devices_ret
,
261 u64
*total_devs
, u64 super_offset
, unsigned sbflags
)
263 struct btrfs_super_block
*disk_super
;
264 char buf
[BTRFS_SUPER_INFO_SIZE
];
268 disk_super
= (struct btrfs_super_block
*)buf
;
269 ret
= btrfs_read_dev_super(fd
, disk_super
, super_offset
, sbflags
);
272 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
273 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_METADUMP
)
276 *total_devs
= btrfs_super_num_devices(disk_super
);
278 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
284 * find_free_dev_extent_start - find free space in the specified device
285 * @device: the device which we search the free space in
286 * @num_bytes: the size of the free space that we need
287 * @search_start: the position from which to begin the search
288 * @start: store the start of the free space.
289 * @len: the size of the free space. that we find, or the size
290 * of the max free space if we don't find suitable free space
292 * this uses a pretty simple search, the expectation is that it is
293 * called very infrequently and that a given device has a small number
296 * @start is used to store the start of the free space if we find. But if we
297 * don't find suitable free space, it will be used to store the start position
298 * of the max free space.
300 * @len is used to store the size of the free space that we find.
301 * But if we don't find suitable free space, it is used to store the size of
302 * the max free space.
304 static int find_free_dev_extent_start(struct btrfs_trans_handle
*trans
,
305 struct btrfs_device
*device
, u64 num_bytes
,
306 u64 search_start
, u64
*start
, u64
*len
)
308 struct btrfs_key key
;
309 struct btrfs_root
*root
= device
->dev_root
;
310 struct btrfs_dev_extent
*dev_extent
;
311 struct btrfs_path
*path
;
316 u64 search_end
= device
->total_bytes
;
319 struct extent_buffer
*l
;
320 u64 min_search_start
;
323 * We don't want to overwrite the superblock on the drive nor any area
324 * used by the boot loader (grub for example), so we make sure to start
325 * at an offset of at least 1MB.
327 min_search_start
= max(root
->fs_info
->alloc_start
, (u64
)SZ_1M
);
328 search_start
= max(search_start
, min_search_start
);
330 path
= btrfs_alloc_path();
334 max_hole_start
= search_start
;
337 if (search_start
>= search_end
) {
344 key
.objectid
= device
->devid
;
345 key
.offset
= search_start
;
346 key
.type
= BTRFS_DEV_EXTENT_KEY
;
348 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
352 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
359 slot
= path
->slots
[0];
360 if (slot
>= btrfs_header_nritems(l
)) {
361 ret
= btrfs_next_leaf(root
, path
);
369 btrfs_item_key_to_cpu(l
, &key
, slot
);
371 if (key
.objectid
< device
->devid
)
374 if (key
.objectid
> device
->devid
)
377 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
380 if (key
.offset
> search_start
) {
381 hole_size
= key
.offset
- search_start
;
384 * Have to check before we set max_hole_start, otherwise
385 * we could end up sending back this offset anyway.
387 if (hole_size
> max_hole_size
) {
388 max_hole_start
= search_start
;
389 max_hole_size
= hole_size
;
393 * If this free space is greater than which we need,
394 * it must be the max free space that we have found
395 * until now, so max_hole_start must point to the start
396 * of this free space and the length of this free space
397 * is stored in max_hole_size. Thus, we return
398 * max_hole_start and max_hole_size and go back to the
401 if (hole_size
>= num_bytes
) {
407 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
408 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
410 if (extent_end
> search_start
)
411 search_start
= extent_end
;
418 * At this point, search_start should be the end of
419 * allocated dev extents, and when shrinking the device,
420 * search_end may be smaller than search_start.
422 if (search_end
> search_start
) {
423 hole_size
= search_end
- search_start
;
425 if (hole_size
> max_hole_size
) {
426 max_hole_start
= search_start
;
427 max_hole_size
= hole_size
;
432 if (max_hole_size
< num_bytes
)
438 btrfs_free_path(path
);
439 *start
= max_hole_start
;
441 *len
= max_hole_size
;
445 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
446 struct btrfs_device
*device
, u64 num_bytes
,
449 /* FIXME use last free of some kind */
450 return find_free_dev_extent_start(trans
, device
,
451 num_bytes
, 0, start
, NULL
);
454 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
455 struct btrfs_device
*device
,
456 u64 chunk_tree
, u64 chunk_objectid
,
458 u64 num_bytes
, u64
*start
, int convert
)
461 struct btrfs_path
*path
;
462 struct btrfs_root
*root
= device
->dev_root
;
463 struct btrfs_dev_extent
*extent
;
464 struct extent_buffer
*leaf
;
465 struct btrfs_key key
;
467 path
= btrfs_alloc_path();
472 * For convert case, just skip search free dev_extent, as caller
473 * is responsible to make sure it's free.
476 ret
= find_free_dev_extent(trans
, device
, num_bytes
,
482 key
.objectid
= device
->devid
;
484 key
.type
= BTRFS_DEV_EXTENT_KEY
;
485 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
489 leaf
= path
->nodes
[0];
490 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
491 struct btrfs_dev_extent
);
492 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
493 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
494 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
496 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
497 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
500 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
501 btrfs_mark_buffer_dirty(leaf
);
503 btrfs_free_path(path
);
507 static int find_next_chunk(struct btrfs_root
*root
, u64 objectid
, u64
*offset
)
509 struct btrfs_path
*path
;
511 struct btrfs_key key
;
512 struct btrfs_chunk
*chunk
;
513 struct btrfs_key found_key
;
515 path
= btrfs_alloc_path();
519 key
.objectid
= objectid
;
520 key
.offset
= (u64
)-1;
521 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
523 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
529 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
533 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
535 if (found_key
.objectid
!= objectid
)
538 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
540 *offset
= found_key
.offset
+
541 btrfs_chunk_length(path
->nodes
[0], chunk
);
546 btrfs_free_path(path
);
550 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
554 struct btrfs_key key
;
555 struct btrfs_key found_key
;
557 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
558 key
.type
= BTRFS_DEV_ITEM_KEY
;
559 key
.offset
= (u64
)-1;
561 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
567 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
572 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
574 *objectid
= found_key
.offset
+ 1;
578 btrfs_release_path(path
);
583 * the device information is stored in the chunk root
584 * the btrfs_device struct should be fully filled in
586 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
587 struct btrfs_root
*root
,
588 struct btrfs_device
*device
)
591 struct btrfs_path
*path
;
592 struct btrfs_dev_item
*dev_item
;
593 struct extent_buffer
*leaf
;
594 struct btrfs_key key
;
598 root
= root
->fs_info
->chunk_root
;
600 path
= btrfs_alloc_path();
604 ret
= find_next_devid(root
, path
, &free_devid
);
608 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
609 key
.type
= BTRFS_DEV_ITEM_KEY
;
610 key
.offset
= free_devid
;
612 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
617 leaf
= path
->nodes
[0];
618 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
620 device
->devid
= free_devid
;
621 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
622 btrfs_set_device_generation(leaf
, dev_item
, 0);
623 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
624 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
625 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
626 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
627 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
628 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
629 btrfs_set_device_group(leaf
, dev_item
, 0);
630 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
631 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
632 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
634 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
635 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
636 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
637 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
638 btrfs_mark_buffer_dirty(leaf
);
642 btrfs_free_path(path
);
646 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
647 struct btrfs_device
*device
)
650 struct btrfs_path
*path
;
651 struct btrfs_root
*root
;
652 struct btrfs_dev_item
*dev_item
;
653 struct extent_buffer
*leaf
;
654 struct btrfs_key key
;
656 root
= device
->dev_root
->fs_info
->chunk_root
;
658 path
= btrfs_alloc_path();
662 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
663 key
.type
= BTRFS_DEV_ITEM_KEY
;
664 key
.offset
= device
->devid
;
666 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
675 leaf
= path
->nodes
[0];
676 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
678 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
679 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
680 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
681 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
682 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
683 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
684 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
685 btrfs_mark_buffer_dirty(leaf
);
688 btrfs_free_path(path
);
692 int btrfs_add_system_chunk(struct btrfs_root
*root
,
693 struct btrfs_key
*key
,
694 struct btrfs_chunk
*chunk
, int item_size
)
696 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
697 struct btrfs_disk_key disk_key
;
701 array_size
= btrfs_super_sys_array_size(super_copy
);
702 if (array_size
+ item_size
+ sizeof(disk_key
)
703 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
706 ptr
= super_copy
->sys_chunk_array
+ array_size
;
707 btrfs_cpu_key_to_disk(&disk_key
, key
);
708 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
709 ptr
+= sizeof(disk_key
);
710 memcpy(ptr
, chunk
, item_size
);
711 item_size
+= sizeof(disk_key
);
712 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
716 static u64
chunk_bytes_by_type(u64 type
, u64 calc_size
, int num_stripes
,
719 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
721 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
722 return calc_size
* (num_stripes
/ sub_stripes
);
723 else if (type
& BTRFS_BLOCK_GROUP_RAID5
)
724 return calc_size
* (num_stripes
- 1);
725 else if (type
& BTRFS_BLOCK_GROUP_RAID6
)
726 return calc_size
* (num_stripes
- 2);
728 return calc_size
* num_stripes
;
732 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
734 /* TODO, add a way to store the preferred stripe size */
735 return BTRFS_STRIPE_LEN
;
739 * btrfs_device_avail_bytes - count bytes available for alloc_chunk
741 * It is not equal to "device->total_bytes - device->bytes_used".
742 * We do not allocate any chunk in 1M at beginning of device, and not
743 * allowed to allocate any chunk before alloc_start if it is specified.
744 * So search holes from max(1M, alloc_start) to device->total_bytes.
746 static int btrfs_device_avail_bytes(struct btrfs_trans_handle
*trans
,
747 struct btrfs_device
*device
,
750 struct btrfs_path
*path
;
751 struct btrfs_root
*root
= device
->dev_root
;
752 struct btrfs_key key
;
753 struct btrfs_dev_extent
*dev_extent
= NULL
;
754 struct extent_buffer
*l
;
755 u64 search_start
= root
->fs_info
->alloc_start
;
756 u64 search_end
= device
->total_bytes
;
762 search_start
= max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
, search_start
);
764 path
= btrfs_alloc_path();
768 key
.objectid
= device
->devid
;
769 key
.offset
= root
->fs_info
->alloc_start
;
770 key
.type
= BTRFS_DEV_EXTENT_KEY
;
773 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
776 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
782 slot
= path
->slots
[0];
783 if (slot
>= btrfs_header_nritems(l
)) {
784 ret
= btrfs_next_leaf(root
, path
);
791 btrfs_item_key_to_cpu(l
, &key
, slot
);
793 if (key
.objectid
< device
->devid
)
795 if (key
.objectid
> device
->devid
)
797 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
799 if (key
.offset
> search_end
)
801 if (key
.offset
> search_start
)
802 free_bytes
+= key
.offset
- search_start
;
804 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
805 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
807 if (extent_end
> search_start
)
808 search_start
= extent_end
;
809 if (search_start
> search_end
)
816 if (search_start
< search_end
)
817 free_bytes
+= search_end
- search_start
;
819 *avail_bytes
= free_bytes
;
822 btrfs_free_path(path
);
826 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
827 - sizeof(struct btrfs_item) \
828 - sizeof(struct btrfs_chunk)) \
829 / sizeof(struct btrfs_stripe) + 1)
831 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
832 - 2 * sizeof(struct btrfs_disk_key) \
833 - 2 * sizeof(struct btrfs_chunk)) \
834 / sizeof(struct btrfs_stripe) + 1)
836 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
837 struct btrfs_root
*extent_root
, u64
*start
,
838 u64
*num_bytes
, u64 type
)
841 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
842 struct btrfs_root
*chunk_root
= info
->chunk_root
;
843 struct btrfs_stripe
*stripes
;
844 struct btrfs_device
*device
= NULL
;
845 struct btrfs_chunk
*chunk
;
846 struct list_head private_devs
;
847 struct list_head
*dev_list
= &info
->fs_devices
->devices
;
848 struct list_head
*cur
;
849 struct map_lookup
*map
;
850 int min_stripe_size
= SZ_1M
;
851 u64 calc_size
= SZ_8M
;
853 u64 max_chunk_size
= 4 * calc_size
;
864 int stripe_len
= BTRFS_STRIPE_LEN
;
865 struct btrfs_key key
;
868 if (list_empty(dev_list
)) {
872 if (type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
873 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
874 BTRFS_BLOCK_GROUP_RAID10
|
875 BTRFS_BLOCK_GROUP_DUP
)) {
876 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
878 max_chunk_size
= calc_size
* 2;
879 min_stripe_size
= SZ_1M
;
880 max_stripes
= BTRFS_MAX_DEVS_SYS_CHUNK
;
881 } else if (type
& BTRFS_BLOCK_GROUP_DATA
) {
883 max_chunk_size
= 10 * calc_size
;
884 min_stripe_size
= SZ_64M
;
885 max_stripes
= BTRFS_MAX_DEVS(chunk_root
);
886 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
888 max_chunk_size
= 4 * calc_size
;
889 min_stripe_size
= SZ_32M
;
890 max_stripes
= BTRFS_MAX_DEVS(chunk_root
);
893 if (type
& BTRFS_BLOCK_GROUP_RAID1
) {
894 num_stripes
= min_t(u64
, 2,
895 btrfs_super_num_devices(info
->super_copy
));
900 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
904 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
905 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
906 if (num_stripes
> max_stripes
)
907 num_stripes
= max_stripes
;
910 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
911 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
912 if (num_stripes
> max_stripes
)
913 num_stripes
= max_stripes
;
916 num_stripes
&= ~(u32
)1;
920 if (type
& (BTRFS_BLOCK_GROUP_RAID5
)) {
921 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
922 if (num_stripes
> max_stripes
)
923 num_stripes
= max_stripes
;
927 stripe_len
= find_raid56_stripe_len(num_stripes
- 1,
928 btrfs_super_stripesize(info
->super_copy
));
930 if (type
& (BTRFS_BLOCK_GROUP_RAID6
)) {
931 num_stripes
= btrfs_super_num_devices(info
->super_copy
);
932 if (num_stripes
> max_stripes
)
933 num_stripes
= max_stripes
;
937 stripe_len
= find_raid56_stripe_len(num_stripes
- 2,
938 btrfs_super_stripesize(info
->super_copy
));
941 /* we don't want a chunk larger than 10% of the FS */
942 percent_max
= div_factor(btrfs_super_total_bytes(info
->super_copy
), 1);
943 max_chunk_size
= min(percent_max
, max_chunk_size
);
946 if (chunk_bytes_by_type(type
, calc_size
, num_stripes
, sub_stripes
) >
948 calc_size
= max_chunk_size
;
949 calc_size
/= num_stripes
;
950 calc_size
/= stripe_len
;
951 calc_size
*= stripe_len
;
953 /* we don't want tiny stripes */
954 calc_size
= max_t(u64
, calc_size
, min_stripe_size
);
956 calc_size
/= stripe_len
;
957 calc_size
*= stripe_len
;
958 INIT_LIST_HEAD(&private_devs
);
959 cur
= dev_list
->next
;
962 if (type
& BTRFS_BLOCK_GROUP_DUP
)
963 min_free
= calc_size
* 2;
965 min_free
= calc_size
;
967 /* build a private list of devices we will allocate from */
968 while(index
< num_stripes
) {
969 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
970 ret
= btrfs_device_avail_bytes(trans
, device
, &avail
);
974 if (avail
>= min_free
) {
975 list_move_tail(&device
->dev_list
, &private_devs
);
977 if (type
& BTRFS_BLOCK_GROUP_DUP
)
979 } else if (avail
> max_avail
)
984 if (index
< num_stripes
) {
985 list_splice(&private_devs
, dev_list
);
986 if (index
>= min_stripes
) {
988 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
989 num_stripes
/= sub_stripes
;
990 num_stripes
*= sub_stripes
;
995 if (!looped
&& max_avail
> 0) {
997 calc_size
= max_avail
;
1002 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1006 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1007 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1008 key
.offset
= offset
;
1010 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1014 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
1020 stripes
= &chunk
->stripe
;
1021 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
1022 num_stripes
, sub_stripes
);
1024 while(index
< num_stripes
) {
1025 struct btrfs_stripe
*stripe
;
1026 BUG_ON(list_empty(&private_devs
));
1027 cur
= private_devs
.next
;
1028 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1030 /* loop over this device again if we're doing a dup group */
1031 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
1032 (index
== num_stripes
- 1))
1033 list_move_tail(&device
->dev_list
, dev_list
);
1035 ret
= btrfs_alloc_dev_extent(trans
, device
,
1036 info
->chunk_root
->root_key
.objectid
,
1037 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1038 calc_size
, &dev_offset
, 0);
1041 device
->bytes_used
+= calc_size
;
1042 ret
= btrfs_update_device(trans
, device
);
1045 map
->stripes
[index
].dev
= device
;
1046 map
->stripes
[index
].physical
= dev_offset
;
1047 stripe
= stripes
+ index
;
1048 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1049 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1050 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1053 BUG_ON(!list_empty(&private_devs
));
1055 /* key was set above */
1056 btrfs_set_stack_chunk_length(chunk
, *num_bytes
);
1057 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1058 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1059 btrfs_set_stack_chunk_type(chunk
, type
);
1060 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1061 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1062 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1063 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1064 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1065 map
->sector_size
= extent_root
->sectorsize
;
1066 map
->stripe_len
= stripe_len
;
1067 map
->io_align
= stripe_len
;
1068 map
->io_width
= stripe_len
;
1070 map
->num_stripes
= num_stripes
;
1071 map
->sub_stripes
= sub_stripes
;
1073 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1074 btrfs_chunk_item_size(num_stripes
));
1076 *start
= key
.offset
;;
1078 map
->ce
.start
= key
.offset
;
1079 map
->ce
.size
= *num_bytes
;
1081 ret
= insert_cache_extent(&info
->mapping_tree
.cache_tree
, &map
->ce
);
1084 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1085 ret
= btrfs_add_system_chunk(chunk_root
, &key
,
1086 chunk
, btrfs_chunk_item_size(num_stripes
));
1095 * Alloc a DATA chunk with SINGLE profile.
1097 * If 'convert' is set, it will alloc a chunk with 1:1 mapping
1098 * (btrfs logical bytenr == on-disk bytenr)
1099 * For that case, caller must make sure the chunk and dev_extent are not
1102 int btrfs_alloc_data_chunk(struct btrfs_trans_handle
*trans
,
1103 struct btrfs_root
*extent_root
, u64
*start
,
1104 u64 num_bytes
, u64 type
, int convert
)
1107 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1108 struct btrfs_root
*chunk_root
= info
->chunk_root
;
1109 struct btrfs_stripe
*stripes
;
1110 struct btrfs_device
*device
= NULL
;
1111 struct btrfs_chunk
*chunk
;
1112 struct list_head
*dev_list
= &info
->fs_devices
->devices
;
1113 struct list_head
*cur
;
1114 struct map_lookup
*map
;
1115 u64 calc_size
= SZ_8M
;
1116 int num_stripes
= 1;
1117 int sub_stripes
= 0;
1120 int stripe_len
= BTRFS_STRIPE_LEN
;
1121 struct btrfs_key key
;
1123 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1124 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1126 if (*start
!= round_down(*start
, extent_root
->sectorsize
)) {
1127 error("DATA chunk start not sectorsize aligned: %llu",
1128 (unsigned long long)*start
);
1131 key
.offset
= *start
;
1132 dev_offset
= *start
;
1136 ret
= find_next_chunk(chunk_root
,
1137 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1144 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1148 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
1154 stripes
= &chunk
->stripe
;
1155 calc_size
= num_bytes
;
1158 cur
= dev_list
->next
;
1159 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1161 while (index
< num_stripes
) {
1162 struct btrfs_stripe
*stripe
;
1164 ret
= btrfs_alloc_dev_extent(trans
, device
,
1165 info
->chunk_root
->root_key
.objectid
,
1166 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1167 calc_size
, &dev_offset
, convert
);
1170 device
->bytes_used
+= calc_size
;
1171 ret
= btrfs_update_device(trans
, device
);
1174 map
->stripes
[index
].dev
= device
;
1175 map
->stripes
[index
].physical
= dev_offset
;
1176 stripe
= stripes
+ index
;
1177 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1178 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1179 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1183 /* key was set above */
1184 btrfs_set_stack_chunk_length(chunk
, num_bytes
);
1185 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1186 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1187 btrfs_set_stack_chunk_type(chunk
, type
);
1188 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1189 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1190 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1191 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1192 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1193 map
->sector_size
= extent_root
->sectorsize
;
1194 map
->stripe_len
= stripe_len
;
1195 map
->io_align
= stripe_len
;
1196 map
->io_width
= stripe_len
;
1198 map
->num_stripes
= num_stripes
;
1199 map
->sub_stripes
= sub_stripes
;
1201 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1202 btrfs_chunk_item_size(num_stripes
));
1205 *start
= key
.offset
;
1207 map
->ce
.start
= key
.offset
;
1208 map
->ce
.size
= num_bytes
;
1210 ret
= insert_cache_extent(&info
->mapping_tree
.cache_tree
, &map
->ce
);
1217 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
1219 struct cache_extent
*ce
;
1220 struct map_lookup
*map
;
1223 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1225 fprintf(stderr
, "No mapping for %llu-%llu\n",
1226 (unsigned long long)logical
,
1227 (unsigned long long)logical
+len
);
1230 if (ce
->start
> logical
|| ce
->start
+ ce
->size
< logical
) {
1231 fprintf(stderr
, "Invalid mapping for %llu-%llu, got "
1232 "%llu-%llu\n", (unsigned long long)logical
,
1233 (unsigned long long)logical
+len
,
1234 (unsigned long long)ce
->start
,
1235 (unsigned long long)ce
->start
+ ce
->size
);
1238 map
= container_of(ce
, struct map_lookup
, ce
);
1240 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
1241 ret
= map
->num_stripes
;
1242 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1243 ret
= map
->sub_stripes
;
1244 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
1246 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
1253 int btrfs_next_bg(struct btrfs_mapping_tree
*map_tree
, u64
*logical
,
1254 u64
*size
, u64 type
)
1256 struct cache_extent
*ce
;
1257 struct map_lookup
*map
;
1260 ce
= search_cache_extent(&map_tree
->cache_tree
, cur
);
1264 * only jump to next bg if our cur is not 0
1265 * As the initial logical for btrfs_next_bg() is 0, and
1266 * if we jump to next bg, we skipped a valid bg.
1269 ce
= next_cache_extent(ce
);
1275 map
= container_of(ce
, struct map_lookup
, ce
);
1276 if (map
->type
& type
) {
1277 *logical
= ce
->start
;
1286 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
1287 u64 chunk_start
, u64 physical
, u64 devid
,
1288 u64
**logical
, int *naddrs
, int *stripe_len
)
1290 struct cache_extent
*ce
;
1291 struct map_lookup
*map
;
1299 ce
= search_cache_extent(&map_tree
->cache_tree
, chunk_start
);
1301 map
= container_of(ce
, struct map_lookup
, ce
);
1304 rmap_len
= map
->stripe_len
;
1305 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1306 length
= ce
->size
/ (map
->num_stripes
/ map
->sub_stripes
);
1307 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
1308 length
= ce
->size
/ map
->num_stripes
;
1309 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
1310 BTRFS_BLOCK_GROUP_RAID6
)) {
1311 length
= ce
->size
/ nr_data_stripes(map
);
1312 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
1315 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
1317 for (i
= 0; i
< map
->num_stripes
; i
++) {
1318 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
1320 if (map
->stripes
[i
].physical
> physical
||
1321 map
->stripes
[i
].physical
+ length
<= physical
)
1324 stripe_nr
= (physical
- map
->stripes
[i
].physical
) /
1327 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1328 stripe_nr
= (stripe_nr
* map
->num_stripes
+ i
) /
1330 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
1331 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
1332 } /* else if RAID[56], multiply by nr_data_stripes().
1333 * Alternatively, just use rmap_len below instead of
1334 * map->stripe_len */
1336 bytenr
= ce
->start
+ stripe_nr
* rmap_len
;
1337 for (j
= 0; j
< nr
; j
++) {
1338 if (buf
[j
] == bytenr
)
1347 *stripe_len
= rmap_len
;
1352 static inline int parity_smaller(u64 a
, u64 b
)
1357 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1358 static void sort_parity_stripes(struct btrfs_multi_bio
*bbio
, u64
*raid_map
)
1360 struct btrfs_bio_stripe s
;
1367 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
1368 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
1369 s
= bbio
->stripes
[i
];
1371 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
1372 raid_map
[i
] = raid_map
[i
+1];
1373 bbio
->stripes
[i
+1] = s
;
1381 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1382 u64 logical
, u64
*length
,
1383 struct btrfs_multi_bio
**multi_ret
, int mirror_num
,
1386 return __btrfs_map_block(map_tree
, rw
, logical
, length
, NULL
,
1387 multi_ret
, mirror_num
, raid_map_ret
);
1390 int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1391 u64 logical
, u64
*length
, u64
*type
,
1392 struct btrfs_multi_bio
**multi_ret
, int mirror_num
,
1395 struct cache_extent
*ce
;
1396 struct map_lookup
*map
;
1400 u64
*raid_map
= NULL
;
1401 int stripes_allocated
= 8;
1402 int stripes_required
= 1;
1405 struct btrfs_multi_bio
*multi
= NULL
;
1407 if (multi_ret
&& rw
== READ
) {
1408 stripes_allocated
= 1;
1411 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1417 if (ce
->start
> logical
) {
1419 *length
= ce
->start
- logical
;
1424 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
1429 map
= container_of(ce
, struct map_lookup
, ce
);
1430 offset
= logical
- ce
->start
;
1433 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
1434 BTRFS_BLOCK_GROUP_DUP
)) {
1435 stripes_required
= map
->num_stripes
;
1436 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1437 stripes_required
= map
->sub_stripes
;
1440 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)
1441 && multi_ret
&& ((rw
& WRITE
) || mirror_num
> 1) && raid_map_ret
) {
1442 /* RAID[56] write or recovery. Return all stripes */
1443 stripes_required
= map
->num_stripes
;
1445 /* Only allocate the map if we've already got a large enough multi_ret */
1446 if (stripes_allocated
>= stripes_required
) {
1447 raid_map
= kmalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
1455 /* if our multi bio struct is too small, back off and try again */
1456 if (multi_ret
&& stripes_allocated
< stripes_required
) {
1457 stripes_allocated
= stripes_required
;
1464 * stripe_nr counts the total number of stripes we have to stride
1465 * to get to this block
1467 stripe_nr
= stripe_nr
/ map
->stripe_len
;
1469 stripe_offset
= stripe_nr
* map
->stripe_len
;
1470 BUG_ON(offset
< stripe_offset
);
1472 /* stripe_offset is the offset of this block in its stripe*/
1473 stripe_offset
= offset
- stripe_offset
;
1475 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
1476 BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
|
1477 BTRFS_BLOCK_GROUP_RAID10
|
1478 BTRFS_BLOCK_GROUP_DUP
)) {
1479 /* we limit the length of each bio to what fits in a stripe */
1480 *length
= min_t(u64
, ce
->size
- offset
,
1481 map
->stripe_len
- stripe_offset
);
1483 *length
= ce
->size
- offset
;
1489 multi
->num_stripes
= 1;
1491 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1493 multi
->num_stripes
= map
->num_stripes
;
1494 else if (mirror_num
)
1495 stripe_index
= mirror_num
- 1;
1497 stripe_index
= stripe_nr
% map
->num_stripes
;
1498 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1499 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1501 stripe_index
= stripe_nr
% factor
;
1502 stripe_index
*= map
->sub_stripes
;
1505 multi
->num_stripes
= map
->sub_stripes
;
1506 else if (mirror_num
)
1507 stripe_index
+= mirror_num
- 1;
1509 stripe_nr
= stripe_nr
/ factor
;
1510 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1512 multi
->num_stripes
= map
->num_stripes
;
1513 else if (mirror_num
)
1514 stripe_index
= mirror_num
- 1;
1515 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
1516 BTRFS_BLOCK_GROUP_RAID6
)) {
1521 u64 raid56_full_stripe_start
;
1522 u64 full_stripe_len
= nr_data_stripes(map
) * map
->stripe_len
;
1525 * align the start of our data stripe in the logical
1528 raid56_full_stripe_start
= offset
/ full_stripe_len
;
1529 raid56_full_stripe_start
*= full_stripe_len
;
1531 /* get the data stripe number */
1532 stripe_nr
= raid56_full_stripe_start
/ map
->stripe_len
;
1533 stripe_nr
= stripe_nr
/ nr_data_stripes(map
);
1535 /* Work out the disk rotation on this stripe-set */
1536 rot
= stripe_nr
% map
->num_stripes
;
1538 /* Fill in the logical address of each stripe */
1539 tmp
= stripe_nr
* nr_data_stripes(map
);
1541 for (i
= 0; i
< nr_data_stripes(map
); i
++)
1542 raid_map
[(i
+rot
) % map
->num_stripes
] =
1543 ce
->start
+ (tmp
+ i
) * map
->stripe_len
;
1545 raid_map
[(i
+rot
) % map
->num_stripes
] = BTRFS_RAID5_P_STRIPE
;
1546 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
1547 raid_map
[(i
+rot
+1) % map
->num_stripes
] = BTRFS_RAID6_Q_STRIPE
;
1549 *length
= map
->stripe_len
;
1552 multi
->num_stripes
= map
->num_stripes
;
1554 stripe_index
= stripe_nr
% nr_data_stripes(map
);
1555 stripe_nr
= stripe_nr
/ nr_data_stripes(map
);
1558 * Mirror #0 or #1 means the original data block.
1559 * Mirror #2 is RAID5 parity block.
1560 * Mirror #3 is RAID6 Q block.
1563 stripe_index
= nr_data_stripes(map
) + mirror_num
- 2;
1565 /* We distribute the parity blocks across stripes */
1566 stripe_index
= (stripe_nr
+ stripe_index
) % map
->num_stripes
;
1570 * after this do_div call, stripe_nr is the number of stripes
1571 * on this device we have to walk to find the data, and
1572 * stripe_index is the number of our device in the stripe array
1574 stripe_index
= stripe_nr
% map
->num_stripes
;
1575 stripe_nr
= stripe_nr
/ map
->num_stripes
;
1577 BUG_ON(stripe_index
>= map
->num_stripes
);
1579 for (i
= 0; i
< multi
->num_stripes
; i
++) {
1580 multi
->stripes
[i
].physical
=
1581 map
->stripes
[stripe_index
].physical
+ stripe_offset
+
1582 stripe_nr
* map
->stripe_len
;
1583 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
1592 sort_parity_stripes(multi
, raid_map
);
1593 *raid_map_ret
= raid_map
;
1599 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
1602 struct btrfs_device
*device
;
1603 struct btrfs_fs_devices
*cur_devices
;
1605 cur_devices
= root
->fs_info
->fs_devices
;
1606 while (cur_devices
) {
1608 (!memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
) ||
1609 root
->fs_info
->ignore_fsid_mismatch
)) {
1610 device
= __find_device(&cur_devices
->devices
,
1615 cur_devices
= cur_devices
->seed
;
1620 struct btrfs_device
*
1621 btrfs_find_device_by_devid(struct btrfs_fs_devices
*fs_devices
,
1622 u64 devid
, int instance
)
1624 struct list_head
*head
= &fs_devices
->devices
;
1625 struct btrfs_device
*dev
;
1628 list_for_each_entry(dev
, head
, dev_list
) {
1629 if (dev
->devid
== devid
&& num_found
++ == instance
)
1635 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
1637 struct cache_extent
*ce
;
1638 struct map_lookup
*map
;
1639 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1644 * During chunk recovering, we may fail to find block group's
1645 * corresponding chunk, we will rebuild it later
1647 ce
= search_cache_extent(&map_tree
->cache_tree
, chunk_offset
);
1648 if (!root
->fs_info
->is_chunk_recover
)
1653 map
= container_of(ce
, struct map_lookup
, ce
);
1654 for (i
= 0; i
< map
->num_stripes
; i
++) {
1655 if (!map
->stripes
[i
].dev
->writeable
) {
1664 static struct btrfs_device
*fill_missing_device(u64 devid
)
1666 struct btrfs_device
*device
;
1668 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1669 device
->devid
= devid
;
1675 * slot == -1: SYSTEM chunk
1676 * return -EIO on error, otherwise return 0
1678 int btrfs_check_chunk_valid(struct btrfs_root
*root
,
1679 struct extent_buffer
*leaf
,
1680 struct btrfs_chunk
*chunk
,
1681 int slot
, u64 logical
)
1689 length
= btrfs_chunk_length(leaf
, chunk
);
1690 stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
1691 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
1692 sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
1693 type
= btrfs_chunk_type(leaf
, chunk
);
1696 * These valid checks may be insufficient to cover every corner cases.
1698 if (!IS_ALIGNED(logical
, root
->sectorsize
)) {
1699 error("invalid chunk logical %llu", logical
);
1702 if (btrfs_chunk_sector_size(leaf
, chunk
) != root
->sectorsize
) {
1703 error("invalid chunk sectorsize %llu",
1704 (unsigned long long)btrfs_chunk_sector_size(leaf
, chunk
));
1707 if (!length
|| !IS_ALIGNED(length
, root
->sectorsize
)) {
1708 error("invalid chunk length %llu", length
);
1711 if (stripe_len
!= BTRFS_STRIPE_LEN
) {
1712 error("invalid chunk stripe length: %llu", stripe_len
);
1715 /* Check on chunk item type */
1716 if (slot
== -1 && (type
& BTRFS_BLOCK_GROUP_SYSTEM
) == 0) {
1717 error("invalid chunk type %llu", type
);
1720 if (type
& ~(BTRFS_BLOCK_GROUP_TYPE_MASK
|
1721 BTRFS_BLOCK_GROUP_PROFILE_MASK
)) {
1722 error("unrecognized chunk type: %llu",
1723 ~(BTRFS_BLOCK_GROUP_TYPE_MASK
|
1724 BTRFS_BLOCK_GROUP_PROFILE_MASK
) & type
);
1728 * Btrfs_chunk contains at least one stripe, and for sys_chunk
1729 * it can't exceed the system chunk array size
1730 * For normal chunk, it should match its chunk item size.
1732 if (num_stripes
< 1 ||
1733 (slot
== -1 && sizeof(struct btrfs_stripe
) * num_stripes
>
1734 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) ||
1735 (slot
>= 0 && sizeof(struct btrfs_stripe
) * (num_stripes
- 1) >
1736 btrfs_item_size_nr(leaf
, slot
))) {
1737 error("invalid num_stripes: %u", num_stripes
);
1741 * Device number check against profile
1743 if ((type
& BTRFS_BLOCK_GROUP_RAID10
&& sub_stripes
== 0) ||
1744 (type
& BTRFS_BLOCK_GROUP_RAID1
&& num_stripes
< 1) ||
1745 (type
& BTRFS_BLOCK_GROUP_RAID5
&& num_stripes
< 2) ||
1746 (type
& BTRFS_BLOCK_GROUP_RAID6
&& num_stripes
< 3) ||
1747 (type
& BTRFS_BLOCK_GROUP_DUP
&& num_stripes
> 2) ||
1748 ((type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) == 0 &&
1749 num_stripes
!= 1)) {
1750 error("Invalid num_stripes:sub_stripes %u:%u for profile %llu",
1751 num_stripes
, sub_stripes
,
1752 type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
);
1760 * Slot is used to verify the chunk item is valid
1762 * For sys chunk in superblock, pass -1 to indicate sys chunk.
1764 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
1765 struct extent_buffer
*leaf
,
1766 struct btrfs_chunk
*chunk
, int slot
)
1768 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1769 struct map_lookup
*map
;
1770 struct cache_extent
*ce
;
1774 u8 uuid
[BTRFS_UUID_SIZE
];
1779 logical
= key
->offset
;
1780 length
= btrfs_chunk_length(leaf
, chunk
);
1781 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
1782 /* Validation check */
1783 ret
= btrfs_check_chunk_valid(root
, leaf
, chunk
, slot
, logical
);
1785 error("%s checksums match, but it has an invalid chunk, %s",
1786 (slot
== -1) ? "Superblock" : "Metadata",
1787 (slot
== -1) ? "try btrfsck --repair -s <superblock> ie, 0,1,2" : "");
1791 ce
= search_cache_extent(&map_tree
->cache_tree
, logical
);
1793 /* already mapped? */
1794 if (ce
&& ce
->start
<= logical
&& ce
->start
+ ce
->size
> logical
) {
1798 map
= kmalloc(btrfs_map_lookup_size(num_stripes
), GFP_NOFS
);
1802 map
->ce
.start
= logical
;
1803 map
->ce
.size
= length
;
1804 map
->num_stripes
= num_stripes
;
1805 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
1806 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
1807 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
1808 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
1809 map
->type
= btrfs_chunk_type(leaf
, chunk
);
1810 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
1812 for (i
= 0; i
< num_stripes
; i
++) {
1813 map
->stripes
[i
].physical
=
1814 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
1815 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
1816 read_extent_buffer(leaf
, uuid
, (unsigned long)
1817 btrfs_stripe_dev_uuid_nr(chunk
, i
),
1819 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
1821 if (!map
->stripes
[i
].dev
) {
1822 map
->stripes
[i
].dev
= fill_missing_device(devid
);
1823 printf("warning, device %llu is missing\n",
1824 (unsigned long long)devid
);
1825 list_add(&map
->stripes
[i
].dev
->dev_list
,
1826 &root
->fs_info
->fs_devices
->devices
);
1830 ret
= insert_cache_extent(&map_tree
->cache_tree
, &map
->ce
);
1836 static int fill_device_from_item(struct extent_buffer
*leaf
,
1837 struct btrfs_dev_item
*dev_item
,
1838 struct btrfs_device
*device
)
1842 device
->devid
= btrfs_device_id(leaf
, dev_item
);
1843 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
1844 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
1845 device
->type
= btrfs_device_type(leaf
, dev_item
);
1846 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
1847 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
1848 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
1850 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1851 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1856 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
1858 struct btrfs_fs_devices
*fs_devices
;
1861 fs_devices
= root
->fs_info
->fs_devices
->seed
;
1862 while (fs_devices
) {
1863 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
1867 fs_devices
= fs_devices
->seed
;
1870 fs_devices
= find_fsid(fsid
);
1872 /* missing all seed devices */
1873 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1878 INIT_LIST_HEAD(&fs_devices
->devices
);
1879 list_add(&fs_devices
->list
, &fs_uuids
);
1880 memcpy(fs_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
);
1883 ret
= btrfs_open_devices(fs_devices
, O_RDONLY
);
1887 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
1888 root
->fs_info
->fs_devices
->seed
= fs_devices
;
1893 static int read_one_dev(struct btrfs_root
*root
,
1894 struct extent_buffer
*leaf
,
1895 struct btrfs_dev_item
*dev_item
)
1897 struct btrfs_device
*device
;
1900 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1901 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1903 devid
= btrfs_device_id(leaf
, dev_item
);
1904 read_extent_buffer(leaf
, dev_uuid
,
1905 (unsigned long)btrfs_device_uuid(dev_item
),
1907 read_extent_buffer(leaf
, fs_uuid
,
1908 (unsigned long)btrfs_device_fsid(dev_item
),
1911 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
1912 ret
= open_seed_devices(root
, fs_uuid
);
1917 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1919 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1923 list_add(&device
->dev_list
,
1924 &root
->fs_info
->fs_devices
->devices
);
1927 fill_device_from_item(leaf
, dev_item
, device
);
1928 device
->dev_root
= root
->fs_info
->dev_root
;
1932 int btrfs_read_sys_array(struct btrfs_root
*root
)
1934 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1935 struct extent_buffer
*sb
;
1936 struct btrfs_disk_key
*disk_key
;
1937 struct btrfs_chunk
*chunk
;
1939 unsigned long sb_array_offset
;
1945 struct btrfs_key key
;
1947 sb
= btrfs_find_create_tree_block(root
->fs_info
,
1948 BTRFS_SUPER_INFO_OFFSET
,
1949 BTRFS_SUPER_INFO_SIZE
);
1952 btrfs_set_buffer_uptodate(sb
);
1953 write_extent_buffer(sb
, super_copy
, 0, sizeof(*super_copy
));
1954 array_size
= btrfs_super_sys_array_size(super_copy
);
1956 array_ptr
= super_copy
->sys_chunk_array
;
1957 sb_array_offset
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
1960 while (cur_offset
< array_size
) {
1961 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
1962 len
= sizeof(*disk_key
);
1963 if (cur_offset
+ len
> array_size
)
1964 goto out_short_read
;
1966 btrfs_disk_key_to_cpu(&key
, disk_key
);
1969 sb_array_offset
+= len
;
1972 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1973 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
1975 * At least one btrfs_chunk with one stripe must be
1976 * present, exact stripe count check comes afterwards
1978 len
= btrfs_chunk_item_size(1);
1979 if (cur_offset
+ len
> array_size
)
1980 goto out_short_read
;
1982 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
1985 "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
1986 num_stripes
, cur_offset
);
1991 len
= btrfs_chunk_item_size(num_stripes
);
1992 if (cur_offset
+ len
> array_size
)
1993 goto out_short_read
;
1995 ret
= read_one_chunk(root
, &key
, sb
, chunk
, -1);
2000 "ERROR: unexpected item type %u in sys_array at offset %u\n",
2001 (u32
)key
.type
, cur_offset
);
2006 sb_array_offset
+= len
;
2009 free_extent_buffer(sb
);
2013 printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
2015 free_extent_buffer(sb
);
2019 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
2021 struct btrfs_path
*path
;
2022 struct extent_buffer
*leaf
;
2023 struct btrfs_key key
;
2024 struct btrfs_key found_key
;
2028 root
= root
->fs_info
->chunk_root
;
2030 path
= btrfs_alloc_path();
2035 * Read all device items, and then all the chunk items. All
2036 * device items are found before any chunk item (their object id
2037 * is smaller than the lowest possible object id for a chunk
2038 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
2040 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2043 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2047 leaf
= path
->nodes
[0];
2048 slot
= path
->slots
[0];
2049 if (slot
>= btrfs_header_nritems(leaf
)) {
2050 ret
= btrfs_next_leaf(root
, path
);
2057 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2058 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
2059 struct btrfs_dev_item
*dev_item
;
2060 dev_item
= btrfs_item_ptr(leaf
, slot
,
2061 struct btrfs_dev_item
);
2062 ret
= read_one_dev(root
, leaf
, dev_item
);
2064 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2065 struct btrfs_chunk
*chunk
;
2066 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2067 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
,
2076 btrfs_free_path(path
);
2080 struct list_head
*btrfs_scanned_uuids(void)
2085 static int rmw_eb(struct btrfs_fs_info
*info
,
2086 struct extent_buffer
*eb
, struct extent_buffer
*orig_eb
)
2089 unsigned long orig_off
= 0;
2090 unsigned long dest_off
= 0;
2091 unsigned long copy_len
= eb
->len
;
2093 ret
= read_whole_eb(info
, eb
, 0);
2097 if (eb
->start
+ eb
->len
<= orig_eb
->start
||
2098 eb
->start
>= orig_eb
->start
+ orig_eb
->len
)
2101 * | ----- orig_eb ------- |
2102 * | ----- stripe ------- |
2103 * | ----- orig_eb ------- |
2104 * | ----- orig_eb ------- |
2106 if (eb
->start
> orig_eb
->start
)
2107 orig_off
= eb
->start
- orig_eb
->start
;
2108 if (orig_eb
->start
> eb
->start
)
2109 dest_off
= orig_eb
->start
- eb
->start
;
2111 if (copy_len
> orig_eb
->len
- orig_off
)
2112 copy_len
= orig_eb
->len
- orig_off
;
2113 if (copy_len
> eb
->len
- dest_off
)
2114 copy_len
= eb
->len
- dest_off
;
2116 memcpy(eb
->data
+ dest_off
, orig_eb
->data
+ orig_off
, copy_len
);
2120 static int split_eb_for_raid56(struct btrfs_fs_info
*info
,
2121 struct extent_buffer
*orig_eb
,
2122 struct extent_buffer
**ebs
,
2123 u64 stripe_len
, u64
*raid_map
,
2126 struct extent_buffer
**tmp_ebs
;
2127 u64 start
= orig_eb
->start
;
2132 tmp_ebs
= calloc(num_stripes
, sizeof(*tmp_ebs
));
2136 /* Alloc memory in a row for data stripes */
2137 for (i
= 0; i
< num_stripes
; i
++) {
2138 if (raid_map
[i
] >= BTRFS_RAID5_P_STRIPE
)
2141 tmp_ebs
[i
] = calloc(1, sizeof(**tmp_ebs
) + stripe_len
);
2148 for (i
= 0; i
< num_stripes
; i
++) {
2149 struct extent_buffer
*eb
= tmp_ebs
[i
];
2151 if (raid_map
[i
] >= BTRFS_RAID5_P_STRIPE
)
2154 eb
->start
= raid_map
[i
];
2155 eb
->len
= stripe_len
;
2159 eb
->dev_bytenr
= (u64
)-1;
2161 this_eb_start
= raid_map
[i
];
2163 if (start
> this_eb_start
||
2164 start
+ orig_eb
->len
< this_eb_start
+ stripe_len
) {
2165 ret
= rmw_eb(info
, eb
, orig_eb
);
2169 memcpy(eb
->data
, orig_eb
->data
+ eb
->start
- start
,
2177 for (i
= 0; i
< num_stripes
; i
++)
2183 int write_raid56_with_parity(struct btrfs_fs_info
*info
,
2184 struct extent_buffer
*eb
,
2185 struct btrfs_multi_bio
*multi
,
2186 u64 stripe_len
, u64
*raid_map
)
2188 struct extent_buffer
**ebs
, *p_eb
= NULL
, *q_eb
= NULL
;
2191 int alloc_size
= eb
->len
;
2194 ebs
= malloc(sizeof(*ebs
) * multi
->num_stripes
);
2195 pointers
= malloc(sizeof(*pointers
) * multi
->num_stripes
);
2196 if (!ebs
|| !pointers
) {
2202 if (stripe_len
> alloc_size
)
2203 alloc_size
= stripe_len
;
2205 ret
= split_eb_for_raid56(info
, eb
, ebs
, stripe_len
, raid_map
,
2206 multi
->num_stripes
);
2210 for (i
= 0; i
< multi
->num_stripes
; i
++) {
2211 struct extent_buffer
*new_eb
;
2212 if (raid_map
[i
] < BTRFS_RAID5_P_STRIPE
) {
2213 ebs
[i
]->dev_bytenr
= multi
->stripes
[i
].physical
;
2214 ebs
[i
]->fd
= multi
->stripes
[i
].dev
->fd
;
2215 multi
->stripes
[i
].dev
->total_ios
++;
2216 if (ebs
[i
]->start
!= raid_map
[i
]) {
2218 goto out_free_split
;
2222 new_eb
= malloc(sizeof(*eb
) + alloc_size
);
2225 goto out_free_split
;
2227 new_eb
->dev_bytenr
= multi
->stripes
[i
].physical
;
2228 new_eb
->fd
= multi
->stripes
[i
].dev
->fd
;
2229 multi
->stripes
[i
].dev
->total_ios
++;
2230 new_eb
->len
= stripe_len
;
2232 if (raid_map
[i
] == BTRFS_RAID5_P_STRIPE
)
2234 else if (raid_map
[i
] == BTRFS_RAID6_Q_STRIPE
)
2238 ebs
[multi
->num_stripes
- 2] = p_eb
;
2239 ebs
[multi
->num_stripes
- 1] = q_eb
;
2241 for (i
= 0; i
< multi
->num_stripes
; i
++)
2242 pointers
[i
] = ebs
[i
]->data
;
2244 raid6_gen_syndrome(multi
->num_stripes
, stripe_len
, pointers
);
2246 ebs
[multi
->num_stripes
- 1] = p_eb
;
2247 for (i
= 0; i
< multi
->num_stripes
; i
++)
2248 pointers
[i
] = ebs
[i
]->data
;
2249 ret
= raid5_gen_result(multi
->num_stripes
, stripe_len
,
2250 multi
->num_stripes
- 1, pointers
);
2252 goto out_free_split
;
2255 for (i
= 0; i
< multi
->num_stripes
; i
++) {
2256 ret
= write_extent_to_disk(ebs
[i
]);
2258 goto out_free_split
;
2262 for (i
= 0; i
< multi
->num_stripes
; i
++) {