blob: ad311864272a00f67e971d5a84a599851dc355a7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
David Sterba9888c342018-04-03 19:16:55 +02002
3#ifndef BTRFS_EXTENT_MAP_H
4#define BTRFS_EXTENT_MAP_H
Chris Masona52d9a82007-08-27 16:49:44 -04005
6#include <linux/rbtree.h>
Elena Reshetova490b54d2017-03-03 10:55:12 +02007#include <linux/refcount.h>
Chris Masona52d9a82007-08-27 16:49:44 -04008
Dulshani Gunawardhanae248e042013-10-31 10:31:13 +05309#define EXTENT_MAP_LAST_BYTE ((u64)-4)
10#define EXTENT_MAP_HOLE ((u64)-3)
11#define EXTENT_MAP_INLINE ((u64)-2)
Nikolay Borisov951e05a2019-01-08 16:53:46 +020012/* used only during fiemap calls */
Dulshani Gunawardhanae248e042013-10-31 10:31:13 +053013#define EXTENT_MAP_DELALLOC ((u64)-1)
Chris Masona52d9a82007-08-27 16:49:44 -040014
David Sterba50b5b602018-11-27 15:11:43 +010015/* bits for the extent_map::flags field */
16enum {
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,
Qu Wenruo199257a2022-02-11 14:46:13 +080028 /* This em is merged from two or more physically adjacent ems */
29 EXTENT_FLAG_MERGED,
David Sterba50b5b602018-11-27 15:11:43 +010030};
Chris Mason7f3c74f2008-07-18 12:01:11 -040031
Chris Masond1310b22008-01-24 16:13:08 -050032struct extent_map {
33 struct rb_node rb_node;
Chris Mason5f39d392007-10-15 16:14:19 -040034
Chris Masond1310b22008-01-24 16:13:08 -050035 /* all of these are in bytes */
36 u64 start;
37 u64 len;
Liu Bo4e2f84e2012-08-27 10:52:20 -060038 u64 mod_start;
39 u64 mod_len;
Yan Zhengff5b7ee2008-11-10 07:34:43 -050040 u64 orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -050041 u64 orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -040042 u64 ram_bytes;
Chris Masond1310b22008-01-24 16:13:08 -050043 u64 block_start;
Chris Masonc8b97812008-10-29 14:49:59 -040044 u64 block_len;
Qu Wenruo199257a2022-02-11 14:46:13 +080045
46 /*
47 * Generation of the extent map, for merged em it's the highest
48 * generation of all merged ems.
49 * For non-merged extents, it's from btrfs_file_extent_item::generation.
50 */
Josef Bacik5dc562c2012-08-17 13:14:17 -040051 u64 generation;
Chris Masond1310b22008-01-24 16:13:08 -050052 unsigned long flags;
David Sterbaa019e9e2019-08-30 15:40:53 +020053 /* Used for chunk mappings, flag EXTENT_FLAG_FS_MAPPING must be set */
54 struct map_lookup *map_lookup;
Elena Reshetova490b54d2017-03-03 10:55:12 +020055 refcount_t refs;
David Sterbac08782d2012-01-26 15:01:12 -050056 unsigned int compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -040057 struct list_head list;
Chris Mason07157aa2007-08-30 08:50:51 -040058};
59
Chris Masona52d9a82007-08-27 16:49:44 -040060struct extent_map_tree {
Liu Bo07e1ce02018-08-23 03:51:52 +080061 struct rb_root_cached map;
Josef Bacik5dc562c2012-08-17 13:14:17 -040062 struct list_head modified_extents;
Chris Mason890871b2009-09-02 16:24:52 -040063 rwlock_t lock;
Chris Masona52d9a82007-08-27 16:49:44 -040064};
65
Filipe Manana4c0c8cf2022-09-19 15:06:29 +010066struct btrfs_inode;
67
Filipe Mananacbc0e922014-02-25 14:15:12 +000068static inline int extent_map_in_tree(const struct extent_map *em)
69{
70 return !RB_EMPTY_NODE(&em->rb_node);
71}
72
Chris Masond1310b22008-01-24 16:13:08 -050073static inline u64 extent_map_end(struct extent_map *em)
74{
75 if (em->start + em->len < em->start)
76 return (u64)-1;
77 return em->start + em->len;
78}
Chris Masona52d9a82007-08-27 16:49:44 -040079
Chris Masond1310b22008-01-24 16:13:08 -050080static inline u64 extent_map_block_end(struct extent_map *em)
81{
Chris Masonc8b97812008-10-29 14:49:59 -040082 if (em->block_start + em->block_len < em->block_start)
Chris Masond1310b22008-01-24 16:13:08 -050083 return (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -040084 return em->block_start + em->block_len;
Chris Masond1310b22008-01-24 16:13:08 -050085}
Chris Mason07157aa2007-08-30 08:50:51 -040086
David Sterbaa8067e02011-04-21 00:34:43 +020087void extent_map_tree_init(struct extent_map_tree *tree);
Chris Masona52d9a82007-08-27 16:49:44 -040088struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -050089 u64 start, u64 len);
Chris Masona52d9a82007-08-27 16:49:44 -040090int add_extent_mapping(struct extent_map_tree *tree,
Josef Bacik09a2a8f92013-04-05 16:51:15 -040091 struct extent_map *em, int modified);
zhong jiangc1766dd2018-09-13 11:01:15 +080092void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em);
Filipe Manana176840b2014-02-25 14:15:13 +000093void replace_extent_mapping(struct extent_map_tree *tree,
94 struct extent_map *cur,
95 struct extent_map *new,
96 int modified);
Chris Masond1310b22008-01-24 16:13:08 -050097
David Sterba172ddd62011-04-21 00:48:27 +020098struct extent_map *alloc_extent_map(void);
Chris Masona52d9a82007-08-27 16:49:44 -040099void free_extent_map(struct extent_map *em);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500100int __init extent_map_init(void);
David Sterbae67c7182018-02-19 17:24:18 +0100101void __cold extent_map_exit(void);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400102int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen);
Josef Bacik201a9032013-01-24 12:02:07 -0500103void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
Chris Masonb917b7c2009-09-18 16:07:03 -0400104struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
105 u64 start, u64 len);
David Sterbaf46b24c2018-04-03 21:45:57 +0200106int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
107 struct extent_map_tree *em_tree,
Liu Boc04e61b2018-01-05 12:51:11 -0700108 struct extent_map **em_in, u64 start, u64 len);
Filipe Manana4c0c8cf2022-09-19 15:06:29 +0100109void btrfs_drop_extent_map_range(struct btrfs_inode *inode,
110 u64 start, u64 end,
111 bool skip_pinned);
Filipe Mananaa1ba4c02022-09-19 15:06:33 +0100112int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
113 struct extent_map *new_em,
114 bool modified);
David Sterba9888c342018-04-03 19:16:55 +0200115
Chris Masona52d9a82007-08-27 16:49:44 -0400116#endif