vl_alias.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 two addresses.
  33. */
  34. static int afs_compare_addrs(const struct sockaddr_rxrpc *srx_a,
  35. const struct sockaddr_rxrpc *srx_b)
  36. {
  37. short port_a, port_b;
  38. int addr_a, addr_b, diff;
  39. diff = (short)srx_a->transport_type - (short)srx_b->transport_type;
  40. if (diff)
  41. goto out;
  42. switch (srx_a->transport_type) {
  43. case AF_INET: {
  44. const struct sockaddr_in *a = &srx_a->transport.sin;
  45. const struct sockaddr_in *b = &srx_b->transport.sin;
  46. addr_a = ntohl(a->sin_addr.s_addr);
  47. addr_b = ntohl(b->sin_addr.s_addr);
  48. diff = addr_a - addr_b;
  49. if (diff == 0) {
  50. port_a = ntohs(a->sin_port);
  51. port_b = ntohs(b->sin_port);
  52. diff = port_a - port_b;
  53. }
  54. break;
  55. }
  56. case AF_INET6: {
  57. const struct sockaddr_in6 *a = &srx_a->transport.sin6;
  58. const struct sockaddr_in6 *b = &srx_b->transport.sin6;
  59. diff = memcmp(&a->sin6_addr, &b->sin6_addr, 16);
  60. if (diff == 0) {
  61. port_a = ntohs(a->sin6_port);
  62. port_b = ntohs(b->sin6_port);
  63. diff = port_a - port_b;
  64. }
  65. break;
  66. }
  67. default:
  68. WARN_ON(1);
  69. diff = 1;
  70. }
  71. out:
  72. return diff;
  73. }
  74. /*
  75. * Compare the address lists of a pair of fileservers.
  76. */
  77. static int afs_compare_fs_alists(const struct afs_server *server_a,
  78. const struct afs_server *server_b)
  79. {
  80. const struct afs_addr_list *la, *lb;
  81. int a = 0, b = 0, addr_matches = 0;
  82. la = rcu_dereference(server_a->addresses);
  83. lb = rcu_dereference(server_b->addresses);
  84. while (a < la->nr_addrs && b < lb->nr_addrs) {
  85. const struct sockaddr_rxrpc *srx_a = &la->addrs[a];
  86. const struct sockaddr_rxrpc *srx_b = &lb->addrs[b];
  87. int diff = afs_compare_addrs(srx_a, srx_b);
  88. if (diff < 0) {
  89. a++;
  90. } else if (diff > 0) {
  91. b++;
  92. } else {
  93. addr_matches++;
  94. a++;
  95. b++;
  96. }
  97. }
  98. return addr_matches;
  99. }
  100. /*
  101. * Compare the fileserver lists of two volumes. The server lists are sorted in
  102. * order of ascending UUID.
  103. */
  104. static int afs_compare_volume_slists(const struct afs_volume *vol_a,
  105. const struct afs_volume *vol_b)
  106. {
  107. const struct afs_server_list *la, *lb;
  108. int i, a = 0, b = 0, uuid_matches = 0, addr_matches = 0;
  109. la = rcu_dereference(vol_a->servers);
  110. lb = rcu_dereference(vol_b->servers);
  111. for (i = 0; i < AFS_MAXTYPES; i++)
  112. if (la->vids[i] != lb->vids[i])
  113. return 0;
  114. while (a < la->nr_servers && b < lb->nr_servers) {
  115. const struct afs_server *server_a = la->servers[a].server;
  116. const struct afs_server *server_b = lb->servers[b].server;
  117. int diff = memcmp(&server_a->uuid, &server_b->uuid, sizeof(uuid_t));
  118. if (diff < 0) {
  119. a++;
  120. } else if (diff > 0) {
  121. b++;
  122. } else {
  123. uuid_matches++;
  124. addr_matches += afs_compare_fs_alists(server_a, server_b);
  125. a++;
  126. b++;
  127. }
  128. }
  129. _leave(" = %d [um %d]", addr_matches, uuid_matches);
  130. return addr_matches;
  131. }
  132. /*
  133. * Compare root.cell volumes.
  134. */
  135. static int afs_compare_cell_roots(struct afs_cell *cell)
  136. {
  137. struct afs_cell *p;
  138. _enter("");
  139. rcu_read_lock();
  140. hlist_for_each_entry_rcu(p, &cell->net->proc_cells, proc_link) {
  141. if (p == cell || p->alias_of)
  142. continue;
  143. if (!p->root_volume)
  144. continue; /* Ignore cells that don't have a root.cell volume. */
  145. if (afs_compare_volume_slists(cell->root_volume, p->root_volume) != 0)
  146. goto is_alias;
  147. }
  148. rcu_read_unlock();
  149. _leave(" = 0");
  150. return 0;
  151. is_alias:
  152. rcu_read_unlock();
  153. cell->alias_of = afs_use_cell(p, afs_cell_trace_use_alias);
  154. return 1;
  155. }
  156. /*
  157. * Query the new cell for a volume from a cell we're already using.
  158. */
  159. static int afs_query_for_alias_one(struct afs_cell *cell, struct key *key,
  160. struct afs_cell *p)
  161. {
  162. struct afs_volume *volume, *pvol = NULL;
  163. int ret;
  164. /* Arbitrarily pick a volume from the list. */
  165. read_seqlock_excl(&p->volume_lock);
  166. if (!RB_EMPTY_ROOT(&p->volumes))
  167. pvol = afs_get_volume(rb_entry(p->volumes.rb_node,
  168. struct afs_volume, cell_node),
  169. afs_volume_trace_get_query_alias);
  170. read_sequnlock_excl(&p->volume_lock);
  171. if (!pvol)
  172. return 0;
  173. _enter("%s:%s", cell->name, pvol->name);
  174. /* And see if it's in the new cell. */
  175. volume = afs_sample_volume(cell, key, pvol->name, pvol->name_len);
  176. if (IS_ERR(volume)) {
  177. afs_put_volume(cell->net, pvol, afs_volume_trace_put_query_alias);
  178. if (PTR_ERR(volume) != -ENOMEDIUM)
  179. return PTR_ERR(volume);
  180. /* That volume is not in the new cell, so not an alias */
  181. return 0;
  182. }
  183. /* The new cell has a like-named volume also - compare volume ID,
  184. * server and address lists.
  185. */
  186. ret = 0;
  187. if (pvol->vid == volume->vid) {
  188. rcu_read_lock();
  189. if (afs_compare_volume_slists(volume, pvol))
  190. ret = 1;
  191. rcu_read_unlock();
  192. }
  193. afs_put_volume(cell->net, volume, afs_volume_trace_put_query_alias);
  194. afs_put_volume(cell->net, pvol, afs_volume_trace_put_query_alias);
  195. return ret;
  196. }
  197. /*
  198. * Query the new cell for volumes we know exist in cells we're already using.
  199. */
  200. static int afs_query_for_alias(struct afs_cell *cell, struct key *key)
  201. {
  202. struct afs_cell *p;
  203. _enter("%s", cell->name);
  204. if (mutex_lock_interruptible(&cell->net->proc_cells_lock) < 0)
  205. return -ERESTARTSYS;
  206. hlist_for_each_entry(p, &cell->net->proc_cells, proc_link) {
  207. if (p == cell || p->alias_of)
  208. continue;
  209. if (RB_EMPTY_ROOT(&p->volumes))
  210. continue;
  211. if (p->root_volume)
  212. continue; /* Ignore cells that have a root.cell volume. */
  213. afs_use_cell(p, afs_cell_trace_use_check_alias);
  214. mutex_unlock(&cell->net->proc_cells_lock);
  215. if (afs_query_for_alias_one(cell, key, p) != 0)
  216. goto is_alias;
  217. if (mutex_lock_interruptible(&cell->net->proc_cells_lock) < 0) {
  218. afs_unuse_cell(cell->net, p, afs_cell_trace_unuse_check_alias);
  219. return -ERESTARTSYS;
  220. }
  221. afs_unuse_cell(cell->net, p, afs_cell_trace_unuse_check_alias);
  222. }
  223. mutex_unlock(&cell->net->proc_cells_lock);
  224. _leave(" = 0");
  225. return 0;
  226. is_alias:
  227. cell->alias_of = p; /* Transfer our ref */
  228. return 1;
  229. }
  230. /*
  231. * Look up a VLDB record for a volume.
  232. */
  233. static char *afs_vl_get_cell_name(struct afs_cell *cell, struct key *key)
  234. {
  235. struct afs_vl_cursor vc;
  236. char *cell_name = ERR_PTR(-EDESTADDRREQ);
  237. bool skipped = false, not_skipped = false;
  238. int ret;
  239. if (!afs_begin_vlserver_operation(&vc, cell, key))
  240. return ERR_PTR(-ERESTARTSYS);
  241. while (afs_select_vlserver(&vc)) {
  242. if (!test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags)) {
  243. vc.ac.error = -EOPNOTSUPP;
  244. skipped = true;
  245. continue;
  246. }
  247. not_skipped = true;
  248. cell_name = afs_yfsvl_get_cell_name(&vc);
  249. }
  250. ret = afs_end_vlserver_operation(&vc);
  251. if (skipped && !not_skipped)
  252. ret = -EOPNOTSUPP;
  253. return ret < 0 ? ERR_PTR(ret) : cell_name;
  254. }
  255. static int yfs_check_canonical_cell_name(struct afs_cell *cell, struct key *key)
  256. {
  257. struct afs_cell *master;
  258. char *cell_name;
  259. cell_name = afs_vl_get_cell_name(cell, key);
  260. if (IS_ERR(cell_name))
  261. return PTR_ERR(cell_name);
  262. if (strcmp(cell_name, cell->name) == 0) {
  263. kfree(cell_name);
  264. return 0;
  265. }
  266. master = afs_lookup_cell(cell->net, cell_name, strlen(cell_name),
  267. NULL, false);
  268. kfree(cell_name);
  269. if (IS_ERR(master))
  270. return PTR_ERR(master);
  271. cell->alias_of = master; /* Transfer our ref */
  272. return 1;
  273. }
  274. static int afs_do_cell_detect_alias(struct afs_cell *cell, struct key *key)
  275. {
  276. struct afs_volume *root_volume;
  277. int ret;
  278. _enter("%s", cell->name);
  279. ret = yfs_check_canonical_cell_name(cell, key);
  280. if (ret != -EOPNOTSUPP)
  281. return ret;
  282. /* Try and get the root.cell volume for comparison with other cells */
  283. root_volume = afs_sample_volume(cell, key, "root.cell", 9);
  284. if (!IS_ERR(root_volume)) {
  285. cell->root_volume = root_volume;
  286. return afs_compare_cell_roots(cell);
  287. }
  288. if (PTR_ERR(root_volume) != -ENOMEDIUM)
  289. return PTR_ERR(root_volume);
  290. /* Okay, this cell doesn't have an root.cell volume. We need to
  291. * locate some other random volume and use that to check.
  292. */
  293. return afs_query_for_alias(cell, key);
  294. }
  295. /*
  296. * Check to see if a new cell is an alias of a cell we already have. At this
  297. * point we have the cell's volume server list.
  298. *
  299. * Returns 0 if we didn't detect an alias, 1 if we found an alias and an error
  300. * if we had problems gathering the data required. In the case the we did
  301. * detect an alias, cell->alias_of is set to point to the assumed master.
  302. */
  303. int afs_cell_detect_alias(struct afs_cell *cell, struct key *key)
  304. {
  305. struct afs_net *net = cell->net;
  306. int ret;
  307. if (mutex_lock_interruptible(&net->cells_alias_lock) < 0)
  308. return -ERESTARTSYS;
  309. if (test_bit(AFS_CELL_FL_CHECK_ALIAS, &cell->flags)) {
  310. ret = afs_do_cell_detect_alias(cell, key);
  311. if (ret >= 0)
  312. clear_bit_unlock(AFS_CELL_FL_CHECK_ALIAS, &cell->flags);
  313. } else {
  314. ret = cell->alias_of ? 1 : 0;
  315. }
  316. mutex_unlock(&net->cells_alias_lock);
  317. if (ret == 1)
  318. pr_notice("kAFS: Cell %s is an alias of %s\n",
  319. cell->name, cell->alias_of->name);
  320. return ret;
  321. }