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 
afs_put_serverlist(struct afs_net * net,struct afs_server_list * slist)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  */
afs_alloc_server_list(struct afs_cell * cell,struct key * key,struct afs_vldb_entry * vldb,u8 type_mask)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(sizeof(struct afs_server_list) +
46 			sizeof(struct afs_server_entry) * nr_servers,
47 			GFP_KERNEL);
48 	if (!slist)
49 		goto error;
50 
51 	refcount_set(&slist->usage, 1);
52 	rwlock_init(&slist->lock);
53 
54 	/* Make sure a records exists for each server in the list. */
55 	for (i = 0; i < vldb->nr_servers; i++) {
56 		if (!(vldb->fs_mask[i] & type_mask))
57 			continue;
58 
59 		server = afs_lookup_server(cell, key, &vldb->fs_server[i]);
60 		if (IS_ERR(server)) {
61 			ret = PTR_ERR(server);
62 			if (ret == -ENOENT ||
63 			    ret == -ENOMEDIUM)
64 				continue;
65 			goto error_2;
66 		}
67 
68 		/* Insertion-sort by UUID */
69 		for (j = 0; j < slist->nr_servers; j++)
70 			if (memcmp(&slist->servers[j].server->uuid,
71 				   &server->uuid,
72 				   sizeof(server->uuid)) >= 0)
73 				break;
74 		if (j < slist->nr_servers) {
75 			if (slist->servers[j].server == server) {
76 				afs_put_server(cell->net, server);
77 				continue;
78 			}
79 
80 			memmove(slist->servers + j + 1,
81 				slist->servers + j,
82 				(slist->nr_servers - j) * sizeof(struct afs_server_entry));
83 		}
84 
85 		slist->servers[j].server = server;
86 		slist->nr_servers++;
87 	}
88 
89 	if (slist->nr_servers == 0) {
90 		ret = -EDESTADDRREQ;
91 		goto error_2;
92 	}
93 
94 	return slist;
95 
96 error_2:
97 	afs_put_serverlist(cell->net, slist);
98 error:
99 	return ERR_PTR(ret);
100 }
101 
102 /*
103  * Copy the annotations from an old server list to its potential replacement.
104  */
afs_annotate_server_list(struct afs_server_list * new,struct afs_server_list * old)105 bool afs_annotate_server_list(struct afs_server_list *new,
106 			      struct afs_server_list *old)
107 {
108 	struct afs_server *cur;
109 	int i, j;
110 
111 	if (old->nr_servers != new->nr_servers)
112 		goto changed;
113 
114 	for (i = 0; i < old->nr_servers; i++)
115 		if (old->servers[i].server != new->servers[i].server)
116 			goto changed;
117 
118 	return false;
119 
120 changed:
121 	/* Maintain the same current server as before if possible. */
122 	cur = old->servers[old->index].server;
123 	for (j = 0; j < new->nr_servers; j++) {
124 		if (new->servers[j].server == cur) {
125 			new->index = j;
126 			break;
127 		}
128 	}
129 
130 	/* Keep the old callback interest records where possible so that we
131 	 * maintain callback interception.
132 	 */
133 	i = 0;
134 	j = 0;
135 	while (i < old->nr_servers && j < new->nr_servers) {
136 		if (new->servers[j].server == old->servers[i].server) {
137 			struct afs_cb_interest *cbi = old->servers[i].cb_interest;
138 			if (cbi) {
139 				new->servers[j].cb_interest = cbi;
140 				refcount_inc(&cbi->usage);
141 			}
142 			i++;
143 			j++;
144 			continue;
145 		}
146 
147 		if (new->servers[j].server < old->servers[i].server) {
148 			j++;
149 			continue;
150 		}
151 
152 		i++;
153 		continue;
154 	}
155 
156 	return true;
157 }
158