vl_alias.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* AFS cell alias detection
  3. *
  4. * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/sched.h>
  9. #include <linux/namei.h>
  10. #include <keys/rxrpc-type.h>
  11. #include "internal.h"
  12. /*
  13. * Sample a volume.
  14. */
  15. static struct afs_volume *afs_sample_volume(struct afs_cell *cell, struct key *key,
  16. const char *name, unsigned int namelen)
  17. {
  18. struct afs_volume *volume;
  19. struct afs_fs_context fc = {
  20. .type = 0, /* Explicitly leave it to the VLDB */
  21. .volnamesz = namelen,
  22. .volname = name,
  23. .net = cell->net,
  24. .cell = cell,
  25. .key = key, /* This might need to be something */
  26. };
  27. volume = afs_create_volume(&fc);
  28. _leave(" = %p", volume);
  29. return volume;
  30. }
  31. /*
  32. * Compare the address lists of a pair of fileservers.
  33. */
  34. static int afs_compare_fs_alists(const struct afs_server *server_a,
  35. const struct afs_server *server_b)
  36. {
  37. const struct afs_addr_list *la, *lb;
  38. int a = 0, b = 0, addr_matches = 0;
  39. la = rcu_dereference(server_a->endpoint_state)->addresses;
  40. lb = rcu_dereference(server_b->endpoint_state)->addresses;
  41. while (a < la->nr_addrs && b < lb->nr_addrs) {
  42. unsigned long pa = (unsigned long)la->addrs[a].peer;
  43. unsigned long pb = (unsigned long)lb->addrs[b].peer;
  44. long diff = pa - pb;
  45. if (diff < 0) {
  46. a++;
  47. } else if (diff > 0) {
  48. b++;
  49. } else {
  50. addr_matches++;
  51. a++;
  52. b++;
  53. }
  54. }
  55. return addr_matches;
  56. }
  57. /*
  58. * Compare the fileserver lists of two volumes. The server lists are sorted in
  59. * order of ascending UUID.
  60. */
  61. static int afs_compare_volume_slists(const struct afs_volume *vol_a,
  62. const struct afs_volume *vol_b)
  63. {
  64. const struct afs_server_list *la, *lb;
  65. int i, a = 0, b = 0, uuid_matches = 0, addr_matches = 0;
  66. la = rcu_dereference(vol_a->servers);
  67. lb = rcu_dereference(vol_b->servers);
  68. for (i = 0; i < AFS_MAXTYPES; i++)
  69. if (vol_a->vids[i] != vol_b->vids[i])
  70. return 0;
  71. while (a < la->nr_servers && b < lb->nr_servers) {
  72. const struct afs_server *server_a = la->servers[a].server;
  73. const struct afs_server *server_b = lb->servers[b].server;
  74. int diff = memcmp(&server_a->uuid, &server_b->uuid, sizeof(uuid_t));
  75. if (diff < 0) {
  76. a++;
  77. } else if (diff > 0) {
  78. b++;
  79. } else {
  80. uuid_matches++;
  81. addr_matches += afs_compare_fs_alists(server_a, server_b);
  82. a++;
  83. b++;
  84. }
  85. }
  86. _leave(" = %d [um %d]", addr_matches, uuid_matches);
  87. return addr_matches;
  88. }
  89. /*
  90. * Compare root.cell volumes.
  91. */
  92. static int afs_compare_cell_roots(struct afs_cell *cell)
  93. {
  94. struct afs_cell *p;
  95. _enter("");
  96. rcu_read_lock();
  97. hlist_for_each_entry_rcu(p, &cell->net->proc_cells, proc_link) {
  98. if (p == cell || p->alias_of)
  99. continue;
  100. if (!p->root_volume)
  101. continue; /* Ignore cells that don't have a root.cell volume. */
  102. if (afs_compare_volume_slists(cell->root_volume, p->root_volume) != 0)
  103. goto is_alias;
  104. }
  105. rcu_read_unlock();
  106. _leave(" = 0");
  107. return 0;
  108. is_alias:
  109. rcu_read_unlock();
  110. cell->alias_of = afs_use_cell(p, afs_cell_trace_use_alias);
  111. return 1;
  112. }
  113. /*
  114. * Query the new cell for a volume from a cell we're already using.
  115. */
  116. static int afs_query_for_alias_one(struct afs_cell *cell, struct key *key,
  117. struct afs_cell *p)
  118. {
  119. struct afs_volume *volume, *pvol = NULL;
  120. int ret;
  121. /* Arbitrarily pick a volume from the list. */
  122. read_seqlock_excl(&p->volume_lock);
  123. if (!RB_EMPTY_ROOT(&p->volumes))
  124. pvol = afs_get_volume(rb_entry(p->volumes.rb_node,
  125. struct afs_volume, cell_node),
  126. afs_volume_trace_get_query_alias);
  127. read_sequnlock_excl(&p->volume_lock);
  128. if (!pvol)
  129. return 0;
  130. _enter("%s:%s", cell->name, pvol->name);
  131. /* And see if it's in the new cell. */
  132. volume = afs_sample_volume(cell, key, pvol->name, pvol->name_len);
  133. if (IS_ERR(volume)) {
  134. afs_put_volume(pvol, afs_volume_trace_put_query_alias);
  135. if (PTR_ERR(volume) != -ENOMEDIUM)
  136. return PTR_ERR(volume);
  137. /* That volume is not in the new cell, so not an alias */
  138. return 0;
  139. }
  140. /* The new cell has a like-named volume also - compare volume ID,
  141. * server and address lists.
  142. */
  143. ret = 0;
  144. if (pvol->vid == volume->vid) {
  145. rcu_read_lock();
  146. if (afs_compare_volume_slists(volume, pvol))
  147. ret = 1;
  148. rcu_read_unlock();
  149. }
  150. afs_put_volume(volume, afs_volume_trace_put_query_alias);
  151. afs_put_volume(pvol, afs_volume_trace_put_query_alias);
  152. return ret;
  153. }
  154. /*
  155. * Query the new cell for volumes we know exist in cells we're already using.
  156. */
  157. static int afs_query_for_alias(struct afs_cell *cell, struct key *key)
  158. {
  159. struct afs_cell *p;
  160. _enter("%s", cell->name);
  161. if (mutex_lock_interruptible(&cell->net->proc_cells_lock) < 0)
  162. return -ERESTARTSYS;
  163. hlist_for_each_entry(p, &cell->net->proc_cells, proc_link) {
  164. if (p == cell || p->alias_of)
  165. continue;
  166. if (RB_EMPTY_ROOT(&p->volumes))
  167. continue;
  168. if (p->root_volume)
  169. continue; /* Ignore cells that have a root.cell volume. */
  170. afs_use_cell(p, afs_cell_trace_use_check_alias);
  171. mutex_unlock(&cell->net->proc_cells_lock);
  172. if (afs_query_for_alias_one(cell, key, p) != 0)
  173. goto is_alias;
  174. if (mutex_lock_interruptible(&cell->net->proc_cells_lock) < 0) {
  175. afs_unuse_cell(p, afs_cell_trace_unuse_check_alias);
  176. return -ERESTARTSYS;
  177. }
  178. afs_unuse_cell(p, afs_cell_trace_unuse_check_alias);
  179. }
  180. mutex_unlock(&cell->net->proc_cells_lock);
  181. _leave(" = 0");
  182. return 0;
  183. is_alias:
  184. cell->alias_of = p; /* Transfer our ref */
  185. return 1;
  186. }
  187. /*
  188. * Look up a VLDB record for a volume.
  189. */
  190. static char *afs_vl_get_cell_name(struct afs_cell *cell, struct key *key)
  191. {
  192. struct afs_vl_cursor vc;
  193. char *cell_name = ERR_PTR(-EDESTADDRREQ);
  194. bool skipped = false, not_skipped = false;
  195. int ret;
  196. if (!afs_begin_vlserver_operation(&vc, cell, key))
  197. return ERR_PTR(-ERESTARTSYS);
  198. while (afs_select_vlserver(&vc)) {
  199. if (!test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags)) {
  200. vc.call_error = -EOPNOTSUPP;
  201. skipped = true;
  202. continue;
  203. }
  204. not_skipped = true;
  205. cell_name = afs_yfsvl_get_cell_name(&vc);
  206. }
  207. ret = afs_end_vlserver_operation(&vc);
  208. if (skipped && !not_skipped)
  209. ret = -EOPNOTSUPP;
  210. return ret < 0 ? ERR_PTR(ret) : cell_name;
  211. }
  212. static int yfs_check_canonical_cell_name(struct afs_cell *cell, struct key *key)
  213. {
  214. struct afs_cell *master;
  215. size_t name_len;
  216. char *cell_name;
  217. cell_name = afs_vl_get_cell_name(cell, key);
  218. if (IS_ERR(cell_name))
  219. return PTR_ERR(cell_name);
  220. if (strcmp(cell_name, cell->name) == 0) {
  221. kfree(cell_name);
  222. return 0;
  223. }
  224. name_len = strlen(cell_name);
  225. if (!name_len || name_len > AFS_MAXCELLNAME)
  226. master = ERR_PTR(-EOPNOTSUPP);
  227. else
  228. master = afs_lookup_cell(cell->net, cell_name, name_len, NULL,
  229. AFS_LOOKUP_CELL_ALIAS_CHECK,
  230. afs_cell_trace_use_lookup_canonical);
  231. kfree(cell_name);
  232. if (IS_ERR(master))
  233. return PTR_ERR(master);
  234. cell->alias_of = master; /* Transfer our ref */
  235. return 1;
  236. }
  237. static int afs_do_cell_detect_alias(struct afs_cell *cell, struct key *key)
  238. {
  239. struct afs_volume *root_volume;
  240. int ret;
  241. _enter("%s", cell->name);
  242. ret = yfs_check_canonical_cell_name(cell, key);
  243. if (ret != -EOPNOTSUPP)
  244. return ret;
  245. /* Try and get the root.cell volume for comparison with other cells */
  246. root_volume = afs_sample_volume(cell, key, "root.cell", 9);
  247. if (!IS_ERR(root_volume)) {
  248. cell->root_volume = root_volume;
  249. return afs_compare_cell_roots(cell);
  250. }
  251. if (PTR_ERR(root_volume) != -ENOMEDIUM)
  252. return PTR_ERR(root_volume);
  253. /* Okay, this cell doesn't have an root.cell volume. We need to
  254. * locate some other random volume and use that to check.
  255. */
  256. return afs_query_for_alias(cell, key);
  257. }
  258. /*
  259. * Check to see if a new cell is an alias of a cell we already have. At this
  260. * point we have the cell's volume server list.
  261. *
  262. * Returns 0 if we didn't detect an alias, 1 if we found an alias and an error
  263. * if we had problems gathering the data required. In the case the we did
  264. * detect an alias, cell->alias_of is set to point to the assumed master.
  265. */
  266. int afs_cell_detect_alias(struct afs_cell *cell, struct key *key)
  267. {
  268. struct afs_net *net = cell->net;
  269. int ret;
  270. if (mutex_lock_interruptible(&net->cells_alias_lock) < 0)
  271. return -ERESTARTSYS;
  272. if (test_bit(AFS_CELL_FL_CHECK_ALIAS, &cell->flags)) {
  273. ret = afs_do_cell_detect_alias(cell, key);
  274. if (ret >= 0)
  275. clear_bit_unlock(AFS_CELL_FL_CHECK_ALIAS, &cell->flags);
  276. } else {
  277. ret = cell->alias_of ? 1 : 0;
  278. }
  279. mutex_unlock(&net->cells_alias_lock);
  280. if (ret == 1)
  281. pr_notice("kAFS: Cell %s is an alias of %s\n",
  282. cell->name, cell->alias_of->name);
  283. return ret;
  284. }