inode-map.c 3.3 KB

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