inode-item.c 6.4 KB

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