xref: /linux/fs/afs/server_list.c (revision dd5b2498d845f925904cb2afabb6ba11bfc317c5)
1 /* AFS fileserver list management.
2  *
3  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include "internal.h"
15 
16 void afs_put_serverlist(struct afs_net *net, struct afs_server_list *slist)
17 {
18 	int i;
19 
20 	if (slist && refcount_dec_and_test(&slist->usage)) {
21 		for (i = 0; i < slist->nr_servers; i++) {
22 			afs_put_cb_interest(net, slist->servers[i].cb_interest);
23 			afs_put_server(net, slist->servers[i].server);
24 		}
25 		kfree(slist);
26 	}
27 }
28 
29 /*
30  * Build a server list from a VLDB record.
31  */
32 struct afs_server_list *afs_alloc_server_list(struct afs_cell *cell,
33 					      struct key *key,
34 					      struct afs_vldb_entry *vldb,
35 					      u8 type_mask)
36 {
37 	struct afs_server_list *slist;
38 	struct afs_server *server;
39 	int ret = -ENOMEM, nr_servers = 0, i, j;
40 
41 	for (i = 0; i < vldb->nr_servers; i++)
42 		if (vldb->fs_mask[i] & type_mask)
43 			nr_servers++;
44 
45 	slist = kzalloc(struct_size(slist, servers, nr_servers), GFP_KERNEL);
46 	if (!slist)
47 		goto error;
48 
49 	refcount_set(&slist->usage, 1);
50 	rwlock_init(&slist->lock);
51 
52 	/* Make sure a records exists for each server in the list. */
53 	for (i = 0; i < vldb->nr_servers; i++) {
54 		if (!(vldb->fs_mask[i] & type_mask))
55 			continue;
56 
57 		server = afs_lookup_server(cell, key, &vldb->fs_server[i]);
58 		if (IS_ERR(server)) {
59 			ret = PTR_ERR(server);
60 			if (ret == -ENOENT ||
61 			    ret == -ENOMEDIUM)
62 				continue;
63 			goto error_2;
64 		}
65 
66 		/* Insertion-sort by UUID */
67 		for (j = 0; j < slist->nr_servers; j++)
68 			if (memcmp(&slist->servers[j].server->uuid,
69 				   &server->uuid,
70 				   sizeof(server->uuid)) >= 0)
71 				break;
72 		if (j < slist->nr_servers) {
73 			if (slist->servers[j].server == server) {
74 				afs_put_server(cell->net, server);
75 				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 
94 error_2:
95 	afs_put_serverlist(cell->net, slist);
96 error:
97 	return ERR_PTR(ret);
98 }
99 
100 /*
101  * Copy the annotations from an old server list to its potential replacement.
102  */
103 bool 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 
118 changed:
119 	/* Maintain the same preferred server as before if possible. */
120 	cur = old->servers[old->preferred].server;
121 	for (j = 0; j < new->nr_servers; j++) {
122 		if (new->servers[j].server == cur) {
123 			new->preferred = j;
124 			break;
125 		}
126 	}
127 
128 	/* Keep the old callback interest records where possible so that we
129 	 * maintain callback interception.
130 	 */
131 	i = 0;
132 	j = 0;
133 	while (i < old->nr_servers && j < new->nr_servers) {
134 		if (new->servers[j].server == old->servers[i].server) {
135 			struct afs_cb_interest *cbi = old->servers[i].cb_interest;
136 			if (cbi) {
137 				new->servers[j].cb_interest = cbi;
138 				refcount_inc(&cbi->usage);
139 			}
140 			i++;
141 			j++;
142 			continue;
143 		}
144 
145 		if (new->servers[j].server < old->servers[i].server) {
146 			j++;
147 			continue;
148 		}
149 
150 		i++;
151 		continue;
152 	}
153 
154 	return true;
155 }
156