inode-item.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. 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. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  70. if (ret > 0) {
  71. ret = -ENOENT;
  72. goto out;
  73. } else if (ret < 0) {
  74. goto out;
  75. }
  76. if (!find_name_in_backref(path, name, name_len, &ref)) {
  77. ret = -ENOENT;
  78. goto out;
  79. }
  80. leaf = path->nodes[0];
  81. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  82. if (index)
  83. *index = btrfs_inode_ref_index(leaf, ref);
  84. if (del_len == item_size) {
  85. ret = btrfs_del_item(trans, root, path);
  86. goto out;
  87. }
  88. ptr = (unsigned long)ref;
  89. sub_item_len = name_len + sizeof(*ref);
  90. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  91. memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
  92. item_size - (ptr + sub_item_len - item_start));
  93. ret = btrfs_truncate_item(trans, root, path,
  94. item_size - sub_item_len, 1);
  95. BUG_ON(ret);
  96. out:
  97. btrfs_free_path(path);
  98. return ret;
  99. }
  100. int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
  101. struct btrfs_root *root,
  102. const char *name, int name_len,
  103. u64 inode_objectid, u64 ref_objectid, u64 index)
  104. {
  105. struct btrfs_path *path;
  106. struct btrfs_key key;
  107. struct btrfs_inode_ref *ref;
  108. unsigned long ptr;
  109. int ret;
  110. int ins_len = name_len + sizeof(*ref);
  111. key.objectid = inode_objectid;
  112. key.offset = ref_objectid;
  113. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  114. path = btrfs_alloc_path();
  115. if (!path)
  116. return -ENOMEM;
  117. ret = btrfs_insert_empty_item(trans, root, path, &key,
  118. ins_len);
  119. if (ret == -EEXIST) {
  120. u32 old_size;
  121. if (find_name_in_backref(path, name, name_len, &ref))
  122. goto out;
  123. old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  124. ret = btrfs_extend_item(trans, root, path, ins_len);
  125. BUG_ON(ret);
  126. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  127. struct btrfs_inode_ref);
  128. ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
  129. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  130. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  131. ptr = (unsigned long)(ref + 1);
  132. ret = 0;
  133. } else if (ret < 0) {
  134. goto out;
  135. } else {
  136. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  137. struct btrfs_inode_ref);
  138. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  139. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  140. ptr = (unsigned long)(ref + 1);
  141. }
  142. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  143. btrfs_mark_buffer_dirty(path->nodes[0]);
  144. out:
  145. btrfs_free_path(path);
  146. return ret;
  147. }
  148. int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
  149. struct btrfs_root *root,
  150. struct btrfs_path *path, u64 objectid)
  151. {
  152. struct btrfs_key key;
  153. int ret;
  154. key.objectid = objectid;
  155. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  156. key.offset = 0;
  157. ret = btrfs_insert_empty_item(trans, root, path, &key,
  158. sizeof(struct btrfs_inode_item));
  159. if (ret == 0 && objectid > root->highest_inode)
  160. root->highest_inode = objectid;
  161. return ret;
  162. }
  163. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  164. *root, struct btrfs_path *path,
  165. struct btrfs_key *location, int mod)
  166. {
  167. int ins_len = mod < 0 ? -1 : 0;
  168. int cow = mod != 0;
  169. int ret;
  170. int slot;
  171. struct extent_buffer *leaf;
  172. struct btrfs_key found_key;
  173. ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
  174. if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
  175. location->offset == (u64)-1 && path->slots[0] != 0) {
  176. slot = path->slots[0] - 1;
  177. leaf = path->nodes[0];
  178. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  179. if (found_key.objectid == location->objectid &&
  180. btrfs_key_type(&found_key) == btrfs_key_type(location)) {
  181. path->slots[0]--;
  182. return 0;
  183. }
  184. }
  185. return ret;
  186. }