Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 2 | /* AFS fileserver list management. |
| 3 | * |
| 4 | * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include "internal.h" |
| 11 | |
| 12 | void afs_put_serverlist(struct afs_net *net, struct afs_server_list *slist) |
| 13 | { |
| 14 | int i; |
| 15 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 16 | if (slist && refcount_dec_and_test(&slist->usage)) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 17 | for (i = 0; i < slist->nr_servers; i++) { |
| 18 | afs_put_cb_interest(net, slist->servers[i].cb_interest); |
| 19 | afs_put_server(net, slist->servers[i].server); |
| 20 | } |
| 21 | kfree(slist); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /* |
| 26 | * Build a server list from a VLDB record. |
| 27 | */ |
| 28 | struct afs_server_list *afs_alloc_server_list(struct afs_cell *cell, |
| 29 | struct key *key, |
| 30 | struct afs_vldb_entry *vldb, |
| 31 | u8 type_mask) |
| 32 | { |
| 33 | struct afs_server_list *slist; |
| 34 | struct afs_server *server; |
| 35 | int ret = -ENOMEM, nr_servers = 0, i, j; |
| 36 | |
| 37 | for (i = 0; i < vldb->nr_servers; i++) |
| 38 | if (vldb->fs_mask[i] & type_mask) |
| 39 | nr_servers++; |
| 40 | |
Gustavo A. R. Silva | c2b8bd4 | 2019-01-09 17:23:54 +0000 | [diff] [blame] | 41 | slist = kzalloc(struct_size(slist, servers, nr_servers), GFP_KERNEL); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 42 | if (!slist) |
| 43 | goto error; |
| 44 | |
| 45 | refcount_set(&slist->usage, 1); |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 46 | rwlock_init(&slist->lock); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 47 | |
| 48 | /* Make sure a records exists for each server in the list. */ |
| 49 | for (i = 0; i < vldb->nr_servers; i++) { |
| 50 | if (!(vldb->fs_mask[i] & type_mask)) |
| 51 | continue; |
| 52 | |
| 53 | server = afs_lookup_server(cell, key, &vldb->fs_server[i]); |
| 54 | if (IS_ERR(server)) { |
| 55 | ret = PTR_ERR(server); |
David Howells | 45df846 | 2018-02-06 14:12:32 +0000 | [diff] [blame] | 56 | if (ret == -ENOENT || |
| 57 | ret == -ENOMEDIUM) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 58 | continue; |
| 59 | goto error_2; |
| 60 | } |
| 61 | |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 62 | /* Insertion-sort by UUID */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 63 | for (j = 0; j < slist->nr_servers; j++) |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 64 | if (memcmp(&slist->servers[j].server->uuid, |
| 65 | &server->uuid, |
| 66 | sizeof(server->uuid)) >= 0) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 67 | break; |
| 68 | if (j < slist->nr_servers) { |
| 69 | if (slist->servers[j].server == server) { |
| 70 | afs_put_server(cell->net, server); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | memmove(slist->servers + j + 1, |
| 75 | slist->servers + j, |
| 76 | (slist->nr_servers - j) * sizeof(struct afs_server_entry)); |
| 77 | } |
| 78 | |
| 79 | slist->servers[j].server = server; |
| 80 | slist->nr_servers++; |
| 81 | } |
| 82 | |
| 83 | if (slist->nr_servers == 0) { |
| 84 | ret = -EDESTADDRREQ; |
| 85 | goto error_2; |
| 86 | } |
| 87 | |
| 88 | return slist; |
| 89 | |
| 90 | error_2: |
| 91 | afs_put_serverlist(cell->net, slist); |
| 92 | error: |
| 93 | return ERR_PTR(ret); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Copy the annotations from an old server list to its potential replacement. |
| 98 | */ |
| 99 | bool afs_annotate_server_list(struct afs_server_list *new, |
| 100 | struct afs_server_list *old) |
| 101 | { |
| 102 | struct afs_server *cur; |
| 103 | int i, j; |
| 104 | |
| 105 | if (old->nr_servers != new->nr_servers) |
| 106 | goto changed; |
| 107 | |
| 108 | for (i = 0; i < old->nr_servers; i++) |
| 109 | if (old->servers[i].server != new->servers[i].server) |
| 110 | goto changed; |
| 111 | |
| 112 | return false; |
| 113 | |
| 114 | changed: |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 115 | /* Maintain the same preferred server as before if possible. */ |
| 116 | cur = old->servers[old->preferred].server; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 117 | for (j = 0; j < new->nr_servers; j++) { |
| 118 | if (new->servers[j].server == cur) { |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 119 | new->preferred = j; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* Keep the old callback interest records where possible so that we |
| 125 | * maintain callback interception. |
| 126 | */ |
| 127 | i = 0; |
| 128 | j = 0; |
| 129 | while (i < old->nr_servers && j < new->nr_servers) { |
| 130 | if (new->servers[j].server == old->servers[i].server) { |
| 131 | struct afs_cb_interest *cbi = old->servers[i].cb_interest; |
| 132 | if (cbi) { |
| 133 | new->servers[j].cb_interest = cbi; |
| 134 | refcount_inc(&cbi->usage); |
| 135 | } |
| 136 | i++; |
| 137 | j++; |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if (new->servers[j].server < old->servers[i].server) { |
| 142 | j++; |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | i++; |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | return true; |
| 151 | } |