inode-item.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include "ctree.h"
  19. #include "disk-io.h"
  20. #include "transaction.h"
  21. static int find_name_in_backref(struct btrfs_path *path, const char *name,
  22. int name_len, struct btrfs_inode_ref **ref_ret)
  23. {
  24. struct extent_buffer *leaf;
  25. struct btrfs_inode_ref *ref;
  26. unsigned long ptr;
  27. unsigned long name_ptr;
  28. u32 item_size;
  29. u32 cur_offset = 0;
  30. int len;
  31. leaf = path->nodes[0];
  32. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  33. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  34. while (cur_offset < item_size) {
  35. ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  36. len = btrfs_inode_ref_name_len(leaf, ref);
  37. name_ptr = (unsigned long)(ref + 1);
  38. cur_offset += len + sizeof(*ref);
  39. if (len != name_len)
  40. continue;
  41. if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) {
  42. *ref_ret = ref;
  43. return 1;
  44. }
  45. }
  46. return 0;
  47. }
  48. int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
  49. struct btrfs_root *root,
  50. const char *name, int name_len,
  51. u64 inode_objectid, u64 ref_objectid, u64 *index)
  52. {
  53. struct btrfs_path *path;
  54. struct btrfs_key key;
  55. struct btrfs_inode_ref *ref;
  56. struct extent_buffer *leaf;
  57. unsigned long ptr;
  58. unsigned long item_start;
  59. u32 item_size;
  60. u32 sub_item_len;
  61. int ret;
  62. int del_len = name_len + sizeof(*ref);
  63. key.objectid = inode_objectid;
  64. key.offset = ref_objectid;
  65. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  66. path = btrfs_alloc_path();
  67. if (!path)
  68. return -ENOMEM;
  69. path->leave_spinning = 1;
  70. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  71. if (ret > 0) {
  72. ret = -ENOENT;
  73. goto out;
  74. } else if (ret < 0) {
  75. goto out;
  76. }
  77. if (!find_name_in_backref(path, name, name_len, &ref)) {
  78. ret = -ENOENT;
  79. goto out;
  80. }
  81. leaf = path->nodes[0];
  82. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  83. if (index)
  84. *index = btrfs_inode_ref_index(leaf, ref);
  85. if (del_len == item_size) {
  86. ret = btrfs_del_item(trans, root, path);
  87. goto out;
  88. }
  89. ptr = (unsigned long)ref;
  90. sub_item_len = name_len + sizeof(*ref);
  91. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  92. memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
  93. item_size - (ptr + sub_item_len - item_start));
  94. ret = btrfs_truncate_item(trans, root, path,
  95. item_size - sub_item_len, 1);
  96. BUG_ON(ret);
  97. out:
  98. btrfs_free_path(path);
  99. return ret;
  100. }
  101. int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
  102. struct btrfs_root *root,
  103. const char *name, int name_len,
  104. u64 inode_objectid, u64 ref_objectid, u64 index)
  105. {
  106. struct btrfs_path *path;
  107. struct btrfs_key key;
  108. struct btrfs_inode_ref *ref;
  109. unsigned long ptr;
  110. int ret;
  111. int ins_len = name_len + sizeof(*ref);
  112. key.objectid = inode_objectid;
  113. key.offset = ref_objectid;
  114. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  115. path = btrfs_alloc_path();
  116. if (!path)
  117. return -ENOMEM;
  118. path->leave_spinning = 1;
  119. ret = btrfs_insert_empty_item(trans, root, path, &key,
  120. ins_len);
  121. if (ret == -EEXIST) {
  122. u32 old_size;
  123. if (find_name_in_backref(path, name, name_len, &ref))
  124. goto out;
  125. old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  126. ret = btrfs_extend_item(trans, root, path, ins_len);
  127. BUG_ON(ret);
  128. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  129. struct btrfs_inode_ref);
  130. ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
  131. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  132. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  133. ptr = (unsigned long)(ref + 1);
  134. ret = 0;
  135. } else if (ret < 0) {
  136. goto out;
  137. } else {
  138. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  139. struct btrfs_inode_ref);
  140. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  141. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  142. ptr = (unsigned long)(ref + 1);
  143. }
  144. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  145. btrfs_mark_buffer_dirty(path->nodes[0]);
  146. out:
  147. btrfs_free_path(path);
  148. return ret;
  149. }
  150. int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
  151. struct btrfs_root *root,
  152. struct btrfs_path *path, u64 objectid)
  153. {
  154. struct btrfs_key key;
  155. int ret;
  156. key.objectid = objectid;
  157. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  158. key.offset = 0;
  159. ret = btrfs_insert_empty_item(trans, root, path, &key,
  160. sizeof(struct btrfs_inode_item));
  161. if (ret == 0 && objectid > root->highest_inode)
  162. root->highest_inode = objectid;
  163. return ret;
  164. }
  165. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  166. *root, struct btrfs_path *path,
  167. struct btrfs_key *location, int mod)
  168. {
  169. int ins_len = mod < 0 ? -1 : 0;
  170. int cow = mod != 0;
  171. int ret;
  172. int slot;
  173. struct extent_buffer *leaf;
  174. struct btrfs_key found_key;
  175. ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
  176. if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
  177. location->offset == (u64)-1 && path->slots[0] != 0) {
  178. slot = path->slots[0] - 1;
  179. leaf = path->nodes[0];
  180. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  181. if (found_key.objectid == location->objectid &&
  182. btrfs_key_type(&found_key) == btrfs_key_type(location)) {
  183. path->slots[0]--;
  184. return 0;
  185. }
  186. }
  187. return ret;
  188. }