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