inode-map.c 3.9 KB

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