inode-map.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "ctree.h"
  19. #include "disk-io.h"
  20. #include "transaction.h"
  21. int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
  22. {
  23. struct btrfs_path *path;
  24. int ret;
  25. struct extent_buffer *l;
  26. struct btrfs_key search_key;
  27. struct btrfs_key found_key;
  28. int slot;
  29. path = btrfs_alloc_path();
  30. BUG_ON(!path);
  31. search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
  32. search_key.type = -1;
  33. search_key.offset = (u64)-1;
  34. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  35. if (ret < 0)
  36. goto error;
  37. BUG_ON(ret == 0);
  38. if (path->slots[0] > 0) {
  39. slot = path->slots[0] - 1;
  40. l = path->nodes[0];
  41. btrfs_item_key_to_cpu(l, &found_key, slot);
  42. *objectid = found_key.objectid;
  43. } else {
  44. *objectid = BTRFS_FIRST_FREE_OBJECTID;
  45. }
  46. ret = 0;
  47. error:
  48. btrfs_free_path(path);
  49. return ret;
  50. }
  51. /*
  52. * walks the btree of allocated inodes and find a hole.
  53. */
  54. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  55. struct btrfs_root *root,
  56. u64 dirid, u64 *objectid)
  57. {
  58. struct btrfs_path *path;
  59. struct btrfs_key key;
  60. int ret;
  61. int slot = 0;
  62. u64 last_ino = 0;
  63. int start_found;
  64. struct extent_buffer *l;
  65. struct btrfs_key search_key;
  66. u64 search_start = dirid;
  67. mutex_lock(&root->objectid_mutex);
  68. if (root->last_inode_alloc >= BTRFS_FIRST_FREE_OBJECTID &&
  69. root->last_inode_alloc < BTRFS_LAST_FREE_OBJECTID) {
  70. *objectid = ++root->last_inode_alloc;
  71. mutex_unlock(&root->objectid_mutex);
  72. return 0;
  73. }
  74. path = btrfs_alloc_path();
  75. BUG_ON(!path);
  76. search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
  77. search_key.objectid = search_start;
  78. search_key.type = 0;
  79. search_key.offset = 0;
  80. btrfs_init_path(path);
  81. start_found = 0;
  82. ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
  83. if (ret < 0)
  84. goto error;
  85. while (1) {
  86. l = path->nodes[0];
  87. slot = path->slots[0];
  88. if (slot >= btrfs_header_nritems(l)) {
  89. ret = btrfs_next_leaf(root, path);
  90. if (ret == 0)
  91. continue;
  92. if (ret < 0)
  93. goto error;
  94. if (!start_found) {
  95. *objectid = search_start;
  96. start_found = 1;
  97. goto found;
  98. }
  99. *objectid = last_ino > search_start ?
  100. last_ino : search_start;
  101. goto found;
  102. }
  103. btrfs_item_key_to_cpu(l, &key, slot);
  104. if (key.objectid >= search_start) {
  105. if (start_found) {
  106. if (last_ino < search_start)
  107. last_ino = search_start;
  108. if (key.objectid > last_ino) {
  109. *objectid = last_ino;
  110. goto found;
  111. }
  112. } else if (key.objectid > search_start) {
  113. *objectid = search_start;
  114. goto found;
  115. }
  116. }
  117. if (key.objectid >= BTRFS_LAST_FREE_OBJECTID)
  118. break;
  119. start_found = 1;
  120. last_ino = key.objectid + 1;
  121. path->slots[0]++;
  122. }
  123. BUG_ON(1);
  124. found:
  125. btrfs_release_path(root, path);
  126. btrfs_free_path(path);
  127. BUG_ON(*objectid < search_start);
  128. mutex_unlock(&root->objectid_mutex);
  129. return 0;
  130. error:
  131. btrfs_release_path(root, path);
  132. btrfs_free_path(path);
  133. mutex_unlock(&root->objectid_mutex);
  134. return ret;
  135. }