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 | 2032596 | 2020-04-30 01:03:49 +0100 | [diff] [blame] | 17 | for (i = 0; i < slist->nr_servers; i++) |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 18 | afs_unuse_server(net, slist->servers[i].server, |
| 19 | afs_server_trace_put_slist); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 20 | kfree(slist); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * Build a server list from a VLDB record. |
| 26 | */ |
| 27 | struct afs_server_list *afs_alloc_server_list(struct afs_cell *cell, |
| 28 | struct key *key, |
| 29 | struct afs_vldb_entry *vldb, |
| 30 | u8 type_mask) |
| 31 | { |
| 32 | struct afs_server_list *slist; |
| 33 | struct afs_server *server; |
| 34 | int ret = -ENOMEM, nr_servers = 0, i, j; |
| 35 | |
| 36 | for (i = 0; i < vldb->nr_servers; i++) |
| 37 | if (vldb->fs_mask[i] & type_mask) |
| 38 | nr_servers++; |
| 39 | |
Gustavo A. R. Silva | c2b8bd4 | 2019-01-09 17:23:54 +0000 | [diff] [blame] | 40 | slist = kzalloc(struct_size(slist, servers, nr_servers), GFP_KERNEL); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 41 | if (!slist) |
| 42 | goto error; |
| 43 | |
| 44 | refcount_set(&slist->usage, 1); |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 45 | rwlock_init(&slist->lock); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 46 | |
David Howells | 194d28cf | 2020-04-27 13:42:53 +0100 | [diff] [blame] | 47 | for (i = 0; i < AFS_MAXTYPES; i++) |
| 48 | slist->vids[i] = vldb->vid[i]; |
| 49 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 50 | /* Make sure a records exists for each server in the list. */ |
| 51 | for (i = 0; i < vldb->nr_servers; i++) { |
| 52 | if (!(vldb->fs_mask[i] & type_mask)) |
| 53 | continue; |
| 54 | |
David Howells | 8100680 | 2020-04-16 17:05:28 +0100 | [diff] [blame] | 55 | server = afs_lookup_server(cell, key, &vldb->fs_server[i], |
| 56 | vldb->addr_version[i]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 57 | if (IS_ERR(server)) { |
| 58 | ret = PTR_ERR(server); |
David Howells | 45df846 | 2018-02-06 14:12:32 +0000 | [diff] [blame] | 59 | if (ret == -ENOENT || |
| 60 | ret == -ENOMEDIUM) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 61 | continue; |
| 62 | goto error_2; |
| 63 | } |
| 64 | |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 65 | /* Insertion-sort by UUID */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 66 | for (j = 0; j < slist->nr_servers; j++) |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 67 | if (memcmp(&slist->servers[j].server->uuid, |
| 68 | &server->uuid, |
| 69 | sizeof(server->uuid)) >= 0) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 70 | break; |
| 71 | if (j < slist->nr_servers) { |
| 72 | if (slist->servers[j].server == server) { |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 73 | afs_put_server(cell->net, server, |
| 74 | afs_server_trace_put_slist_isort); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 75 | continue; |
| 76 | } |
| 77 | |
| 78 | memmove(slist->servers + j + 1, |
| 79 | slist->servers + j, |
| 80 | (slist->nr_servers - j) * sizeof(struct afs_server_entry)); |
| 81 | } |
| 82 | |
| 83 | slist->servers[j].server = server; |
| 84 | slist->nr_servers++; |
| 85 | } |
| 86 | |
| 87 | if (slist->nr_servers == 0) { |
| 88 | ret = -EDESTADDRREQ; |
| 89 | goto error_2; |
| 90 | } |
| 91 | |
| 92 | return slist; |
| 93 | |
| 94 | error_2: |
| 95 | afs_put_serverlist(cell->net, slist); |
| 96 | error: |
| 97 | return ERR_PTR(ret); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Copy the annotations from an old server list to its potential replacement. |
| 102 | */ |
| 103 | bool afs_annotate_server_list(struct afs_server_list *new, |
| 104 | struct afs_server_list *old) |
| 105 | { |
| 106 | struct afs_server *cur; |
| 107 | int i, j; |
| 108 | |
| 109 | if (old->nr_servers != new->nr_servers) |
| 110 | goto changed; |
| 111 | |
| 112 | for (i = 0; i < old->nr_servers; i++) |
| 113 | if (old->servers[i].server != new->servers[i].server) |
| 114 | goto changed; |
| 115 | |
| 116 | return false; |
| 117 | |
| 118 | changed: |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 119 | /* Maintain the same preferred server as before if possible. */ |
| 120 | cur = old->servers[old->preferred].server; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 121 | for (j = 0; j < new->nr_servers; j++) { |
| 122 | if (new->servers[j].server == cur) { |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 123 | new->preferred = j; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 128 | return true; |
| 129 | } |