Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[cris-mirror.git] / fs / afs / server_list.c
blob0f8dc4c8f07c43b3efb0f899b8a40b8f1a697e6c
1 /* AFS fileserver list management.
3 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include "internal.h"
16 void afs_put_serverlist(struct afs_net *net, struct afs_server_list *slist)
18 int i;
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);
25 kfree(slist);
30 * Build a server list from a VLDB record.
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)
37 struct afs_server_list *slist;
38 struct afs_server *server;
39 int ret = -ENOMEM, nr_servers = 0, i, j;
41 for (i = 0; i < vldb->nr_servers; i++)
42 if (vldb->fs_mask[i] & type_mask)
43 nr_servers++;
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;
51 refcount_set(&slist->usage, 1);
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;
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;
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;
77 memmove(slist->servers + j + 1,
78 slist->servers + j,
79 (slist->nr_servers - j) * sizeof(struct afs_server_entry));
82 slist->servers[j].server = server;
83 slist->nr_servers++;
86 if (slist->nr_servers == 0) {
87 ret = -EDESTADDRREQ;
88 goto error_2;
91 return slist;
93 error_2:
94 afs_put_serverlist(cell->net, slist);
95 error:
96 return ERR_PTR(ret);
100 * Copy the annotations from an old server list to its potential replacement.
102 bool afs_annotate_server_list(struct afs_server_list *new,
103 struct afs_server_list *old)
105 struct afs_server *cur;
106 int i, j;
108 if (old->nr_servers != new->nr_servers)
109 goto changed;
111 for (i = 0; i < old->nr_servers; i++)
112 if (old->servers[i].server != new->servers[i].server)
113 goto changed;
115 return false;
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;
127 /* Keep the old callback interest records where possible so that we
128 * maintain callback interception.
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);
139 i++;
140 j++;
141 continue;
144 if (new->servers[j].server < old->servers[i].server) {
145 j++;
146 continue;
149 i++;
150 continue;
153 return true;