uuid-tree.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. pr_warn("btrfs: uuid item with illegal size %lu!\n",
  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. pr_warn("btrfs: insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!\n",
  125. ret, (unsigned long long)key.objectid,
  126. (unsigned long long)key.offset, type);
  127. goto out;
  128. }
  129. ret = 0;
  130. subid_le = cpu_to_le64(subid_cpu);
  131. write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
  132. btrfs_mark_buffer_dirty(eb);
  133. out:
  134. btrfs_free_path(path);
  135. return ret;
  136. }
  137. int btrfs_uuid_tree_rem(struct btrfs_trans_handle *trans,
  138. struct btrfs_root *uuid_root, u8 *uuid, u8 type,
  139. u64 subid)
  140. {
  141. int ret;
  142. struct btrfs_path *path = NULL;
  143. struct btrfs_key key;
  144. struct extent_buffer *eb;
  145. int slot;
  146. unsigned long offset;
  147. u32 item_size;
  148. unsigned long move_dst;
  149. unsigned long move_src;
  150. unsigned long move_len;
  151. if (WARN_ON_ONCE(!uuid_root)) {
  152. ret = -EINVAL;
  153. goto out;
  154. }
  155. btrfs_uuid_to_key(uuid, type, &key);
  156. path = btrfs_alloc_path();
  157. if (!path) {
  158. ret = -ENOMEM;
  159. goto out;
  160. }
  161. ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
  162. if (ret < 0) {
  163. pr_warn("btrfs: error %d while searching for uuid item!\n",
  164. ret);
  165. goto out;
  166. }
  167. if (ret > 0) {
  168. ret = -ENOENT;
  169. goto out;
  170. }
  171. eb = path->nodes[0];
  172. slot = path->slots[0];
  173. offset = btrfs_item_ptr_offset(eb, slot);
  174. item_size = btrfs_item_size_nr(eb, slot);
  175. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  176. pr_warn("btrfs: uuid item with illegal size %lu!\n",
  177. (unsigned long)item_size);
  178. ret = -ENOENT;
  179. goto out;
  180. }
  181. while (item_size) {
  182. __le64 read_subid;
  183. read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
  184. if (le64_to_cpu(read_subid) == subid)
  185. break;
  186. offset += sizeof(read_subid);
  187. item_size -= sizeof(read_subid);
  188. }
  189. if (!item_size) {
  190. ret = -ENOENT;
  191. goto out;
  192. }
  193. item_size = btrfs_item_size_nr(eb, slot);
  194. if (item_size == sizeof(subid)) {
  195. ret = btrfs_del_item(trans, uuid_root, path);
  196. goto out;
  197. }
  198. move_dst = offset;
  199. move_src = offset + sizeof(subid);
  200. move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
  201. memmove_extent_buffer(eb, move_dst, move_src, move_len);
  202. btrfs_truncate_item(uuid_root, path, item_size - sizeof(subid), 1);
  203. out:
  204. btrfs_free_path(path);
  205. return ret;
  206. }
  207. static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
  208. u64 subid)
  209. {
  210. struct btrfs_trans_handle *trans;
  211. int ret;
  212. /* 1 - for the uuid item */
  213. trans = btrfs_start_transaction(uuid_root, 1);
  214. if (IS_ERR(trans)) {
  215. ret = PTR_ERR(trans);
  216. goto out;
  217. }
  218. ret = btrfs_uuid_tree_rem(trans, uuid_root, uuid, type, subid);
  219. btrfs_end_transaction(trans, uuid_root);
  220. out:
  221. return ret;
  222. }
  223. int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
  224. int (*check_func)(struct btrfs_fs_info *, u8 *, u8,
  225. u64))
  226. {
  227. struct btrfs_root *root = fs_info->uuid_root;
  228. struct btrfs_key key;
  229. struct btrfs_path *path;
  230. int ret = 0;
  231. struct extent_buffer *leaf;
  232. int slot;
  233. u32 item_size;
  234. unsigned long offset;
  235. path = btrfs_alloc_path();
  236. if (!path) {
  237. ret = -ENOMEM;
  238. goto out;
  239. }
  240. key.objectid = 0;
  241. key.type = 0;
  242. key.offset = 0;
  243. again_search_slot:
  244. path->keep_locks = 1;
  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. pr_warn("btrfs: uuid item with illegal size %lu!\n",
  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. if (ret)
  309. pr_warn("btrfs: btrfs_uuid_tree_iterate failed %d\n", ret);
  310. return 0;
  311. }