inode-map.c 3.2 KB

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