1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/interval_tree.c
38 * Interval tree library used by ldlm extent lock code
40 * Author: Huang Wei <huangwei@clusterfs.com>
41 * Author: Jay Xiong <jinshan.xiong@sun.com>
44 # include <lustre_dlm.h>
46 # include <liblustre.h>
47 # include <libcfs/kp30.h>
49 #include <obd_support.h>
50 #include <interval_tree.h>
57 static inline int node_is_left_child(struct interval_node *node)
59 LASSERT(node->in_parent != NULL);
60 return node == node->in_parent->in_left;
63 static inline int node_is_right_child(struct interval_node *node)
65 LASSERT(node->in_parent != NULL);
66 return node == node->in_parent->in_right;
69 static inline int node_is_red(struct interval_node *node)
71 return node->in_color == INTERVAL_RED;
74 static inline int node_is_black(struct interval_node *node)
76 return node->in_color == INTERVAL_BLACK;
79 static inline int extent_compare(struct interval_node_extent *e1,
80 struct interval_node_extent *e2)
83 if (e1->start == e2->start) {
84 if (e1->end < e2->end)
86 else if (e1->end > e2->end)
91 if (e1->start < e2->start)
99 static inline int extent_equal(struct interval_node_extent *e1,
100 struct interval_node_extent *e2)
102 return (e1->start == e2->start) && (e1->end == e2->end);
105 static inline int extent_overlapped(struct interval_node_extent *e1,
106 struct interval_node_extent *e2)
108 return (e1->start <= e2->end) && (e2->start <= e1->end);
111 static inline int node_compare(struct interval_node *n1,
112 struct interval_node *n2)
114 return extent_compare(&n1->in_extent, &n2->in_extent);
117 static inline int node_equal(struct interval_node *n1,
118 struct interval_node *n2)
120 return extent_equal(&n1->in_extent, &n2->in_extent);
123 static inline __u64 max_u64(__u64 x, __u64 y)
125 return x > y ? x : y;
128 static inline __u64 min_u64(__u64 x, __u64 y)
130 return x < y ? x : y;
133 #define interval_for_each(node, root) \
134 for (node = interval_first(root); node != NULL; \
135 node = interval_next(node))
137 #define interval_for_each_reverse(node, root) \
138 for (node = interval_last(root); node != NULL; \
139 node = interval_prev(node))
141 static struct interval_node *interval_first(struct interval_node *node)
147 while (node->in_left)
148 node = node->in_left;
152 static struct interval_node *interval_last(struct interval_node *node)
158 while (node->in_right)
159 node = node->in_right;
163 static struct interval_node *interval_next(struct interval_node *node)
170 RETURN(interval_first(node->in_right));
171 while (node->in_parent && node_is_right_child(node))
172 node = node->in_parent;
173 RETURN(node->in_parent);
176 static struct interval_node *interval_prev(struct interval_node *node)
184 RETURN(interval_last(node->in_left));
186 while (node->in_parent && node_is_left_child(node))
187 node = node->in_parent;
189 RETURN(node->in_parent);
192 enum interval_iter interval_iterate(struct interval_node *root,
193 interval_callback_t func,
196 struct interval_node *node;
197 enum interval_iter rc = INTERVAL_ITER_CONT;
200 interval_for_each(node, root) {
201 rc = func(node, data);
202 if (rc == INTERVAL_ITER_STOP)
208 EXPORT_SYMBOL(interval_iterate);
210 enum interval_iter interval_iterate_reverse(struct interval_node *root,
211 interval_callback_t func,
214 struct interval_node *node;
215 enum interval_iter rc = INTERVAL_ITER_CONT;
218 interval_for_each_reverse(node, root) {
219 rc = func(node, data);
220 if (rc == INTERVAL_ITER_STOP)
226 EXPORT_SYMBOL(interval_iterate_reverse);
228 /* try to find a node with same interval in the tree,
229 * if found, return the pointer to the node, otherwise return NULL*/
230 struct interval_node *interval_find(struct interval_node *root,
231 struct interval_node_extent *ex)
233 struct interval_node *walk = root;
238 rc = extent_compare(ex, &walk->in_extent);
242 walk = walk->in_left;
244 walk = walk->in_right;
249 EXPORT_SYMBOL(interval_find);
251 static void __rotate_change_maxhigh(struct interval_node *node,
252 struct interval_node *rotate)
254 __u64 left_max, right_max;
256 rotate->in_max_high = node->in_max_high;
257 left_max = node->in_left ? node->in_left->in_max_high : 0;
258 right_max = node->in_right ? node->in_right->in_max_high : 0;
259 node->in_max_high = max_u64(interval_high(node),
260 max_u64(left_max,right_max));
263 /* The left rotation "pivots" around the link from node to node->right, and
264 * - node will be linked to node->right's left child, and
265 * - node->right's left child will be linked to node's right child. */
266 static void __rotate_left(struct interval_node *node,
267 struct interval_node **root)
269 struct interval_node *right = node->in_right;
270 struct interval_node *parent = node->in_parent;
272 node->in_right = right->in_left;
274 right->in_left->in_parent = node;
276 right->in_left = node;
277 right->in_parent = parent;
279 if (node_is_left_child(node))
280 parent->in_left = right;
282 parent->in_right = right;
286 node->in_parent = right;
288 /* update max_high for node and right */
289 __rotate_change_maxhigh(node, right);
292 /* The right rotation "pivots" around the link from node to node->left, and
293 * - node will be linked to node->left's right child, and
294 * - node->left's right child will be linked to node's left child. */
295 static void __rotate_right(struct interval_node *node,
296 struct interval_node **root)
298 struct interval_node *left = node->in_left;
299 struct interval_node *parent = node->in_parent;
301 node->in_left = left->in_right;
303 left->in_right->in_parent = node;
304 left->in_right = node;
306 left->in_parent = parent;
308 if (node_is_right_child(node))
309 parent->in_right = left;
311 parent->in_left = left;
315 node->in_parent = left;
317 /* update max_high for node and left */
318 __rotate_change_maxhigh(node, left);
321 #define interval_swap(a, b) do { \
322 struct interval_node *c = a; a = b; b = c; \
326 * Operations INSERT and DELETE, when run on a tree with n keys,
327 * take O(logN) time.Because they modify the tree, the result
328 * may violate the red-black properties.To restore these properties,
329 * we must change the colors of some of the nodes in the tree
330 * and also change the pointer structure.
332 static void interval_insert_color(struct interval_node *node,
333 struct interval_node **root)
335 struct interval_node *parent, *gparent;
338 while ((parent = node->in_parent) && node_is_red(parent)) {
339 gparent = parent->in_parent;
340 /* Parent is RED, so gparent must not be NULL */
341 if (node_is_left_child(parent)) {
342 struct interval_node *uncle;
343 uncle = gparent->in_right;
344 if (uncle && node_is_red(uncle)) {
345 uncle->in_color = INTERVAL_BLACK;
346 parent->in_color = INTERVAL_BLACK;
347 gparent->in_color = INTERVAL_RED;
352 if (parent->in_right == node) {
353 __rotate_left(parent, root);
354 interval_swap(node, parent);
357 parent->in_color = INTERVAL_BLACK;
358 gparent->in_color = INTERVAL_RED;
359 __rotate_right(gparent, root);
361 struct interval_node *uncle;
362 uncle = gparent->in_left;
363 if (uncle && node_is_red(uncle)) {
364 uncle->in_color = INTERVAL_BLACK;
365 parent->in_color = INTERVAL_BLACK;
366 gparent->in_color = INTERVAL_RED;
371 if (node_is_left_child(node)) {
372 __rotate_right(parent, root);
373 interval_swap(node, parent);
376 parent->in_color = INTERVAL_BLACK;
377 gparent->in_color = INTERVAL_RED;
378 __rotate_left(gparent, root);
382 (*root)->in_color = INTERVAL_BLACK;
386 struct interval_node *interval_insert(struct interval_node *node,
387 struct interval_node **root)
389 struct interval_node **p, *parent = NULL;
392 LASSERT(!interval_is_intree(node));
396 if (node_equal(parent, node))
399 /* max_high field must be updated after each iteration */
400 if (parent->in_max_high < interval_high(node))
401 parent->in_max_high = interval_high(node);
403 if (node_compare(node, parent) < 0)
404 p = &parent->in_left;
406 p = &parent->in_right;
409 /* link node into the tree */
410 node->in_parent = parent;
411 node->in_color = INTERVAL_RED;
412 node->in_left = node->in_right = NULL;
415 interval_insert_color(node, root);
420 EXPORT_SYMBOL(interval_insert);
422 static inline int node_is_black_or_0(struct interval_node *node)
424 return !node || node_is_black(node);
427 static void interval_erase_color(struct interval_node *node,
428 struct interval_node *parent,
429 struct interval_node **root)
431 struct interval_node *tmp;
434 while (node_is_black_or_0(node) && node != *root) {
435 if (parent->in_left == node) {
436 tmp = parent->in_right;
437 if (node_is_red(tmp)) {
438 tmp->in_color = INTERVAL_BLACK;
439 parent->in_color = INTERVAL_RED;
440 __rotate_left(parent, root);
441 tmp = parent->in_right;
443 if (node_is_black_or_0(tmp->in_left) &&
444 node_is_black_or_0(tmp->in_right)) {
445 tmp->in_color = INTERVAL_RED;
447 parent = node->in_parent;
449 if (node_is_black_or_0(tmp->in_right)) {
450 struct interval_node *o_left;
451 if ((o_left = tmp->in_left))
452 o_left->in_color = INTERVAL_BLACK;
453 tmp->in_color = INTERVAL_RED;
454 __rotate_right(tmp, root);
455 tmp = parent->in_right;
457 tmp->in_color = parent->in_color;
458 parent->in_color = INTERVAL_BLACK;
460 tmp->in_right->in_color = INTERVAL_BLACK;
461 __rotate_left(parent, root);
466 tmp = parent->in_left;
467 if (node_is_red(tmp)) {
468 tmp->in_color = INTERVAL_BLACK;
469 parent->in_color = INTERVAL_RED;
470 __rotate_right(parent, root);
471 tmp = parent->in_left;
473 if (node_is_black_or_0(tmp->in_left) &&
474 node_is_black_or_0(tmp->in_right)) {
475 tmp->in_color = INTERVAL_RED;
477 parent = node->in_parent;
479 if (node_is_black_or_0(tmp->in_left)) {
480 struct interval_node *o_right;
481 if ((o_right = tmp->in_right))
482 o_right->in_color = INTERVAL_BLACK;
483 tmp->in_color = INTERVAL_RED;
484 __rotate_left(tmp, root);
485 tmp = parent->in_left;
487 tmp->in_color = parent->in_color;
488 parent->in_color = INTERVAL_BLACK;
490 tmp->in_left->in_color = INTERVAL_BLACK;
491 __rotate_right(parent, root);
498 node->in_color = INTERVAL_BLACK;
503 * if the @max_high value of @node is changed, this function traverse a path
504 * from node up to the root to update max_high for the whole tree.
506 static void update_maxhigh(struct interval_node *node,
509 __u64 left_max, right_max;
513 left_max = node->in_left ? node->in_left->in_max_high : 0;
514 right_max = node->in_right ? node->in_right->in_max_high : 0;
515 node->in_max_high = max_u64(interval_high(node),
516 max_u64(left_max, right_max));
518 if (node->in_max_high >= old_maxhigh)
520 node = node->in_parent;
525 void interval_erase(struct interval_node *node,
526 struct interval_node **root)
528 struct interval_node *child, *parent;
532 LASSERT(interval_is_intree(node));
534 if (!node->in_left) {
535 child = node->in_right;
536 } else if (!node->in_right) {
537 child = node->in_left;
538 } else { /* Both left and right child are not NULL */
539 struct interval_node *old = node;
541 node = interval_next(node);
542 child = node->in_right;
543 parent = node->in_parent;
544 color = node->in_color;
547 child->in_parent = parent;
549 parent->in_right = child;
552 parent->in_left = child;
555 node->in_color = old->in_color;
556 node->in_right = old->in_right;
557 node->in_left = old->in_left;
558 node->in_parent = old->in_parent;
560 if (old->in_parent) {
561 if (node_is_left_child(old))
562 old->in_parent->in_left = node;
564 old->in_parent->in_right = node;
569 old->in_left->in_parent = node;
571 old->in_right->in_parent = node;
572 update_maxhigh(child, node->in_max_high);
573 update_maxhigh(node, old->in_max_high);
576 parent = node->in_parent;
577 color = node->in_color;
580 child->in_parent = parent;
582 if (node_is_left_child(node))
583 parent->in_left = child;
585 parent->in_right = child;
590 update_maxhigh(child, node->in_max_high);
593 if (color == INTERVAL_BLACK)
594 interval_erase_color(child, parent, root);
597 EXPORT_SYMBOL(interval_erase);
599 static inline int interval_may_overlap(struct interval_node *node,
600 struct interval_node_extent *ext)
602 return (ext->start <= node->in_max_high &&
603 ext->end >= interval_low(node));
607 * This function finds all intervals that overlap interval ext,
608 * and calls func to handle resulted intervals one by one.
609 * in lustre, this function will find all conflicting locks in
610 * the granted queue and add these locks to the ast work list.
615 * if (ext->end < interval_low(node)) {
616 * interval_search(node->in_left, ext, func, data);
617 * } else if (interval_may_overlap(node, ext)) {
618 * if (extent_overlapped(ext, &node->in_extent))
620 * interval_search(node->in_left, ext, func, data);
621 * interval_search(node->in_right, ext, func, data);
627 enum interval_iter interval_search(struct interval_node *node,
628 struct interval_node_extent *ext,
629 interval_callback_t func,
632 struct interval_node *parent;
633 enum interval_iter rc = INTERVAL_ITER_CONT;
635 LASSERT(ext != NULL);
636 LASSERT(func != NULL);
639 if (ext->end < interval_low(node)) {
641 node = node->in_left;
644 } else if (interval_may_overlap(node, ext)) {
645 if (extent_overlapped(ext, &node->in_extent)) {
646 rc = func(node, data);
647 if (rc == INTERVAL_ITER_STOP)
652 node = node->in_left;
655 if (node->in_right) {
656 node = node->in_right;
661 parent = node->in_parent;
663 if (node_is_left_child(node) &&
665 /* If we ever got the left, it means that the
666 * parent met ext->end<interval_low(parent), or
667 * may_overlap(parent). If the former is true,
668 * we needn't go back. So stop early and check
669 * may_overlap(parent) after this loop. */
670 node = parent->in_right;
674 parent = parent->in_parent;
676 if (parent == NULL || !interval_may_overlap(parent, ext))
682 EXPORT_SYMBOL(interval_search);
684 static enum interval_iter interval_overlap_cb(struct interval_node *n,
688 return INTERVAL_ITER_STOP;
691 int interval_is_overlapped(struct interval_node *root,
692 struct interval_node_extent *ext)
695 (void)interval_search(root, ext, interval_overlap_cb, &has);
698 EXPORT_SYMBOL(interval_is_overlapped);
700 /* Don't expand to low. Expanding downwards is expensive, and meaningless to
701 * some extents, because programs seldom do IO backward.
703 * The recursive algorithm of expanding low:
704 * interval_expand_low {
705 * struct interval_node *tmp;
706 * static __u64 res = 0;
710 * if (root->in_max_high < low) {
711 * res = max_u64(root->in_max_high + 1, res);
713 * } else if (low < interval_low(root)) {
714 * interval_expand_low(root->in_left, low);
718 * if (interval_high(root) < low)
719 * res = max_u64(interval_high(root) + 1, res);
720 * interval_expand_low(root->in_left, low);
721 * interval_expand_low(root->in_right, low);
726 * It's much easy to eliminate the recursion, see interval_search for
729 static inline __u64 interval_expand_low(struct interval_node *root, __u64 low)
731 /* we only concern the empty tree right now. */
737 static inline __u64 interval_expand_high(struct interval_node *node, __u64 high)
741 while (node != NULL) {
742 if (node->in_max_high < high)
745 if (interval_low(node) > high) {
746 result = interval_low(node) - 1;
747 node = node->in_left;
749 node = node->in_right;
756 /* expanding the extent based on @ext. */
757 void interval_expand(struct interval_node *root,
758 struct interval_node_extent *ext,
759 struct interval_node_extent *limiter)
761 /* The assertion of interval_is_overlapped is expensive because we may
762 * travel many nodes to find the overlapped node. */
763 LASSERT(interval_is_overlapped(root, ext) == 0);
764 if (!limiter || limiter->start < ext->start)
765 ext->start = interval_expand_low(root, ext->start);
766 if (!limiter || limiter->end > ext->end)
767 ext->end = interval_expand_high(root, ext->end);
768 LASSERT(interval_is_overlapped(root, ext) == 0);
770 EXPORT_SYMBOL(interval_expand);