inode-map.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
  26. search_key.objectid = search_start;
  27. search_key.flags = 0;
  28. btrfs_set_key_type(&search_key, BTRFS_INODE_MAP_ITEM_KEY);
  29. search_key.offset = 0;
  30. btrfs_init_path(&path);
  31. start_found = 0;
  32. ret = btrfs_search_slot(trans, root, &search_key, &path, 0, 0);
  33. if (ret < 0)
  34. goto error;
  35. if (path.slots[0] > 0)
  36. path.slots[0]--;
  37. while (1) {
  38. l = btrfs_buffer_leaf(path.nodes[0]);
  39. slot = path.slots[0];
  40. if (slot >= btrfs_header_nritems(&l->header)) {
  41. ret = btrfs_next_leaf(root, &path);
  42. if (ret == 0)
  43. continue;
  44. if (ret < 0)
  45. goto error;
  46. if (!start_found) {
  47. *objectid = search_start;
  48. start_found = 1;
  49. goto found;
  50. }
  51. *objectid = last_ino > search_start ?
  52. last_ino : search_start;
  53. goto found;
  54. }
  55. btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
  56. if (key.objectid >= search_start) {
  57. if (start_found) {
  58. if (last_ino < search_start)
  59. last_ino = search_start;
  60. hole_size = key.objectid - last_ino;
  61. if (hole_size > 0) {
  62. *objectid = last_ino;
  63. goto found;
  64. }
  65. }
  66. }
  67. start_found = 1;
  68. last_ino = key.objectid + 1;
  69. path.slots[0]++;
  70. }
  71. // FIXME -ENOSPC
  72. found:
  73. root->fs_info->last_inode_alloc = *objectid;
  74. root->fs_info->last_inode_alloc_dirid = dirid;
  75. btrfs_release_path(root, &path);
  76. BUG_ON(*objectid < search_start);
  77. return 0;
  78. error:
  79. btrfs_release_path(root, &path);
  80. return ret;
  81. }
  82. int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
  83. struct btrfs_root *fs_root,
  84. u64 objectid, struct btrfs_key *location)
  85. {
  86. int ret = 0;
  87. struct btrfs_path path;
  88. struct btrfs_inode_map_item *inode_item;
  89. struct btrfs_key key;
  90. struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
  91. key.objectid = objectid;
  92. key.flags = 0;
  93. btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
  94. key.offset = 0;
  95. btrfs_init_path(&path);
  96. ret = btrfs_insert_empty_item(trans, inode_root, &path, &key,
  97. sizeof(struct btrfs_inode_map_item));
  98. if (ret)
  99. goto out;
  100. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
  101. path.slots[0], struct btrfs_inode_map_item);
  102. btrfs_cpu_key_to_disk(&inode_item->key, location);
  103. mark_buffer_dirty(path.nodes[0]);
  104. out:
  105. btrfs_release_path(inode_root, &path);
  106. return ret;
  107. }
  108. int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
  109. struct btrfs_root *fs_root, struct btrfs_path *path,
  110. u64 objectid, int mod)
  111. {
  112. int ret;
  113. struct btrfs_key key;
  114. int ins_len = mod < 0 ? -1 : 0;
  115. int cow = mod != 0;
  116. struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
  117. key.objectid = objectid;
  118. key.flags = 0;
  119. key.offset = 0;
  120. btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
  121. ret = btrfs_search_slot(trans, inode_root, &key, path, ins_len, cow);
  122. return ret;
  123. }