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