inode-map.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "kerncompat.h"
  4. #include "radix-tree.h"
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. #include "transaction.h"
  8. /*
  9. * walks the btree of allocated inodes and find a hole.
  10. */
  11. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  12. struct btrfs_root *fs_root,
  13. u64 dirid, u64 *objectid)
  14. {
  15. struct btrfs_path path;
  16. struct btrfs_key key;
  17. int ret;
  18. u64 hole_size = 0;
  19. int slot = 0;
  20. u64 last_ino;
  21. int start_found;
  22. struct btrfs_leaf *l;
  23. struct btrfs_root *root = fs_root->fs_info->inode_root;
  24. struct btrfs_key search_key;
  25. u64 search_start = dirid;
  26. if (fs_root->fs_info->last_inode_alloc_dirid == dirid)
  27. search_start = fs_root->fs_info->last_inode_alloc;
  28. search_key.objectid = search_start;
  29. search_key.flags = 0;
  30. btrfs_set_key_type(&search_key, BTRFS_INODE_MAP_ITEM_KEY);
  31. search_key.offset = 0;
  32. btrfs_init_path(&path);
  33. start_found = 0;
  34. ret = btrfs_search_slot(trans, root, &search_key, &path, 0, 0);
  35. if (ret < 0)
  36. goto error;
  37. if (path.slots[0] > 0)
  38. path.slots[0]--;
  39. while (1) {
  40. l = &path.nodes[0]->leaf;
  41. slot = path.slots[0];
  42. if (slot >= btrfs_header_nritems(&l->header)) {
  43. ret = btrfs_next_leaf(root, &path);
  44. if (ret == 0)
  45. continue;
  46. if (ret < 0)
  47. goto error;
  48. if (!start_found) {
  49. *objectid = search_start;
  50. start_found = 1;
  51. goto found;
  52. }
  53. *objectid = last_ino > search_start ?
  54. last_ino : search_start;
  55. goto found;
  56. }
  57. btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
  58. if (key.objectid >= search_start) {
  59. if (start_found) {
  60. if (last_ino < search_start)
  61. last_ino = search_start;
  62. hole_size = key.objectid - last_ino;
  63. if (hole_size > 0) {
  64. *objectid = last_ino;
  65. goto found;
  66. }
  67. }
  68. }
  69. start_found = 1;
  70. last_ino = key.objectid + 1;
  71. path.slots[0]++;
  72. }
  73. // FIXME -ENOSPC
  74. found:
  75. root->fs_info->last_inode_alloc = *objectid;
  76. root->fs_info->last_inode_alloc_dirid = dirid;
  77. btrfs_release_path(root, &path);
  78. BUG_ON(*objectid < search_start);
  79. return 0;
  80. error:
  81. btrfs_release_path(root, &path);
  82. return ret;
  83. }
  84. int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
  85. struct btrfs_root *fs_root,
  86. u64 objectid, struct btrfs_key *location)
  87. {
  88. int ret = 0;
  89. struct btrfs_path path;
  90. struct btrfs_inode_map_item *inode_item;
  91. struct btrfs_key key;
  92. struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
  93. key.objectid = objectid;
  94. key.flags = 0;
  95. btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
  96. key.offset = 0;
  97. btrfs_init_path(&path);
  98. ret = btrfs_insert_empty_item(trans, inode_root, &path, &key,
  99. sizeof(struct btrfs_inode_map_item));
  100. if (ret)
  101. goto out;
  102. inode_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
  103. struct btrfs_inode_map_item);
  104. btrfs_cpu_key_to_disk(&inode_item->key, location);
  105. out:
  106. btrfs_release_path(inode_root, &path);
  107. return ret;
  108. }
  109. int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
  110. struct btrfs_root *fs_root, struct btrfs_path *path,
  111. u64 objectid, int mod)
  112. {
  113. int ret;
  114. struct btrfs_key key;
  115. int ins_len = mod < 0 ? -1 : 0;
  116. int cow = mod != 0;
  117. struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
  118. key.objectid = objectid;
  119. key.flags = 0;
  120. key.offset = 0;
  121. btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
  122. ret = btrfs_search_slot(trans, inode_root, &key, path, ins_len, cow);
  123. return ret;
  124. }