uuid-tree.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) STRATO AG 2013. 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 <linux/uuid.h>
  19. #include <asm/unaligned.h>
  20. #include "ctree.h"
  21. #include "transaction.h"
  22. #include "disk-io.h"
  23. #include "print-tree.h"
  24. static void btrfs_uuid_to_key(u8 *uuid, u8 type, struct btrfs_key *key)
  25. {
  26. key->type = type;
  27. key->objectid = get_unaligned_le64(uuid);
  28. key->offset = get_unaligned_le64(uuid + sizeof(u64));
  29. }
  30. /* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */
  31. static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, u8 *uuid,
  32. u8 type, u64 subid)
  33. {
  34. int ret;
  35. struct btrfs_path *path = NULL;
  36. struct extent_buffer *eb;
  37. int slot;
  38. u32 item_size;
  39. unsigned long offset;
  40. struct btrfs_key key;
  41. if (WARN_ON_ONCE(!uuid_root)) {
  42. ret = -ENOENT;
  43. goto out;
  44. }
  45. path = btrfs_alloc_path();
  46. if (!path) {
  47. ret = -ENOMEM;
  48. goto out;
  49. }
  50. btrfs_uuid_to_key(uuid, type, &key);
  51. ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
  52. if (ret < 0) {
  53. goto out;
  54. } else if (ret > 0) {
  55. ret = -ENOENT;
  56. goto out;
  57. }
  58. eb = path->nodes[0];
  59. slot = path->slots[0];
  60. item_size = btrfs_item_size_nr(eb, slot);
  61. offset = btrfs_item_ptr_offset(eb, slot);
  62. ret = -ENOENT;
  63. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  64. btrfs_warn(uuid_root->fs_info, "uuid item with illegal size %lu!",
  65. (unsigned long)item_size);
  66. goto out;
  67. }
  68. while (item_size) {
  69. __le64 data;
  70. read_extent_buffer(eb, &data, offset, sizeof(data));
  71. if (le64_to_cpu(data) == subid) {
  72. ret = 0;
  73. break;
  74. }
  75. offset += sizeof(data);
  76. item_size -= sizeof(data);
  77. }
  78. out:
  79. btrfs_free_path(path);
  80. return ret;
  81. }
  82. int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans,
  83. struct btrfs_root *uuid_root, u8 *uuid, u8 type,
  84. u64 subid_cpu)
  85. {
  86. int ret;
  87. struct btrfs_path *path = NULL;
  88. struct btrfs_key key;
  89. struct extent_buffer *eb;
  90. int slot;
  91. unsigned long offset;
  92. __le64 subid_le;
  93. ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
  94. if (ret != -ENOENT)
  95. return ret;
  96. if (WARN_ON_ONCE(!uuid_root)) {
  97. ret = -EINVAL;
  98. goto out;
  99. }
  100. btrfs_uuid_to_key(uuid, type, &key);
  101. path = btrfs_alloc_path();
  102. if (!path) {
  103. ret = -ENOMEM;
  104. goto out;
  105. }
  106. ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
  107. sizeof(subid_le));
  108. if (ret >= 0) {
  109. /* Add an item for the type for the first time */
  110. eb = path->nodes[0];
  111. slot = path->slots[0];
  112. offset = btrfs_item_ptr_offset(eb, slot);
  113. } else if (ret == -EEXIST) {
  114. /*
  115. * An item with that type already exists.
  116. * Extend the item and store the new subid at the end.
  117. */
  118. btrfs_extend_item(uuid_root, path, sizeof(subid_le));
  119. eb = path->nodes[0];
  120. slot = path->slots[0];
  121. offset = btrfs_item_ptr_offset(eb, slot);
  122. offset += btrfs_item_size_nr(eb, slot) - sizeof(subid_le);
  123. } else if (ret < 0) {
  124. btrfs_warn(uuid_root->fs_info, "insert uuid item failed %d "
  125. "(0x%016llx, 0x%016llx) type %u!",
  126. ret, (unsigned long long)key.objectid,
  127. (unsigned long long)key.offset, type);
  128. goto out;
  129. }
  130. ret = 0;
  131. subid_le = cpu_to_le64(subid_cpu);
  132. write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
  133. btrfs_mark_buffer_dirty(eb);
  134. out:
  135. btrfs_free_path(path);
  136. return ret;
  137. }
  138. int btrfs_uuid_tree_rem(struct btrfs_trans_handle *trans,
  139. struct btrfs_root *uuid_root, u8 *uuid, u8 type,
  140. u64 subid)
  141. {
  142. int ret;
  143. struct btrfs_path *path = NULL;
  144. struct btrfs_key key;
  145. struct extent_buffer *eb;
  146. int slot;
  147. unsigned long offset;
  148. u32 item_size;
  149. unsigned long move_dst;
  150. unsigned long move_src;
  151. unsigned long move_len;
  152. if (WARN_ON_ONCE(!uuid_root)) {
  153. ret = -EINVAL;
  154. goto out;
  155. }
  156. btrfs_uuid_to_key(uuid, type, &key);
  157. path = btrfs_alloc_path();
  158. if (!path) {
  159. ret = -ENOMEM;
  160. goto out;
  161. }
  162. ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
  163. if (ret < 0) {
  164. btrfs_warn(uuid_root->fs_info, "error %d while searching for uuid item!",
  165. ret);
  166. goto out;
  167. }
  168. if (ret > 0) {
  169. ret = -ENOENT;
  170. goto out;
  171. }
  172. eb = path->nodes[0];
  173. slot = path->slots[0];
  174. offset = btrfs_item_ptr_offset(eb, slot);
  175. item_size = btrfs_item_size_nr(eb, slot);
  176. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  177. btrfs_warn(uuid_root->fs_info, "uuid item with illegal size %lu!",
  178. (unsigned long)item_size);
  179. ret = -ENOENT;
  180. goto out;
  181. }
  182. while (item_size) {
  183. __le64 read_subid;
  184. read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
  185. if (le64_to_cpu(read_subid) == subid)
  186. break;
  187. offset += sizeof(read_subid);
  188. item_size -= sizeof(read_subid);
  189. }
  190. if (!item_size) {
  191. ret = -ENOENT;
  192. goto out;
  193. }
  194. item_size = btrfs_item_size_nr(eb, slot);
  195. if (item_size == sizeof(subid)) {
  196. ret = btrfs_del_item(trans, uuid_root, path);
  197. goto out;
  198. }
  199. move_dst = offset;
  200. move_src = offset + sizeof(subid);
  201. move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
  202. memmove_extent_buffer(eb, move_dst, move_src, move_len);
  203. btrfs_truncate_item(uuid_root, path, item_size - sizeof(subid), 1);
  204. out:
  205. btrfs_free_path(path);
  206. return ret;
  207. }
  208. static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
  209. u64 subid)
  210. {
  211. struct btrfs_trans_handle *trans;
  212. int ret;
  213. /* 1 - for the uuid item */
  214. trans = btrfs_start_transaction(uuid_root, 1);
  215. if (IS_ERR(trans)) {
  216. ret = PTR_ERR(trans);
  217. goto out;
  218. }
  219. ret = btrfs_uuid_tree_rem(trans, uuid_root, uuid, type, subid);
  220. btrfs_end_transaction(trans, uuid_root);
  221. out:
  222. return ret;
  223. }
  224. int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
  225. int (*check_func)(struct btrfs_fs_info *, u8 *, u8,
  226. u64))
  227. {
  228. struct btrfs_root *root = fs_info->uuid_root;
  229. struct btrfs_key key;
  230. struct btrfs_path *path;
  231. int ret = 0;
  232. struct extent_buffer *leaf;
  233. int slot;
  234. u32 item_size;
  235. unsigned long offset;
  236. path = btrfs_alloc_path();
  237. if (!path) {
  238. ret = -ENOMEM;
  239. goto out;
  240. }
  241. key.objectid = 0;
  242. key.type = 0;
  243. key.offset = 0;
  244. again_search_slot:
  245. ret = btrfs_search_forward(root, &key, path, 0);
  246. if (ret) {
  247. if (ret > 0)
  248. ret = 0;
  249. goto out;
  250. }
  251. while (1) {
  252. cond_resched();
  253. leaf = path->nodes[0];
  254. slot = path->slots[0];
  255. btrfs_item_key_to_cpu(leaf, &key, slot);
  256. if (key.type != BTRFS_UUID_KEY_SUBVOL &&
  257. key.type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
  258. goto skip;
  259. offset = btrfs_item_ptr_offset(leaf, slot);
  260. item_size = btrfs_item_size_nr(leaf, slot);
  261. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  262. btrfs_warn(fs_info, "uuid item with illegal size %lu!",
  263. (unsigned long)item_size);
  264. goto skip;
  265. }
  266. while (item_size) {
  267. u8 uuid[BTRFS_UUID_SIZE];
  268. __le64 subid_le;
  269. u64 subid_cpu;
  270. put_unaligned_le64(key.objectid, uuid);
  271. put_unaligned_le64(key.offset, uuid + sizeof(u64));
  272. read_extent_buffer(leaf, &subid_le, offset,
  273. sizeof(subid_le));
  274. subid_cpu = le64_to_cpu(subid_le);
  275. ret = check_func(fs_info, uuid, key.type, subid_cpu);
  276. if (ret < 0)
  277. goto out;
  278. if (ret > 0) {
  279. btrfs_release_path(path);
  280. ret = btrfs_uuid_iter_rem(root, uuid, key.type,
  281. subid_cpu);
  282. if (ret == 0) {
  283. /*
  284. * this might look inefficient, but the
  285. * justification is that it is an
  286. * exception that check_func returns 1,
  287. * and that in the regular case only one
  288. * entry per UUID exists.
  289. */
  290. goto again_search_slot;
  291. }
  292. if (ret < 0 && ret != -ENOENT)
  293. goto out;
  294. }
  295. item_size -= sizeof(subid_le);
  296. offset += sizeof(subid_le);
  297. }
  298. skip:
  299. ret = btrfs_next_item(root, path);
  300. if (ret == 0)
  301. continue;
  302. else if (ret > 0)
  303. ret = 0;
  304. break;
  305. }
  306. out:
  307. btrfs_free_path(path);
  308. return ret;
  309. }