Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
David Sterba | 9888c34 | 2018-04-03 19:16:55 +0200 | [diff] [blame] | 2 | |
| 3 | #ifndef BTRFS_EXTENT_MAP_H |
| 4 | #define BTRFS_EXTENT_MAP_H |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5 | |
| 6 | #include <linux/rbtree.h> |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 7 | #include <linux/refcount.h> |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8 | |
Dulshani Gunawardhana | e248e04 | 2013-10-31 10:31:13 +0530 | [diff] [blame] | 9 | #define EXTENT_MAP_LAST_BYTE ((u64)-4) |
| 10 | #define EXTENT_MAP_HOLE ((u64)-3) |
| 11 | #define EXTENT_MAP_INLINE ((u64)-2) |
Nikolay Borisov | 951e05a | 2019-01-08 16:53:46 +0200 | [diff] [blame] | 12 | /* used only during fiemap calls */ |
Dulshani Gunawardhana | e248e04 | 2013-10-31 10:31:13 +0530 | [diff] [blame] | 13 | #define EXTENT_MAP_DELALLOC ((u64)-1) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 14 | |
David Sterba | 50b5b60 | 2018-11-27 15:11:43 +0100 | [diff] [blame] | 15 | /* bits for the extent_map::flags field */ |
| 16 | enum { |
| 17 | /* this entry not yet on disk, don't free it */ |
| 18 | EXTENT_FLAG_PINNED, |
| 19 | EXTENT_FLAG_COMPRESSED, |
| 20 | /* pre-allocated extent */ |
| 21 | EXTENT_FLAG_PREALLOC, |
| 22 | /* Logging this extent */ |
| 23 | EXTENT_FLAG_LOGGING, |
| 24 | /* Filling in a preallocated extent */ |
| 25 | EXTENT_FLAG_FILLING, |
| 26 | /* filesystem extent mapping type */ |
| 27 | EXTENT_FLAG_FS_MAPPING, |
| 28 | }; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 29 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 30 | struct extent_map { |
| 31 | struct rb_node rb_node; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 32 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 33 | /* all of these are in bytes */ |
| 34 | u64 start; |
| 35 | u64 len; |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 36 | u64 mod_start; |
| 37 | u64 mod_len; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 38 | u64 orig_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 39 | u64 orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 40 | u64 ram_bytes; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 41 | u64 block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 42 | u64 block_len; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 43 | u64 generation; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 44 | unsigned long flags; |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 45 | union { |
| 46 | struct block_device *bdev; |
| 47 | |
| 48 | /* |
| 49 | * used for chunk mappings |
| 50 | * flags & EXTENT_FLAG_FS_MAPPING must be set |
| 51 | */ |
| 52 | struct map_lookup *map_lookup; |
| 53 | }; |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 54 | refcount_t refs; |
David Sterba | c08782d | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 55 | unsigned int compress_type; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 56 | struct list_head list; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 57 | }; |
| 58 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 59 | struct extent_map_tree { |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 60 | struct rb_root_cached map; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 61 | struct list_head modified_extents; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 62 | rwlock_t lock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 65 | static inline int extent_map_in_tree(const struct extent_map *em) |
| 66 | { |
| 67 | return !RB_EMPTY_NODE(&em->rb_node); |
| 68 | } |
| 69 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 70 | static inline u64 extent_map_end(struct extent_map *em) |
| 71 | { |
| 72 | if (em->start + em->len < em->start) |
| 73 | return (u64)-1; |
| 74 | return em->start + em->len; |
| 75 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 76 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 77 | static inline u64 extent_map_block_end(struct extent_map *em) |
| 78 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 79 | if (em->block_start + em->block_len < em->block_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 80 | return (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 81 | return em->block_start + em->block_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 82 | } |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 83 | |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 84 | void extent_map_tree_init(struct extent_map_tree *tree); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 85 | struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 86 | u64 start, u64 len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 87 | int add_extent_mapping(struct extent_map_tree *tree, |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 88 | struct extent_map *em, int modified); |
zhong jiang | c1766dd | 2018-09-13 11:01:15 +0800 | [diff] [blame] | 89 | void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em); |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 90 | void replace_extent_mapping(struct extent_map_tree *tree, |
| 91 | struct extent_map *cur, |
| 92 | struct extent_map *new, |
| 93 | int modified); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 94 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 95 | struct extent_map *alloc_extent_map(void); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 96 | void free_extent_map(struct extent_map *em); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 97 | int __init extent_map_init(void); |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 98 | void __cold extent_map_exit(void); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 99 | int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 100 | void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em); |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 101 | struct extent_map *search_extent_mapping(struct extent_map_tree *tree, |
| 102 | u64 start, u64 len); |
David Sterba | f46b24c | 2018-04-03 21:45:57 +0200 | [diff] [blame] | 103 | int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, |
| 104 | struct extent_map_tree *em_tree, |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 105 | struct extent_map **em_in, u64 start, u64 len); |
David Sterba | 9888c34 | 2018-04-03 19:16:55 +0200 | [diff] [blame] | 106 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 107 | #endif |