uuid-tree.c 8.0 KB

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