inode-item.c 5.5 KB

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