Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * GPL HEADER START |
| 3 | * |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 only, |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License version 2 for more details (a copy is included |
| 14 | * in the LICENSE file that accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * version 2 along with this program; If not, see |
Oleg Drokin | 6a5b99a | 2016-06-14 23:33:40 -0400 | [diff] [blame] | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 20 | * GPL HEADER END |
| 21 | */ |
| 22 | /* |
| 23 | * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. |
| 24 | * Use is subject to license terms. |
| 25 | */ |
| 26 | /* |
| 27 | * This file is part of Lustre, http://www.lustre.org/ |
| 28 | * Lustre is a trademark of Sun Microsystems, Inc. |
| 29 | * |
| 30 | * lustre/include/interval_tree.h |
| 31 | * |
| 32 | * Author: Huang Wei <huangwei@clusterfs.com> |
| 33 | * Author: Jay Xiong <jinshan.xiong@sun.com> |
| 34 | */ |
| 35 | |
| 36 | #ifndef _INTERVAL_H__ |
| 37 | #define _INTERVAL_H__ |
| 38 | |
Greg Kroah-Hartman | 9fdaf8c | 2014-07-11 20:51:16 -0700 | [diff] [blame] | 39 | #include "../../include/linux/libcfs/libcfs.h" /* LASSERT. */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 40 | |
| 41 | struct interval_node { |
| 42 | struct interval_node *in_left; |
| 43 | struct interval_node *in_right; |
| 44 | struct interval_node *in_parent; |
| 45 | unsigned in_color:1, |
| 46 | in_intree:1, /** set if the node is in tree */ |
| 47 | in_res1:30; |
| 48 | __u8 in_res2[4]; /** tags, 8-bytes aligned */ |
| 49 | __u64 in_max_high; |
| 50 | struct interval_node_extent { |
| 51 | __u64 start; |
| 52 | __u64 end; |
| 53 | } in_extent; |
| 54 | }; |
| 55 | |
| 56 | enum interval_iter { |
| 57 | INTERVAL_ITER_CONT = 1, |
| 58 | INTERVAL_ITER_STOP = 2 |
| 59 | }; |
| 60 | |
| 61 | static inline int interval_is_intree(struct interval_node *node) |
| 62 | { |
| 63 | return node->in_intree == 1; |
| 64 | } |
| 65 | |
James Simmons | 7a742d0 | 2016-09-18 16:37:28 -0400 | [diff] [blame] | 66 | static inline __u64 interval_low(struct interval_node *node) |
| 67 | { |
| 68 | return node->in_extent.start; |
| 69 | } |
| 70 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 71 | static inline __u64 interval_high(struct interval_node *node) |
| 72 | { |
| 73 | return node->in_extent.end; |
| 74 | } |
| 75 | |
| 76 | static inline void interval_set(struct interval_node *node, |
| 77 | __u64 start, __u64 end) |
| 78 | { |
| 79 | LASSERT(start <= end); |
| 80 | node->in_extent.start = start; |
| 81 | node->in_extent.end = end; |
| 82 | node->in_max_high = end; |
| 83 | } |
| 84 | |
James Simmons | 7a742d0 | 2016-09-18 16:37:28 -0400 | [diff] [blame] | 85 | /* |
| 86 | * Rules to write an interval callback. |
| 87 | * - the callback returns INTERVAL_ITER_STOP when it thinks the iteration |
| 88 | * should be stopped. It will then cause the iteration function to return |
| 89 | * immediately with return value INTERVAL_ITER_STOP. |
| 90 | * - callbacks for interval_iterate and interval_iterate_reverse: Every |
| 91 | * nodes in the tree will be set to @node before the callback being called |
| 92 | * - callback for interval_search: Only overlapped node will be set to @node |
| 93 | * before the callback being called. |
| 94 | */ |
| 95 | typedef enum interval_iter (*interval_callback_t)(struct interval_node *node, |
| 96 | void *args); |
| 97 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 98 | struct interval_node *interval_insert(struct interval_node *node, |
| 99 | struct interval_node **root); |
| 100 | void interval_erase(struct interval_node *node, struct interval_node **root); |
| 101 | |
James Simmons | 7a742d0 | 2016-09-18 16:37:28 -0400 | [diff] [blame] | 102 | /* |
| 103 | * Search the extents in the tree and call @func for each overlapped |
| 104 | * extents. |
| 105 | */ |
| 106 | enum interval_iter interval_search(struct interval_node *root, |
| 107 | struct interval_node_extent *ex, |
| 108 | interval_callback_t func, void *data); |
| 109 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 110 | #endif |