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