4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/ldlm/interval_tree.c
36 * Interval tree library used by ldlm extent lock code
38 * Author: Huang Wei <huangwei@clusterfs.com>
39 * Author: Jay Xiong <jinshan.xiong@sun.com>
41 #include "../include/lustre_dlm.h"
42 #include "../include/obd_support.h"
43 #include "../include/interval_tree.h"
50 static inline int node_is_left_child(struct interval_node *node)
52 LASSERT(node->in_parent != NULL);
53 return node == node->in_parent->in_left;
56 static inline int node_is_right_child(struct interval_node *node)
58 LASSERT(node->in_parent != NULL);
59 return node == node->in_parent->in_right;
62 static inline int node_is_red(struct interval_node *node)
64 return node->in_color == INTERVAL_RED;
67 static inline int node_is_black(struct interval_node *node)
69 return node->in_color == INTERVAL_BLACK;
72 static inline int extent_compare(struct interval_node_extent *e1,
73 struct interval_node_extent *e2)
77 if (e1->start == e2->start) {
78 if (e1->end < e2->end)
80 else if (e1->end > e2->end)
85 if (e1->start < e2->start)
93 static inline int extent_equal(struct interval_node_extent *e1,
94 struct interval_node_extent *e2)
96 return (e1->start == e2->start) && (e1->end == e2->end);
99 static inline int extent_overlapped(struct interval_node_extent *e1,
100 struct interval_node_extent *e2)
102 return (e1->start <= e2->end) && (e2->start <= e1->end);
105 static inline int node_compare(struct interval_node *n1,
106 struct interval_node *n2)
108 return extent_compare(&n1->in_extent, &n2->in_extent);
111 static inline int node_equal(struct interval_node *n1,
112 struct interval_node *n2)
114 return extent_equal(&n1->in_extent, &n2->in_extent);
117 static inline __u64 max_u64(__u64 x, __u64 y)
119 return x > y ? x : y;
122 static inline __u64 min_u64(__u64 x, __u64 y)
124 return x < y ? x : y;
127 #define interval_for_each(node, root) \
128 for (node = interval_first(root); node != NULL; \
129 node = interval_next(node))
131 #define interval_for_each_reverse(node, root) \
132 for (node = interval_last(root); node != NULL; \
133 node = interval_prev(node))
135 static struct interval_node *interval_first(struct interval_node *node)
139 while (node->in_left)
140 node = node->in_left;
144 static struct interval_node *interval_last(struct interval_node *node)
148 while (node->in_right)
149 node = node->in_right;
153 static struct interval_node *interval_next(struct interval_node *node)
158 return interval_first(node->in_right);
159 while (node->in_parent && node_is_right_child(node))
160 node = node->in_parent;
161 return node->in_parent;
164 static struct interval_node *interval_prev(struct interval_node *node)
170 return interval_last(node->in_left);
172 while (node->in_parent && node_is_left_child(node))
173 node = node->in_parent;
175 return node->in_parent;
178 enum interval_iter interval_iterate(struct interval_node *root,
179 interval_callback_t func,
182 struct interval_node *node;
183 enum interval_iter rc = INTERVAL_ITER_CONT;
185 interval_for_each(node, root) {
186 rc = func(node, data);
187 if (rc == INTERVAL_ITER_STOP)
193 EXPORT_SYMBOL(interval_iterate);
195 enum interval_iter interval_iterate_reverse(struct interval_node *root,
196 interval_callback_t func,
199 struct interval_node *node;
200 enum interval_iter rc = INTERVAL_ITER_CONT;
202 interval_for_each_reverse(node, root) {
203 rc = func(node, data);
204 if (rc == INTERVAL_ITER_STOP)
210 EXPORT_SYMBOL(interval_iterate_reverse);
212 /* try to find a node with same interval in the tree,
213 * if found, return the pointer to the node, otherwise return NULL*/
214 struct interval_node *interval_find(struct interval_node *root,
215 struct interval_node_extent *ex)
217 struct interval_node *walk = root;
221 rc = extent_compare(ex, &walk->in_extent);
225 walk = walk->in_left;
227 walk = walk->in_right;
232 EXPORT_SYMBOL(interval_find);
234 static void __rotate_change_maxhigh(struct interval_node *node,
235 struct interval_node *rotate)
237 __u64 left_max, right_max;
239 rotate->in_max_high = node->in_max_high;
240 left_max = node->in_left ? node->in_left->in_max_high : 0;
241 right_max = node->in_right ? node->in_right->in_max_high : 0;
242 node->in_max_high = max_u64(interval_high(node),
243 max_u64(left_max, right_max));
246 /* The left rotation "pivots" around the link from node to node->right, and
247 * - node will be linked to node->right's left child, and
248 * - node->right's left child will be linked to node's right child. */
249 static void __rotate_left(struct interval_node *node,
250 struct interval_node **root)
252 struct interval_node *right = node->in_right;
253 struct interval_node *parent = node->in_parent;
255 node->in_right = right->in_left;
257 right->in_left->in_parent = node;
259 right->in_left = node;
260 right->in_parent = parent;
262 if (node_is_left_child(node))
263 parent->in_left = right;
265 parent->in_right = right;
269 node->in_parent = right;
271 /* update max_high for node and right */
272 __rotate_change_maxhigh(node, right);
275 /* The right rotation "pivots" around the link from node to node->left, and
276 * - node will be linked to node->left's right child, and
277 * - node->left's right child will be linked to node's left child. */
278 static void __rotate_right(struct interval_node *node,
279 struct interval_node **root)
281 struct interval_node *left = node->in_left;
282 struct interval_node *parent = node->in_parent;
284 node->in_left = left->in_right;
286 left->in_right->in_parent = node;
287 left->in_right = node;
289 left->in_parent = parent;
291 if (node_is_right_child(node))
292 parent->in_right = left;
294 parent->in_left = left;
298 node->in_parent = left;
300 /* update max_high for node and left */
301 __rotate_change_maxhigh(node, left);
304 #define interval_swap(a, b) do { \
305 struct interval_node *c = a; a = b; b = c; \
309 * Operations INSERT and DELETE, when run on a tree with n keys,
310 * take O(logN) time.Because they modify the tree, the result
311 * may violate the red-black properties.To restore these properties,
312 * we must change the colors of some of the nodes in the tree
313 * and also change the pointer structure.
315 static void interval_insert_color(struct interval_node *node,
316 struct interval_node **root)
318 struct interval_node *parent, *gparent;
320 while ((parent = node->in_parent) && node_is_red(parent)) {
321 gparent = parent->in_parent;
322 /* Parent is RED, so gparent must not be NULL */
323 if (node_is_left_child(parent)) {
324 struct interval_node *uncle;
326 uncle = gparent->in_right;
327 if (uncle && node_is_red(uncle)) {
328 uncle->in_color = INTERVAL_BLACK;
329 parent->in_color = INTERVAL_BLACK;
330 gparent->in_color = INTERVAL_RED;
335 if (parent->in_right == node) {
336 __rotate_left(parent, root);
337 interval_swap(node, parent);
340 parent->in_color = INTERVAL_BLACK;
341 gparent->in_color = INTERVAL_RED;
342 __rotate_right(gparent, root);
344 struct interval_node *uncle;
346 uncle = gparent->in_left;
347 if (uncle && node_is_red(uncle)) {
348 uncle->in_color = INTERVAL_BLACK;
349 parent->in_color = INTERVAL_BLACK;
350 gparent->in_color = INTERVAL_RED;
355 if (node_is_left_child(node)) {
356 __rotate_right(parent, root);
357 interval_swap(node, parent);
360 parent->in_color = INTERVAL_BLACK;
361 gparent->in_color = INTERVAL_RED;
362 __rotate_left(gparent, root);
366 (*root)->in_color = INTERVAL_BLACK;
369 struct interval_node *interval_insert(struct interval_node *node,
370 struct interval_node **root)
373 struct interval_node **p, *parent = NULL;
375 LASSERT(!interval_is_intree(node));
379 if (node_equal(parent, node))
382 /* max_high field must be updated after each iteration */
383 if (parent->in_max_high < interval_high(node))
384 parent->in_max_high = interval_high(node);
386 if (node_compare(node, parent) < 0)
387 p = &parent->in_left;
389 p = &parent->in_right;
392 /* link node into the tree */
393 node->in_parent = parent;
394 node->in_color = INTERVAL_RED;
395 node->in_left = node->in_right = NULL;
398 interval_insert_color(node, root);
403 EXPORT_SYMBOL(interval_insert);
405 static inline int node_is_black_or_0(struct interval_node *node)
407 return !node || node_is_black(node);
410 static void interval_erase_color(struct interval_node *node,
411 struct interval_node *parent,
412 struct interval_node **root)
414 struct interval_node *tmp;
416 while (node_is_black_or_0(node) && node != *root) {
417 if (parent->in_left == node) {
418 tmp = parent->in_right;
419 if (node_is_red(tmp)) {
420 tmp->in_color = INTERVAL_BLACK;
421 parent->in_color = INTERVAL_RED;
422 __rotate_left(parent, root);
423 tmp = parent->in_right;
425 if (node_is_black_or_0(tmp->in_left) &&
426 node_is_black_or_0(tmp->in_right)) {
427 tmp->in_color = INTERVAL_RED;
429 parent = node->in_parent;
431 if (node_is_black_or_0(tmp->in_right)) {
432 struct interval_node *o_left;
434 o_left = tmp->in_left;
436 o_left->in_color = INTERVAL_BLACK;
437 tmp->in_color = INTERVAL_RED;
438 __rotate_right(tmp, root);
439 tmp = parent->in_right;
441 tmp->in_color = parent->in_color;
442 parent->in_color = INTERVAL_BLACK;
444 tmp->in_right->in_color = INTERVAL_BLACK;
445 __rotate_left(parent, root);
450 tmp = parent->in_left;
451 if (node_is_red(tmp)) {
452 tmp->in_color = INTERVAL_BLACK;
453 parent->in_color = INTERVAL_RED;
454 __rotate_right(parent, root);
455 tmp = parent->in_left;
457 if (node_is_black_or_0(tmp->in_left) &&
458 node_is_black_or_0(tmp->in_right)) {
459 tmp->in_color = INTERVAL_RED;
461 parent = node->in_parent;
463 if (node_is_black_or_0(tmp->in_left)) {
464 struct interval_node *o_right;
466 o_right = tmp->in_right;
468 o_right->in_color = INTERVAL_BLACK;
469 tmp->in_color = INTERVAL_RED;
470 __rotate_left(tmp, root);
471 tmp = parent->in_left;
473 tmp->in_color = parent->in_color;
474 parent->in_color = INTERVAL_BLACK;
476 tmp->in_left->in_color = INTERVAL_BLACK;
477 __rotate_right(parent, root);
484 node->in_color = INTERVAL_BLACK;
488 * if the @max_high value of @node is changed, this function traverse a path
489 * from node up to the root to update max_high for the whole tree.
491 static void update_maxhigh(struct interval_node *node,
494 __u64 left_max, right_max;
497 left_max = node->in_left ? node->in_left->in_max_high : 0;
498 right_max = node->in_right ? node->in_right->in_max_high : 0;
499 node->in_max_high = max_u64(interval_high(node),
500 max_u64(left_max, right_max));
502 if (node->in_max_high >= old_maxhigh)
504 node = node->in_parent;
508 void interval_erase(struct interval_node *node,
509 struct interval_node **root)
511 struct interval_node *child, *parent;
514 LASSERT(interval_is_intree(node));
516 if (!node->in_left) {
517 child = node->in_right;
518 } else if (!node->in_right) {
519 child = node->in_left;
520 } else { /* Both left and right child are not NULL */
521 struct interval_node *old = node;
523 node = interval_next(node);
524 child = node->in_right;
525 parent = node->in_parent;
526 color = node->in_color;
529 child->in_parent = parent;
531 parent->in_right = child;
533 parent->in_left = child;
535 node->in_color = old->in_color;
536 node->in_right = old->in_right;
537 node->in_left = old->in_left;
538 node->in_parent = old->in_parent;
540 if (old->in_parent) {
541 if (node_is_left_child(old))
542 old->in_parent->in_left = node;
544 old->in_parent->in_right = node;
549 old->in_left->in_parent = node;
551 old->in_right->in_parent = node;
552 update_maxhigh(child ? : parent, node->in_max_high);
553 update_maxhigh(node, old->in_max_high);
558 parent = node->in_parent;
559 color = node->in_color;
562 child->in_parent = parent;
564 if (node_is_left_child(node))
565 parent->in_left = child;
567 parent->in_right = child;
572 update_maxhigh(child ? : parent, node->in_max_high);
575 if (color == INTERVAL_BLACK)
576 interval_erase_color(child, parent, root);
578 EXPORT_SYMBOL(interval_erase);
580 static inline int interval_may_overlap(struct interval_node *node,
581 struct interval_node_extent *ext)
583 return (ext->start <= node->in_max_high &&
584 ext->end >= interval_low(node));
588 * This function finds all intervals that overlap interval ext,
589 * and calls func to handle resulted intervals one by one.
590 * in lustre, this function will find all conflicting locks in
591 * the granted queue and add these locks to the ast work list.
596 * if (ext->end < interval_low(node)) {
597 * interval_search(node->in_left, ext, func, data);
598 * } else if (interval_may_overlap(node, ext)) {
599 * if (extent_overlapped(ext, &node->in_extent))
601 * interval_search(node->in_left, ext, func, data);
602 * interval_search(node->in_right, ext, func, data);
608 enum interval_iter interval_search(struct interval_node *node,
609 struct interval_node_extent *ext,
610 interval_callback_t func,
613 struct interval_node *parent;
614 enum interval_iter rc = INTERVAL_ITER_CONT;
616 LASSERT(ext != NULL);
617 LASSERT(func != NULL);
620 if (ext->end < interval_low(node)) {
622 node = node->in_left;
625 } else if (interval_may_overlap(node, ext)) {
626 if (extent_overlapped(ext, &node->in_extent)) {
627 rc = func(node, data);
628 if (rc == INTERVAL_ITER_STOP)
633 node = node->in_left;
636 if (node->in_right) {
637 node = node->in_right;
642 parent = node->in_parent;
644 if (node_is_left_child(node) &&
646 /* If we ever got the left, it means that the
647 * parent met ext->end<interval_low(parent), or
648 * may_overlap(parent). If the former is true,
649 * we needn't go back. So stop early and check
650 * may_overlap(parent) after this loop. */
651 node = parent->in_right;
655 parent = parent->in_parent;
657 if (parent == NULL || !interval_may_overlap(parent, ext))
663 EXPORT_SYMBOL(interval_search);
665 static enum interval_iter interval_overlap_cb(struct interval_node *n,
669 return INTERVAL_ITER_STOP;
672 int interval_is_overlapped(struct interval_node *root,
673 struct interval_node_extent *ext)
676 (void)interval_search(root, ext, interval_overlap_cb, &has);
679 EXPORT_SYMBOL(interval_is_overlapped);
681 /* Don't expand to low. Expanding downwards is expensive, and meaningless to
682 * some extents, because programs seldom do IO backward.
684 * The recursive algorithm of expanding low:
686 * struct interval_node *tmp;
687 * static __u64 res = 0;
691 * if (root->in_max_high < low) {
692 * res = max_u64(root->in_max_high + 1, res);
694 * } else if (low < interval_low(root)) {
695 * interval_expand_low(root->in_left, low);
699 * if (interval_high(root) < low)
700 * res = max_u64(interval_high(root) + 1, res);
701 * interval_expand_low(root->in_left, low);
702 * interval_expand_low(root->in_right, low);
707 * It's much easy to eliminate the recursion, see interval_search for
710 static inline __u64 interval_expand_low(struct interval_node *root, __u64 low)
712 /* we only concern the empty tree right now. */
718 static inline __u64 interval_expand_high(struct interval_node *node, __u64 high)
722 while (node != NULL) {
723 if (node->in_max_high < high)
726 if (interval_low(node) > high) {
727 result = interval_low(node) - 1;
728 node = node->in_left;
730 node = node->in_right;
737 /* expanding the extent based on @ext. */
738 void interval_expand(struct interval_node *root,
739 struct interval_node_extent *ext,
740 struct interval_node_extent *limiter)
742 /* The assertion of interval_is_overlapped is expensive because we may
743 * travel many nodes to find the overlapped node. */
744 LASSERT(interval_is_overlapped(root, ext) == 0);
745 if (!limiter || limiter->start < ext->start)
746 ext->start = interval_expand_low(root, ext->start);
747 if (!limiter || limiter->end > ext->end)
748 ext->end = interval_expand_high(root, ext->end);
749 LASSERT(interval_is_overlapped(root, ext) == 0);
751 EXPORT_SYMBOL(interval_expand);