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