inode-item.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. struct btrfs_inode_ref *
  49. btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
  50. struct btrfs_root *root,
  51. struct btrfs_path *path,
  52. const char *name, int name_len,
  53. u64 inode_objectid, u64 ref_objectid, int mod)
  54. {
  55. struct btrfs_key key;
  56. struct btrfs_inode_ref *ref;
  57. int ins_len = mod < 0 ? -1 : 0;
  58. int cow = mod != 0;
  59. int ret;
  60. key.objectid = inode_objectid;
  61. key.type = BTRFS_INODE_REF_KEY;
  62. key.offset = ref_objectid;
  63. ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
  64. if (ret < 0)
  65. return ERR_PTR(ret);
  66. if (ret > 0)
  67. return NULL;
  68. if (!find_name_in_backref(path, name, name_len, &ref))
  69. return NULL;
  70. return ref;
  71. }
  72. int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
  73. struct btrfs_root *root,
  74. const char *name, int name_len,
  75. u64 inode_objectid, u64 ref_objectid, u64 *index)
  76. {
  77. struct btrfs_path *path;
  78. struct btrfs_key key;
  79. struct btrfs_inode_ref *ref;
  80. struct extent_buffer *leaf;
  81. unsigned long ptr;
  82. unsigned long item_start;
  83. u32 item_size;
  84. u32 sub_item_len;
  85. int ret;
  86. int del_len = name_len + sizeof(*ref);
  87. key.objectid = inode_objectid;
  88. key.offset = ref_objectid;
  89. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  90. path = btrfs_alloc_path();
  91. if (!path)
  92. return -ENOMEM;
  93. path->leave_spinning = 1;
  94. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  95. if (ret > 0) {
  96. ret = -ENOENT;
  97. goto out;
  98. } else if (ret < 0) {
  99. goto out;
  100. }
  101. if (!find_name_in_backref(path, name, name_len, &ref)) {
  102. ret = -ENOENT;
  103. goto out;
  104. }
  105. leaf = path->nodes[0];
  106. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  107. if (index)
  108. *index = btrfs_inode_ref_index(leaf, ref);
  109. if (del_len == item_size) {
  110. ret = btrfs_del_item(trans, root, path);
  111. goto out;
  112. }
  113. ptr = (unsigned long)ref;
  114. sub_item_len = name_len + sizeof(*ref);
  115. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  116. memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
  117. item_size - (ptr + sub_item_len - item_start));
  118. ret = btrfs_truncate_item(trans, root, path,
  119. item_size - sub_item_len, 1);
  120. out:
  121. btrfs_free_path(path);
  122. return ret;
  123. }
  124. int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
  125. struct btrfs_root *root,
  126. const char *name, int name_len,
  127. u64 inode_objectid, u64 ref_objectid, u64 index)
  128. {
  129. struct btrfs_path *path;
  130. struct btrfs_key key;
  131. struct btrfs_inode_ref *ref;
  132. unsigned long ptr;
  133. int ret;
  134. int ins_len = name_len + sizeof(*ref);
  135. key.objectid = inode_objectid;
  136. key.offset = ref_objectid;
  137. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  138. path = btrfs_alloc_path();
  139. if (!path)
  140. return -ENOMEM;
  141. path->leave_spinning = 1;
  142. ret = btrfs_insert_empty_item(trans, root, path, &key,
  143. ins_len);
  144. if (ret == -EEXIST) {
  145. u32 old_size;
  146. if (find_name_in_backref(path, name, name_len, &ref))
  147. goto out;
  148. old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  149. ret = btrfs_extend_item(trans, root, path, ins_len);
  150. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  151. struct btrfs_inode_ref);
  152. ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
  153. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  154. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  155. ptr = (unsigned long)(ref + 1);
  156. ret = 0;
  157. } else if (ret < 0) {
  158. if (ret == -EOVERFLOW)
  159. ret = -EMLINK;
  160. goto out;
  161. } else {
  162. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  163. struct btrfs_inode_ref);
  164. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  165. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  166. ptr = (unsigned long)(ref + 1);
  167. }
  168. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  169. btrfs_mark_buffer_dirty(path->nodes[0]);
  170. out:
  171. btrfs_free_path(path);
  172. return ret;
  173. }
  174. int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
  175. struct btrfs_root *root,
  176. struct btrfs_path *path, u64 objectid)
  177. {
  178. struct btrfs_key key;
  179. int ret;
  180. key.objectid = objectid;
  181. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  182. key.offset = 0;
  183. ret = btrfs_insert_empty_item(trans, root, path, &key,
  184. sizeof(struct btrfs_inode_item));
  185. return ret;
  186. }
  187. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  188. *root, struct btrfs_path *path,
  189. struct btrfs_key *location, int mod)
  190. {
  191. int ins_len = mod < 0 ? -1 : 0;
  192. int cow = mod != 0;
  193. int ret;
  194. int slot;
  195. struct extent_buffer *leaf;
  196. struct btrfs_key found_key;
  197. ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
  198. if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
  199. location->offset == (u64)-1 && path->slots[0] != 0) {
  200. slot = path->slots[0] - 1;
  201. leaf = path->nodes[0];
  202. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  203. if (found_key.objectid == location->objectid &&
  204. btrfs_key_type(&found_key) == btrfs_key_type(location)) {
  205. path->slots[0]--;
  206. return 0;
  207. }
  208. }
  209. return ret;
  210. }