inode-map.c 2.5 KB

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