blob: a67b2def54131f10326c71092f80f2cd2d706212 [file] [log] [blame]
Chris Masona52d9a82007-08-27 16:49:44 -04001#ifndef __EXTENTMAP__
2#define __EXTENTMAP__
3
4#include <linux/rbtree.h>
Elena Reshetova490b54d2017-03-03 10:55:12 +02005#include <linux/refcount.h>
Chris Masona52d9a82007-08-27 16:49:44 -04006
Dulshani Gunawardhanae248e042013-10-31 10:31:13 +05307#define EXTENT_MAP_LAST_BYTE ((u64)-4)
8#define EXTENT_MAP_HOLE ((u64)-3)
9#define EXTENT_MAP_INLINE ((u64)-2)
10#define EXTENT_MAP_DELALLOC ((u64)-1)
Chris Masona52d9a82007-08-27 16:49:44 -040011
Chris Mason7f3c74f2008-07-18 12:01:11 -040012/* bits for the flags field */
13#define EXTENT_FLAG_PINNED 0 /* this entry not yet on disk, don't free it */
Chris Masonc8b97812008-10-29 14:49:59 -040014#define EXTENT_FLAG_COMPRESSED 1
Yan Zheng9036c102008-10-30 14:19:41 -040015#define EXTENT_FLAG_VACANCY 2 /* no file extent item found */
Yan Zhengd899e052008-10-30 14:25:28 -040016#define EXTENT_FLAG_PREALLOC 3 /* pre-allocated extent */
Josef Bacikff44c6e2012-09-14 12:59:20 -040017#define EXTENT_FLAG_LOGGING 4 /* Logging this extent */
Josef Bacikb11e2342012-12-03 10:58:15 -050018#define EXTENT_FLAG_FILLING 5 /* Filling in a preallocated extent */
Wang Shilong298a8f92014-06-19 10:42:52 +080019#define EXTENT_FLAG_FS_MAPPING 6 /* filesystem extent mapping type */
Chris Mason7f3c74f2008-07-18 12:01:11 -040020
Chris Masond1310b22008-01-24 16:13:08 -050021struct extent_map {
22 struct rb_node rb_node;
Chris Mason5f39d392007-10-15 16:14:19 -040023
Chris Masond1310b22008-01-24 16:13:08 -050024 /* all of these are in bytes */
25 u64 start;
26 u64 len;
Liu Bo4e2f84e2012-08-27 10:52:20 -060027 u64 mod_start;
28 u64 mod_len;
Yan Zhengff5b7ee2008-11-10 07:34:43 -050029 u64 orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -050030 u64 orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -040031 u64 ram_bytes;
Chris Masond1310b22008-01-24 16:13:08 -050032 u64 block_start;
Chris Masonc8b97812008-10-29 14:49:59 -040033 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -040034 u64 generation;
Chris Masond1310b22008-01-24 16:13:08 -050035 unsigned long flags;
Jeff Mahoney95617d62015-06-03 10:55:48 -040036 union {
37 struct block_device *bdev;
38
39 /*
40 * used for chunk mappings
41 * flags & EXTENT_FLAG_FS_MAPPING must be set
42 */
43 struct map_lookup *map_lookup;
44 };
Elena Reshetova490b54d2017-03-03 10:55:12 +020045 refcount_t refs;
David Sterbac08782d2012-01-26 15:01:12 -050046 unsigned int compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -040047 struct list_head list;
Chris Mason07157aa2007-08-30 08:50:51 -040048};
49
Chris Masona52d9a82007-08-27 16:49:44 -040050struct extent_map_tree {
51 struct rb_root map;
Josef Bacik5dc562c2012-08-17 13:14:17 -040052 struct list_head modified_extents;
Chris Mason890871b2009-09-02 16:24:52 -040053 rwlock_t lock;
Chris Masona52d9a82007-08-27 16:49:44 -040054};
55
Filipe Mananacbc0e922014-02-25 14:15:12 +000056static inline int extent_map_in_tree(const struct extent_map *em)
57{
58 return !RB_EMPTY_NODE(&em->rb_node);
59}
60
Chris Masond1310b22008-01-24 16:13:08 -050061static inline u64 extent_map_end(struct extent_map *em)
62{
63 if (em->start + em->len < em->start)
64 return (u64)-1;
65 return em->start + em->len;
66}
Chris Masona52d9a82007-08-27 16:49:44 -040067
Chris Masond1310b22008-01-24 16:13:08 -050068static inline u64 extent_map_block_end(struct extent_map *em)
69{
Chris Masonc8b97812008-10-29 14:49:59 -040070 if (em->block_start + em->block_len < em->block_start)
Chris Masond1310b22008-01-24 16:13:08 -050071 return (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -040072 return em->block_start + em->block_len;
Chris Masond1310b22008-01-24 16:13:08 -050073}
Chris Mason07157aa2007-08-30 08:50:51 -040074
David Sterbaa8067e02011-04-21 00:34:43 +020075void extent_map_tree_init(struct extent_map_tree *tree);
Chris Masona52d9a82007-08-27 16:49:44 -040076struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -050077 u64 start, u64 len);
Chris Masona52d9a82007-08-27 16:49:44 -040078int add_extent_mapping(struct extent_map_tree *tree,
Josef Bacik09a2a8f92013-04-05 16:51:15 -040079 struct extent_map *em, int modified);
Chris Masona52d9a82007-08-27 16:49:44 -040080int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em);
Filipe Manana176840b2014-02-25 14:15:13 +000081void replace_extent_mapping(struct extent_map_tree *tree,
82 struct extent_map *cur,
83 struct extent_map *new,
84 int modified);
Chris Masond1310b22008-01-24 16:13:08 -050085
David Sterba172ddd62011-04-21 00:48:27 +020086struct extent_map *alloc_extent_map(void);
Chris Masona52d9a82007-08-27 16:49:44 -040087void free_extent_map(struct extent_map *em);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050088int __init extent_map_init(void);
Christian Hesse17636e02007-12-11 09:25:06 -050089void extent_map_exit(void);
Josef Bacik5dc562c2012-08-17 13:14:17 -040090int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen);
Josef Bacik201a9032013-01-24 12:02:07 -050091void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
Chris Masonb917b7c2009-09-18 16:07:03 -040092struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
93 u64 start, u64 len);
Chris Masona52d9a82007-08-27 16:49:44 -040094#endif