|
15 | 15 | #include "tree-log.h" |
16 | 16 | #include "delalloc-space.h" |
17 | 17 | #include "discard.h" |
| 18 | +#include "raid56.h" |
18 | 19 |
|
19 | 20 | /* |
20 | 21 | * Return target flags in extended format or 0 if restripe for this chunk_type |
@@ -1561,6 +1562,91 @@ static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
1561 | 1562 | write_sequnlock(&fs_info->profiles_lock); |
1562 | 1563 | } |
1563 | 1564 |
|
| 1565 | +/** |
| 1566 | + * btrfs_rmap_block - Map a physical disk address to a list of logical addresses |
| 1567 | + * @chunk_start: logical address of block group |
| 1568 | + * @physical: physical address to map to logical addresses |
| 1569 | + * @logical: return array of logical addresses which map to @physical |
| 1570 | + * @naddrs: length of @logical |
| 1571 | + * @stripe_len: size of IO stripe for the given block group |
| 1572 | + * |
| 1573 | + * Maps a particular @physical disk address to a list of @logical addresses. |
| 1574 | + * Used primarily to exclude those portions of a block group that contain super |
| 1575 | + * block copies. |
| 1576 | + */ |
| 1577 | +EXPORT_FOR_TESTS |
| 1578 | +int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, |
| 1579 | + u64 physical, u64 **logical, int *naddrs, int *stripe_len) |
| 1580 | +{ |
| 1581 | + struct extent_map *em; |
| 1582 | + struct map_lookup *map; |
| 1583 | + u64 *buf; |
| 1584 | + u64 bytenr; |
| 1585 | + u64 length; |
| 1586 | + u64 stripe_nr; |
| 1587 | + u64 rmap_len; |
| 1588 | + int i, j, nr = 0; |
| 1589 | + |
| 1590 | + em = btrfs_get_chunk_map(fs_info, chunk_start, 1); |
| 1591 | + if (IS_ERR(em)) |
| 1592 | + return -EIO; |
| 1593 | + |
| 1594 | + map = em->map_lookup; |
| 1595 | + length = em->len; |
| 1596 | + rmap_len = map->stripe_len; |
| 1597 | + |
| 1598 | + if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 1599 | + length = div_u64(length, map->num_stripes / map->sub_stripes); |
| 1600 | + else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 1601 | + length = div_u64(length, map->num_stripes); |
| 1602 | + else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 1603 | + length = div_u64(length, nr_data_stripes(map)); |
| 1604 | + rmap_len = map->stripe_len * nr_data_stripes(map); |
| 1605 | + } |
| 1606 | + |
| 1607 | + buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
| 1608 | + BUG_ON(!buf); /* -ENOMEM */ |
| 1609 | + |
| 1610 | + for (i = 0; i < map->num_stripes; i++) { |
| 1611 | + if (map->stripes[i].physical > physical || |
| 1612 | + map->stripes[i].physical + length <= physical) |
| 1613 | + continue; |
| 1614 | + |
| 1615 | + stripe_nr = physical - map->stripes[i].physical; |
| 1616 | + stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
| 1617 | + |
| 1618 | + if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 1619 | + stripe_nr = stripe_nr * map->num_stripes + i; |
| 1620 | + stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
| 1621 | + } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 1622 | + stripe_nr = stripe_nr * map->num_stripes + i; |
| 1623 | + } |
| 1624 | + /* |
| 1625 | + * The remaining case would be for RAID56, multiply by |
| 1626 | + * nr_data_stripes(). Alternatively, just use rmap_len below |
| 1627 | + * instead of map->stripe_len |
| 1628 | + */ |
| 1629 | + |
| 1630 | + bytenr = chunk_start + stripe_nr * rmap_len; |
| 1631 | + WARN_ON(nr >= map->num_stripes); |
| 1632 | + for (j = 0; j < nr; j++) { |
| 1633 | + if (buf[j] == bytenr) |
| 1634 | + break; |
| 1635 | + } |
| 1636 | + if (j == nr) { |
| 1637 | + WARN_ON(nr >= map->num_stripes); |
| 1638 | + buf[nr++] = bytenr; |
| 1639 | + } |
| 1640 | + } |
| 1641 | + |
| 1642 | + *logical = buf; |
| 1643 | + *naddrs = nr; |
| 1644 | + *stripe_len = rmap_len; |
| 1645 | + |
| 1646 | + free_extent_map(em); |
| 1647 | + return 0; |
| 1648 | +} |
| 1649 | + |
1564 | 1650 | static int exclude_super_stripes(struct btrfs_block_group *cache) |
1565 | 1651 | { |
1566 | 1652 | struct btrfs_fs_info *fs_info = cache->fs_info; |
|
0 commit comments