blob: ed9056703505fe073bb3d7c5fa8050b70574efd2 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsd2ddc772017-11-02 15:27:50 +00002/* AFS fileserver list management.
3 *
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howellsd2ddc772017-11-02 15:27:50 +00006 */
7
8#include <linux/kernel.h>
9#include <linux/slab.h>
10#include "internal.h"
11
12void afs_put_serverlist(struct afs_net *net, struct afs_server_list *slist)
13{
14 int i;
15
David Howells0fafdc92017-11-13 16:59:50 +000016 if (slist && refcount_dec_and_test(&slist->usage)) {
David Howells20325962020-04-30 01:03:49 +010017 for (i = 0; i < slist->nr_servers; i++)
David Howells977e5f82020-04-17 17:31:26 +010018 afs_unuse_server(net, slist->servers[i].server,
19 afs_server_trace_put_slist);
David Howellsd2ddc772017-11-02 15:27:50 +000020 kfree(slist);
21 }
22}
23
24/*
25 * Build a server list from a VLDB record.
26 */
27struct afs_server_list *afs_alloc_server_list(struct afs_cell *cell,
28 struct key *key,
29 struct afs_vldb_entry *vldb,
30 u8 type_mask)
31{
32 struct afs_server_list *slist;
33 struct afs_server *server;
34 int ret = -ENOMEM, nr_servers = 0, i, j;
35
36 for (i = 0; i < vldb->nr_servers; i++)
37 if (vldb->fs_mask[i] & type_mask)
38 nr_servers++;
39
Gustavo A. R. Silvac2b8bd42019-01-09 17:23:54 +000040 slist = kzalloc(struct_size(slist, servers, nr_servers), GFP_KERNEL);
David Howellsd2ddc772017-11-02 15:27:50 +000041 if (!slist)
42 goto error;
43
44 refcount_set(&slist->usage, 1);
David Howellsd4a96bec2018-05-10 08:43:04 +010045 rwlock_init(&slist->lock);
David Howellsd2ddc772017-11-02 15:27:50 +000046
David Howells194d28cf2020-04-27 13:42:53 +010047 for (i = 0; i < AFS_MAXTYPES; i++)
48 slist->vids[i] = vldb->vid[i];
49
David Howellsd2ddc772017-11-02 15:27:50 +000050 /* Make sure a records exists for each server in the list. */
51 for (i = 0; i < vldb->nr_servers; i++) {
52 if (!(vldb->fs_mask[i] & type_mask))
53 continue;
54
David Howells81006802020-04-16 17:05:28 +010055 server = afs_lookup_server(cell, key, &vldb->fs_server[i],
56 vldb->addr_version[i]);
David Howellsd2ddc772017-11-02 15:27:50 +000057 if (IS_ERR(server)) {
58 ret = PTR_ERR(server);
David Howells45df8462018-02-06 14:12:32 +000059 if (ret == -ENOENT ||
60 ret == -ENOMEDIUM)
David Howellsd2ddc772017-11-02 15:27:50 +000061 continue;
62 goto error_2;
63 }
64
David Howellsd4a96bec2018-05-10 08:43:04 +010065 /* Insertion-sort by UUID */
David Howellsd2ddc772017-11-02 15:27:50 +000066 for (j = 0; j < slist->nr_servers; j++)
David Howellsd4a96bec2018-05-10 08:43:04 +010067 if (memcmp(&slist->servers[j].server->uuid,
68 &server->uuid,
69 sizeof(server->uuid)) >= 0)
David Howellsd2ddc772017-11-02 15:27:50 +000070 break;
71 if (j < slist->nr_servers) {
72 if (slist->servers[j].server == server) {
David Howells45218192019-06-20 18:12:17 +010073 afs_put_server(cell->net, server,
74 afs_server_trace_put_slist_isort);
David Howellsd2ddc772017-11-02 15:27:50 +000075 continue;
76 }
77
78 memmove(slist->servers + j + 1,
79 slist->servers + j,
80 (slist->nr_servers - j) * sizeof(struct afs_server_entry));
81 }
82
83 slist->servers[j].server = server;
84 slist->nr_servers++;
85 }
86
87 if (slist->nr_servers == 0) {
88 ret = -EDESTADDRREQ;
89 goto error_2;
90 }
91
92 return slist;
93
94error_2:
95 afs_put_serverlist(cell->net, slist);
96error:
97 return ERR_PTR(ret);
98}
99
100/*
101 * Copy the annotations from an old server list to its potential replacement.
102 */
103bool afs_annotate_server_list(struct afs_server_list *new,
104 struct afs_server_list *old)
105{
106 struct afs_server *cur;
107 int i, j;
108
109 if (old->nr_servers != new->nr_servers)
110 goto changed;
111
112 for (i = 0; i < old->nr_servers; i++)
113 if (old->servers[i].server != new->servers[i].server)
114 goto changed;
115
116 return false;
117
118changed:
David Howells3bf0fb62018-10-20 00:57:59 +0100119 /* Maintain the same preferred server as before if possible. */
120 cur = old->servers[old->preferred].server;
David Howellsd2ddc772017-11-02 15:27:50 +0000121 for (j = 0; j < new->nr_servers; j++) {
122 if (new->servers[j].server == cur) {
David Howells3bf0fb62018-10-20 00:57:59 +0100123 new->preferred = j;
David Howellsd2ddc772017-11-02 15:27:50 +0000124 break;
125 }
126 }
127
David Howellsd2ddc772017-11-02 15:27:50 +0000128 return true;
129}