blob: 888d91d195d9f4baa9131d5e586c3bf2b4662eeb [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 Howellsd2ddc772017-11-02 15:27:50 +000017 for (i = 0; i < slist->nr_servers; i++) {
18 afs_put_cb_interest(net, slist->servers[i].cb_interest);
David Howells45218192019-06-20 18:12:17 +010019 afs_put_server(net, slist->servers[i].server,
20 afs_server_trace_put_slist);
David Howellsd2ddc772017-11-02 15:27:50 +000021 }
22 kfree(slist);
23 }
24}
25
26/*
27 * Build a server list from a VLDB record.
28 */
29struct afs_server_list *afs_alloc_server_list(struct afs_cell *cell,
30 struct key *key,
31 struct afs_vldb_entry *vldb,
32 u8 type_mask)
33{
34 struct afs_server_list *slist;
35 struct afs_server *server;
36 int ret = -ENOMEM, nr_servers = 0, i, j;
37
38 for (i = 0; i < vldb->nr_servers; i++)
39 if (vldb->fs_mask[i] & type_mask)
40 nr_servers++;
41
Gustavo A. R. Silvac2b8bd42019-01-09 17:23:54 +000042 slist = kzalloc(struct_size(slist, servers, nr_servers), GFP_KERNEL);
David Howellsd2ddc772017-11-02 15:27:50 +000043 if (!slist)
44 goto error;
45
46 refcount_set(&slist->usage, 1);
David Howellsd4a96be2018-05-10 08:43:04 +010047 rwlock_init(&slist->lock);
David Howellsd2ddc772017-11-02 15:27:50 +000048
49 /* Make sure a records exists for each server in the list. */
50 for (i = 0; i < vldb->nr_servers; i++) {
51 if (!(vldb->fs_mask[i] & type_mask))
52 continue;
53
54 server = afs_lookup_server(cell, key, &vldb->fs_server[i]);
55 if (IS_ERR(server)) {
56 ret = PTR_ERR(server);
David Howells45df8462018-02-06 14:12:32 +000057 if (ret == -ENOENT ||
58 ret == -ENOMEDIUM)
David Howellsd2ddc772017-11-02 15:27:50 +000059 continue;
60 goto error_2;
61 }
62
David Howellsd4a96be2018-05-10 08:43:04 +010063 /* Insertion-sort by UUID */
David Howellsd2ddc772017-11-02 15:27:50 +000064 for (j = 0; j < slist->nr_servers; j++)
David Howellsd4a96be2018-05-10 08:43:04 +010065 if (memcmp(&slist->servers[j].server->uuid,
66 &server->uuid,
67 sizeof(server->uuid)) >= 0)
David Howellsd2ddc772017-11-02 15:27:50 +000068 break;
69 if (j < slist->nr_servers) {
70 if (slist->servers[j].server == server) {
David Howells45218192019-06-20 18:12:17 +010071 afs_put_server(cell->net, server,
72 afs_server_trace_put_slist_isort);
David Howellsd2ddc772017-11-02 15:27:50 +000073 continue;
74 }
75
76 memmove(slist->servers + j + 1,
77 slist->servers + j,
78 (slist->nr_servers - j) * sizeof(struct afs_server_entry));
79 }
80
81 slist->servers[j].server = server;
82 slist->nr_servers++;
83 }
84
85 if (slist->nr_servers == 0) {
86 ret = -EDESTADDRREQ;
87 goto error_2;
88 }
89
90 return slist;
91
92error_2:
93 afs_put_serverlist(cell->net, slist);
94error:
95 return ERR_PTR(ret);
96}
97
98/*
99 * Copy the annotations from an old server list to its potential replacement.
100 */
101bool afs_annotate_server_list(struct afs_server_list *new,
102 struct afs_server_list *old)
103{
104 struct afs_server *cur;
105 int i, j;
106
107 if (old->nr_servers != new->nr_servers)
108 goto changed;
109
110 for (i = 0; i < old->nr_servers; i++)
111 if (old->servers[i].server != new->servers[i].server)
112 goto changed;
113
114 return false;
115
116changed:
David Howells3bf0fb62018-10-20 00:57:59 +0100117 /* Maintain the same preferred server as before if possible. */
118 cur = old->servers[old->preferred].server;
David Howellsd2ddc772017-11-02 15:27:50 +0000119 for (j = 0; j < new->nr_servers; j++) {
120 if (new->servers[j].server == cur) {
David Howells3bf0fb62018-10-20 00:57:59 +0100121 new->preferred = j;
David Howellsd2ddc772017-11-02 15:27:50 +0000122 break;
123 }
124 }
125
126 /* Keep the old callback interest records where possible so that we
127 * maintain callback interception.
128 */
129 i = 0;
130 j = 0;
131 while (i < old->nr_servers && j < new->nr_servers) {
132 if (new->servers[j].server == old->servers[i].server) {
133 struct afs_cb_interest *cbi = old->servers[i].cb_interest;
134 if (cbi) {
135 new->servers[j].cb_interest = cbi;
136 refcount_inc(&cbi->usage);
137 }
138 i++;
139 j++;
140 continue;
141 }
142
143 if (new->servers[j].server < old->servers[i].server) {
144 j++;
145 continue;
146 }
147
148 i++;
149 continue;
150 }
151
152 return true;
153}