David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1 | /* AFS Volume Location Service client |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2002 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/sched.h> |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 15 | #include "afs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "internal.h" |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 19 | * Deliver reply data to a VL.GetEntryByNameU call. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 21 | static int afs_deliver_vl_get_entry_by_name_u(struct afs_call *call) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 23 | struct afs_uvldbentry__xdr *uvldb; |
| 24 | struct afs_vldb_entry *entry; |
| 25 | bool new_only = false; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 26 | u32 tmp; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 27 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 29 | _enter(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 31 | ret = afs_transfer_reply(call); |
David Howells | 372ee16 | 2016-08-03 14:11:40 +0100 | [diff] [blame] | 32 | if (ret < 0) |
| 33 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 35 | /* unmarshall the reply once we've received all of it */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 36 | uvldb = call->buffer; |
David Howells | 97e3043 | 2017-11-02 15:27:48 +0000 | [diff] [blame] | 37 | entry = call->reply[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 39 | for (i = 0; i < ARRAY_SIZE(uvldb->name) - 1; i++) |
| 40 | entry->name[i] = (u8)ntohl(uvldb->name[i]); |
| 41 | entry->name[i] = 0; |
| 42 | entry->name_len = strlen(entry->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 44 | /* If there is a new replication site that we can use, ignore all the |
| 45 | * sites that aren't marked as new. |
| 46 | */ |
| 47 | for (i = 0; i < AFS_NMAXNSERVERS; i++) { |
| 48 | tmp = ntohl(uvldb->serverFlags[i]); |
| 49 | if (!(tmp & AFS_VLSF_DONTUSE) && |
| 50 | (tmp & AFS_VLSF_NEWREPSITE)) |
| 51 | new_only = true; |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 52 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 54 | for (i = 0; i < AFS_NMAXNSERVERS; i++) { |
| 55 | struct afs_uuid__xdr *xdr; |
| 56 | struct afs_uuid *uuid; |
| 57 | int j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 59 | tmp = ntohl(uvldb->serverFlags[i]); |
| 60 | if (tmp & AFS_VLSF_DONTUSE || |
| 61 | (new_only && !(tmp & AFS_VLSF_NEWREPSITE))) |
| 62 | continue; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 63 | if (tmp & AFS_VLSF_RWVOL) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 64 | entry->fs_mask[i] |= AFS_VOL_VTM_RW; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 65 | if (tmp & AFS_VLSF_ROVOL) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 66 | entry->fs_mask[i] |= AFS_VOL_VTM_RO; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 67 | if (tmp & AFS_VLSF_BACKVOL) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 68 | entry->fs_mask[i] |= AFS_VOL_VTM_BAK; |
| 69 | if (!entry->fs_mask[i]) |
| 70 | continue; |
| 71 | |
| 72 | xdr = &uvldb->serverNumber[i]; |
| 73 | uuid = (struct afs_uuid *)&entry->fs_server[i]; |
| 74 | uuid->time_low = xdr->time_low; |
| 75 | uuid->time_mid = htons(ntohl(xdr->time_mid)); |
| 76 | uuid->time_hi_and_version = htons(ntohl(xdr->time_hi_and_version)); |
| 77 | uuid->clock_seq_hi_and_reserved = (u8)ntohl(xdr->clock_seq_hi_and_reserved); |
| 78 | uuid->clock_seq_low = (u8)ntohl(xdr->clock_seq_low); |
| 79 | for (j = 0; j < 6; j++) |
| 80 | uuid->node[j] = (u8)ntohl(xdr->node[j]); |
| 81 | |
| 82 | entry->nr_servers++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 85 | for (i = 0; i < AFS_MAXTYPES; i++) |
| 86 | entry->vid[i] = ntohl(uvldb->volumeId[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 88 | tmp = ntohl(uvldb->flags); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 89 | if (tmp & AFS_VLF_RWEXISTS) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 90 | __set_bit(AFS_VLDB_HAS_RW, &entry->flags); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 91 | if (tmp & AFS_VLF_ROEXISTS) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 92 | __set_bit(AFS_VLDB_HAS_RO, &entry->flags); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 93 | if (tmp & AFS_VLF_BACKEXISTS) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 94 | __set_bit(AFS_VLDB_HAS_BAK, &entry->flags); |
| 95 | |
| 96 | if (!(tmp & (AFS_VLF_RWEXISTS | AFS_VLF_ROEXISTS | AFS_VLF_BACKEXISTS))) { |
| 97 | entry->error = -ENOMEDIUM; |
| 98 | __set_bit(AFS_VLDB_QUERY_ERROR, &entry->flags); |
| 99 | } |
| 100 | |
| 101 | __set_bit(AFS_VLDB_QUERY_VALID, &entry->flags); |
| 102 | _leave(" = 0 [done]"); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static void afs_destroy_vl_get_entry_by_name_u(struct afs_call *call) |
| 107 | { |
| 108 | kfree(call->reply[0]); |
| 109 | afs_flat_call_destructor(call); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * VL.GetEntryByNameU operation type. |
| 114 | */ |
| 115 | static const struct afs_call_type afs_RXVLGetEntryByNameU = { |
| 116 | .name = "VL.GetEntryByNameU", |
| 117 | .deliver = afs_deliver_vl_get_entry_by_name_u, |
| 118 | .destructor = afs_destroy_vl_get_entry_by_name_u, |
| 119 | }; |
| 120 | |
| 121 | /* |
| 122 | * Dispatch a get volume entry by name or ID operation (uuid variant). If the |
| 123 | * volname is a decimal number then it's a volume ID not a volume name. |
| 124 | */ |
| 125 | struct afs_vldb_entry *afs_vl_get_entry_by_name_u(struct afs_net *net, |
| 126 | struct afs_addr_cursor *ac, |
| 127 | struct key *key, |
| 128 | const char *volname, |
| 129 | int volnamesz) |
| 130 | { |
| 131 | struct afs_vldb_entry *entry; |
| 132 | struct afs_call *call; |
| 133 | size_t reqsz, padsz; |
| 134 | __be32 *bp; |
| 135 | |
| 136 | _enter(""); |
| 137 | |
| 138 | padsz = (4 - (volnamesz & 3)) & 3; |
| 139 | reqsz = 8 + volnamesz + padsz; |
| 140 | |
| 141 | entry = kzalloc(sizeof(struct afs_vldb_entry), GFP_KERNEL); |
| 142 | if (!entry) |
| 143 | return ERR_PTR(-ENOMEM); |
| 144 | |
| 145 | call = afs_alloc_flat_call(net, &afs_RXVLGetEntryByNameU, reqsz, |
| 146 | sizeof(struct afs_uvldbentry__xdr)); |
| 147 | if (!call) { |
| 148 | kfree(entry); |
| 149 | return ERR_PTR(-ENOMEM); |
| 150 | } |
| 151 | |
| 152 | call->key = key; |
| 153 | call->reply[0] = entry; |
| 154 | call->ret_reply0 = true; |
| 155 | |
| 156 | /* Marshall the parameters */ |
| 157 | bp = call->request; |
| 158 | *bp++ = htonl(VLGETENTRYBYNAMEU); |
| 159 | *bp++ = htonl(volnamesz); |
| 160 | memcpy(bp, volname, volnamesz); |
| 161 | if (padsz > 0) |
| 162 | memset((void *)bp + volnamesz, 0, padsz); |
| 163 | |
| 164 | return (struct afs_vldb_entry *)afs_make_call(ac, call, GFP_KERNEL, false); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Deliver reply data to a VL.GetAddrsU call. |
| 169 | * |
| 170 | * GetAddrsU(IN ListAddrByAttributes *inaddr, |
| 171 | * OUT afsUUID *uuidp1, |
| 172 | * OUT uint32_t *uniquifier, |
| 173 | * OUT uint32_t *nentries, |
| 174 | * OUT bulkaddrs *blkaddrs); |
| 175 | */ |
| 176 | static int afs_deliver_vl_get_addrs_u(struct afs_call *call) |
| 177 | { |
| 178 | struct afs_addr_list *alist; |
| 179 | __be32 *bp; |
| 180 | u32 uniquifier, nentries, count; |
| 181 | int i, ret; |
| 182 | |
| 183 | _enter("{%u,%zu/%u}", call->unmarshall, call->offset, call->count); |
| 184 | |
| 185 | again: |
| 186 | switch (call->unmarshall) { |
| 187 | case 0: |
| 188 | call->offset = 0; |
| 189 | call->unmarshall++; |
| 190 | |
| 191 | /* Extract the returned uuid, uniquifier, nentries and blkaddrs size */ |
| 192 | case 1: |
| 193 | ret = afs_extract_data(call, call->buffer, |
| 194 | sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32), |
| 195 | true); |
| 196 | if (ret < 0) |
| 197 | return ret; |
| 198 | |
| 199 | bp = call->buffer + sizeof(struct afs_uuid__xdr); |
| 200 | uniquifier = ntohl(*bp++); |
| 201 | nentries = ntohl(*bp++); |
| 202 | count = ntohl(*bp); |
| 203 | |
| 204 | nentries = min(nentries, count); |
| 205 | alist = afs_alloc_addrlist(nentries, FS_SERVICE, AFS_FS_PORT); |
| 206 | if (!alist) |
| 207 | return -ENOMEM; |
| 208 | alist->version = uniquifier; |
| 209 | call->reply[0] = alist; |
| 210 | call->count = count; |
| 211 | call->count2 = nentries; |
| 212 | call->offset = 0; |
| 213 | call->unmarshall++; |
| 214 | |
| 215 | /* Extract entries */ |
| 216 | case 2: |
| 217 | count = min(call->count, 4U); |
| 218 | ret = afs_extract_data(call, call->buffer, |
| 219 | count * sizeof(__be32), |
| 220 | call->count > 4); |
| 221 | if (ret < 0) |
| 222 | return ret; |
| 223 | |
| 224 | alist = call->reply[0]; |
| 225 | bp = call->buffer; |
| 226 | for (i = 0; i < count; i++) |
| 227 | if (alist->nr_addrs < call->count2) |
| 228 | afs_merge_fs_addr4(alist, *bp++); |
| 229 | |
| 230 | call->count -= count; |
| 231 | if (call->count > 0) |
| 232 | goto again; |
| 233 | call->offset = 0; |
| 234 | call->unmarshall++; |
| 235 | break; |
| 236 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 237 | |
| 238 | _leave(" = 0 [done]"); |
| 239 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 242 | static void afs_vl_get_addrs_u_destructor(struct afs_call *call) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 244 | afs_put_server(call->net, (struct afs_server *)call->reply[0]); |
| 245 | kfree(call->reply[1]); |
| 246 | return afs_flat_call_destructor(call); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 250 | * VL.GetAddrsU operation type. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 252 | static const struct afs_call_type afs_RXVLGetAddrsU = { |
| 253 | .name = "VL.GetAddrsU", |
| 254 | .deliver = afs_deliver_vl_get_addrs_u, |
| 255 | .destructor = afs_vl_get_addrs_u_destructor, |
| 256 | }; |
| 257 | |
| 258 | /* |
| 259 | * Dispatch an operation to get the addresses for a server, where the server is |
| 260 | * nominated by UUID. |
| 261 | */ |
| 262 | struct afs_addr_list *afs_vl_get_addrs_u(struct afs_net *net, |
| 263 | struct afs_addr_cursor *ac, |
| 264 | struct key *key, |
| 265 | const uuid_t *uuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 267 | struct afs_ListAddrByAttributes__xdr *r; |
| 268 | const struct afs_uuid *u = (const struct afs_uuid *)uuid; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 269 | struct afs_call *call; |
| 270 | __be32 *bp; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 271 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 273 | _enter(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 275 | call = afs_alloc_flat_call(net, &afs_RXVLGetAddrsU, |
| 276 | sizeof(__be32) + sizeof(struct afs_ListAddrByAttributes__xdr), |
| 277 | sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32)); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 278 | if (!call) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 279 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 281 | call->key = key; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 282 | call->reply[0] = NULL; |
| 283 | call->ret_reply0 = true; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 284 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 285 | /* Marshall the parameters */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 286 | bp = call->request; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 287 | *bp++ = htonl(VLGETADDRSU); |
| 288 | r = (struct afs_ListAddrByAttributes__xdr *)bp; |
| 289 | r->Mask = htonl(AFS_VLADDR_UUID); |
| 290 | r->ipaddr = 0; |
| 291 | r->index = 0; |
| 292 | r->spare = 0; |
| 293 | r->uuid.time_low = u->time_low; |
| 294 | r->uuid.time_mid = htonl(ntohs(u->time_mid)); |
| 295 | r->uuid.time_hi_and_version = htonl(ntohs(u->time_hi_and_version)); |
| 296 | r->uuid.clock_seq_hi_and_reserved = htonl(u->clock_seq_hi_and_reserved); |
| 297 | r->uuid.clock_seq_low = htonl(u->clock_seq_low); |
| 298 | for (i = 0; i < 6; i++) |
| 299 | r->uuid.node[i] = ntohl(u->node[i]); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 300 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 301 | return (struct afs_addr_list *)afs_make_call(ac, call, GFP_KERNEL, false); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 302 | } |