ref-cache.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (C) 2008 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. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/sort.h>
  21. #include "ctree.h"
  22. #include "ref-cache.h"
  23. #include "transaction.h"
  24. static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
  25. struct rb_node *node)
  26. {
  27. struct rb_node **p = &root->rb_node;
  28. struct rb_node *parent = NULL;
  29. struct btrfs_leaf_ref *entry;
  30. while (*p) {
  31. parent = *p;
  32. entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
  33. if (bytenr < entry->bytenr)
  34. p = &(*p)->rb_left;
  35. else if (bytenr > entry->bytenr)
  36. p = &(*p)->rb_right;
  37. else
  38. return parent;
  39. }
  40. entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
  41. rb_link_node(node, parent, p);
  42. rb_insert_color(node, root);
  43. return NULL;
  44. }
  45. static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
  46. {
  47. struct rb_node *n = root->rb_node;
  48. struct btrfs_leaf_ref *entry;
  49. while (n) {
  50. entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
  51. WARN_ON(!entry->in_tree);
  52. if (bytenr < entry->bytenr)
  53. n = n->rb_left;
  54. else if (bytenr > entry->bytenr)
  55. n = n->rb_right;
  56. else
  57. return n;
  58. }
  59. return NULL;
  60. }