btrfs-progs: Add last_cache_extent() for extent-cache.
[platform/upstream/btrfs-progs.git] / extent-cache.h
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #ifndef __EXTENT_CACHE_H__
20 #define __EXTENT_CACHE_H__
21
22 #if BTRFS_FLAT_INCLUDES
23 #include "kerncompat.h"
24 #include "rbtree.h"
25 #else
26 #include <btrfs/kerncompat.h>
27 #include <btrfs/rbtree.h>
28 #endif /* BTRFS_FLAT_INCLUDES */
29
30 struct cache_tree {
31         struct rb_root root;
32 };
33
34 struct cache_extent {
35         struct rb_node rb_node;
36         u64 objectid;
37         u64 start;
38         u64 size;
39 };
40
41 void cache_tree_init(struct cache_tree *tree);
42
43 struct cache_extent *first_cache_extent(struct cache_tree *tree);
44 struct cache_extent *last_cache_extent(struct cache_tree *tree);
45 struct cache_extent *prev_cache_extent(struct cache_extent *pe);
46 struct cache_extent *next_cache_extent(struct cache_extent *pe);
47
48 struct cache_extent *search_cache_extent(struct cache_tree *tree, u64 start);
49 struct cache_extent *lookup_cache_extent(struct cache_tree *tree,
50                                          u64 start, u64 size);
51
52 int add_cache_extent(struct cache_tree *tree, u64 start, u64 size);
53 int insert_cache_extent(struct cache_tree *tree, struct cache_extent *pe);
54 void remove_cache_extent(struct cache_tree *tree, struct cache_extent *pe);
55
56 static inline int cache_tree_empty(struct cache_tree *tree)
57 {
58         return RB_EMPTY_ROOT(&tree->root);
59 }
60
61 typedef void (*free_cache_extent)(struct cache_extent *pe);
62
63 void cache_tree_free_extents(struct cache_tree *tree,
64                              free_cache_extent free_func);
65
66 #define FREE_EXTENT_CACHE_BASED_TREE(name, free_func)           \
67 static void free_##name##_tree(struct cache_tree *tree)         \
68 {                                                               \
69         cache_tree_free_extents(tree, free_func);               \
70 }
71
72 void free_extent_cache_tree(struct cache_tree *tree);
73
74 struct cache_extent *search_cache_extent2(struct cache_tree *tree,
75                                           u64 objectid, u64 start);
76 struct cache_extent *lookup_cache_extent2(struct cache_tree *tree,
77                                           u64 objectid, u64 start, u64 size);
78 int add_cache_extent2(struct cache_tree *tree,
79                       u64 objectid, u64 start, u64 size);
80 int insert_cache_extent2(struct cache_tree *tree, struct cache_extent *pe);
81
82 #endif