]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/afs/vlclient.c
afs: Mark expected switch fall-throughs
[linux.git] / fs / afs / vlclient.c
1 /* AFS Volume Location Service client
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
12 #include <linux/gfp.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include "afs_fs.h"
16 #include "internal.h"
17
18 /*
19  * Deliver reply data to a VL.GetEntryByNameU call.
20  */
21 static int afs_deliver_vl_get_entry_by_name_u(struct afs_call *call)
22 {
23         struct afs_uvldbentry__xdr *uvldb;
24         struct afs_vldb_entry *entry;
25         bool new_only = false;
26         u32 tmp, nr_servers, vlflags;
27         int i, ret;
28
29         _enter("");
30
31         ret = afs_transfer_reply(call);
32         if (ret < 0)
33                 return ret;
34
35         /* unmarshall the reply once we've received all of it */
36         uvldb = call->buffer;
37         entry = call->reply[0];
38
39         nr_servers = ntohl(uvldb->nServers);
40         if (nr_servers > AFS_NMAXNSERVERS)
41                 nr_servers = AFS_NMAXNSERVERS;
42
43         for (i = 0; i < ARRAY_SIZE(uvldb->name) - 1; i++)
44                 entry->name[i] = (u8)ntohl(uvldb->name[i]);
45         entry->name[i] = 0;
46         entry->name_len = strlen(entry->name);
47
48         /* If there is a new replication site that we can use, ignore all the
49          * sites that aren't marked as new.
50          */
51         for (i = 0; i < nr_servers; i++) {
52                 tmp = ntohl(uvldb->serverFlags[i]);
53                 if (!(tmp & AFS_VLSF_DONTUSE) &&
54                     (tmp & AFS_VLSF_NEWREPSITE))
55                         new_only = true;
56         }
57
58         vlflags = ntohl(uvldb->flags);
59         for (i = 0; i < nr_servers; i++) {
60                 struct afs_uuid__xdr *xdr;
61                 struct afs_uuid *uuid;
62                 int j;
63
64                 tmp = ntohl(uvldb->serverFlags[i]);
65                 if (tmp & AFS_VLSF_DONTUSE ||
66                     (new_only && !(tmp & AFS_VLSF_NEWREPSITE)))
67                         continue;
68                 if (tmp & AFS_VLSF_RWVOL) {
69                         entry->fs_mask[i] |= AFS_VOL_VTM_RW;
70                         if (vlflags & AFS_VLF_BACKEXISTS)
71                                 entry->fs_mask[i] |= AFS_VOL_VTM_BAK;
72                 }
73                 if (tmp & AFS_VLSF_ROVOL)
74                         entry->fs_mask[i] |= AFS_VOL_VTM_RO;
75                 if (!entry->fs_mask[i])
76                         continue;
77
78                 xdr = &uvldb->serverNumber[i];
79                 uuid = (struct afs_uuid *)&entry->fs_server[i];
80                 uuid->time_low                  = xdr->time_low;
81                 uuid->time_mid                  = htons(ntohl(xdr->time_mid));
82                 uuid->time_hi_and_version       = htons(ntohl(xdr->time_hi_and_version));
83                 uuid->clock_seq_hi_and_reserved = (u8)ntohl(xdr->clock_seq_hi_and_reserved);
84                 uuid->clock_seq_low             = (u8)ntohl(xdr->clock_seq_low);
85                 for (j = 0; j < 6; j++)
86                         uuid->node[j] = (u8)ntohl(xdr->node[j]);
87
88                 entry->nr_servers++;
89         }
90
91         for (i = 0; i < AFS_MAXTYPES; i++)
92                 entry->vid[i] = ntohl(uvldb->volumeId[i]);
93
94         if (vlflags & AFS_VLF_RWEXISTS)
95                 __set_bit(AFS_VLDB_HAS_RW, &entry->flags);
96         if (vlflags & AFS_VLF_ROEXISTS)
97                 __set_bit(AFS_VLDB_HAS_RO, &entry->flags);
98         if (vlflags & AFS_VLF_BACKEXISTS)
99                 __set_bit(AFS_VLDB_HAS_BAK, &entry->flags);
100
101         if (!(vlflags & (AFS_VLF_RWEXISTS | AFS_VLF_ROEXISTS | AFS_VLF_BACKEXISTS))) {
102                 entry->error = -ENOMEDIUM;
103                 __set_bit(AFS_VLDB_QUERY_ERROR, &entry->flags);
104         }
105
106         __set_bit(AFS_VLDB_QUERY_VALID, &entry->flags);
107         _leave(" = 0 [done]");
108         return 0;
109 }
110
111 static void afs_destroy_vl_get_entry_by_name_u(struct afs_call *call)
112 {
113         kfree(call->reply[0]);
114         afs_flat_call_destructor(call);
115 }
116
117 /*
118  * VL.GetEntryByNameU operation type.
119  */
120 static const struct afs_call_type afs_RXVLGetEntryByNameU = {
121         .name           = "VL.GetEntryByNameU",
122         .op             = afs_VL_GetEntryByNameU,
123         .deliver        = afs_deliver_vl_get_entry_by_name_u,
124         .destructor     = afs_destroy_vl_get_entry_by_name_u,
125 };
126
127 /*
128  * Dispatch a get volume entry by name or ID operation (uuid variant).  If the
129  * volname is a decimal number then it's a volume ID not a volume name.
130  */
131 struct afs_vldb_entry *afs_vl_get_entry_by_name_u(struct afs_vl_cursor *vc,
132                                                   const char *volname,
133                                                   int volnamesz)
134 {
135         struct afs_vldb_entry *entry;
136         struct afs_call *call;
137         struct afs_net *net = vc->cell->net;
138         size_t reqsz, padsz;
139         __be32 *bp;
140
141         _enter("");
142
143         padsz = (4 - (volnamesz & 3)) & 3;
144         reqsz = 8 + volnamesz + padsz;
145
146         entry = kzalloc(sizeof(struct afs_vldb_entry), GFP_KERNEL);
147         if (!entry)
148                 return ERR_PTR(-ENOMEM);
149
150         call = afs_alloc_flat_call(net, &afs_RXVLGetEntryByNameU, reqsz,
151                                    sizeof(struct afs_uvldbentry__xdr));
152         if (!call) {
153                 kfree(entry);
154                 return ERR_PTR(-ENOMEM);
155         }
156
157         call->key = vc->key;
158         call->reply[0] = entry;
159         call->ret_reply0 = true;
160
161         /* Marshall the parameters */
162         bp = call->request;
163         *bp++ = htonl(VLGETENTRYBYNAMEU);
164         *bp++ = htonl(volnamesz);
165         memcpy(bp, volname, volnamesz);
166         if (padsz > 0)
167                 memset((void *)bp + volnamesz, 0, padsz);
168
169         trace_afs_make_vl_call(call);
170         return (struct afs_vldb_entry *)afs_make_call(&vc->ac, call, GFP_KERNEL, false);
171 }
172
173 /*
174  * Deliver reply data to a VL.GetAddrsU call.
175  *
176  *      GetAddrsU(IN ListAddrByAttributes *inaddr,
177  *                OUT afsUUID *uuidp1,
178  *                OUT uint32_t *uniquifier,
179  *                OUT uint32_t *nentries,
180  *                OUT bulkaddrs *blkaddrs);
181  */
182 static int afs_deliver_vl_get_addrs_u(struct afs_call *call)
183 {
184         struct afs_addr_list *alist;
185         __be32 *bp;
186         u32 uniquifier, nentries, count;
187         int i, ret;
188
189         _enter("{%u,%zu/%u}",
190                call->unmarshall, iov_iter_count(call->_iter), call->count);
191
192         switch (call->unmarshall) {
193         case 0:
194                 afs_extract_to_buf(call,
195                                    sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
196                 call->unmarshall++;
197
198                 /* Extract the returned uuid, uniquifier, nentries and
199                  * blkaddrs size */
200                 /* Fall through */
201         case 1:
202                 ret = afs_extract_data(call, true);
203                 if (ret < 0)
204                         return ret;
205
206                 bp = call->buffer + sizeof(struct afs_uuid__xdr);
207                 uniquifier      = ntohl(*bp++);
208                 nentries        = ntohl(*bp++);
209                 count           = ntohl(*bp);
210
211                 nentries = min(nentries, count);
212                 alist = afs_alloc_addrlist(nentries, FS_SERVICE, AFS_FS_PORT);
213                 if (!alist)
214                         return -ENOMEM;
215                 alist->version = uniquifier;
216                 call->reply[0] = alist;
217                 call->count = count;
218                 call->count2 = nentries;
219                 call->unmarshall++;
220
221         more_entries:
222                 count = min(call->count, 4U);
223                 afs_extract_to_buf(call, count * sizeof(__be32));
224
225                 /* Fall through - and extract entries */
226         case 2:
227                 ret = afs_extract_data(call, call->count > 4);
228                 if (ret < 0)
229                         return ret;
230
231                 alist = call->reply[0];
232                 bp = call->buffer;
233                 count = min(call->count, 4U);
234                 for (i = 0; i < count; i++)
235                         if (alist->nr_addrs < call->count2)
236                                 afs_merge_fs_addr4(alist, *bp++, AFS_FS_PORT);
237
238                 call->count -= count;
239                 if (call->count > 0)
240                         goto more_entries;
241                 call->unmarshall++;
242                 break;
243         }
244
245         _leave(" = 0 [done]");
246         return 0;
247 }
248
249 static void afs_vl_get_addrs_u_destructor(struct afs_call *call)
250 {
251         afs_put_server(call->net, (struct afs_server *)call->reply[0]);
252         kfree(call->reply[1]);
253         return afs_flat_call_destructor(call);
254 }
255
256 /*
257  * VL.GetAddrsU operation type.
258  */
259 static const struct afs_call_type afs_RXVLGetAddrsU = {
260         .name           = "VL.GetAddrsU",
261         .op             = afs_VL_GetAddrsU,
262         .deliver        = afs_deliver_vl_get_addrs_u,
263         .destructor     = afs_vl_get_addrs_u_destructor,
264 };
265
266 /*
267  * Dispatch an operation to get the addresses for a server, where the server is
268  * nominated by UUID.
269  */
270 struct afs_addr_list *afs_vl_get_addrs_u(struct afs_vl_cursor *vc,
271                                          const uuid_t *uuid)
272 {
273         struct afs_ListAddrByAttributes__xdr *r;
274         const struct afs_uuid *u = (const struct afs_uuid *)uuid;
275         struct afs_call *call;
276         struct afs_net *net = vc->cell->net;
277         __be32 *bp;
278         int i;
279
280         _enter("");
281
282         call = afs_alloc_flat_call(net, &afs_RXVLGetAddrsU,
283                                    sizeof(__be32) + sizeof(struct afs_ListAddrByAttributes__xdr),
284                                    sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
285         if (!call)
286                 return ERR_PTR(-ENOMEM);
287
288         call->key = vc->key;
289         call->reply[0] = NULL;
290         call->ret_reply0 = true;
291
292         /* Marshall the parameters */
293         bp = call->request;
294         *bp++ = htonl(VLGETADDRSU);
295         r = (struct afs_ListAddrByAttributes__xdr *)bp;
296         r->Mask         = htonl(AFS_VLADDR_UUID);
297         r->ipaddr       = 0;
298         r->index        = 0;
299         r->spare        = 0;
300         r->uuid.time_low                        = u->time_low;
301         r->uuid.time_mid                        = htonl(ntohs(u->time_mid));
302         r->uuid.time_hi_and_version             = htonl(ntohs(u->time_hi_and_version));
303         r->uuid.clock_seq_hi_and_reserved       = htonl(u->clock_seq_hi_and_reserved);
304         r->uuid.clock_seq_low                   = htonl(u->clock_seq_low);
305         for (i = 0; i < 6; i++)
306                 r->uuid.node[i] = htonl(u->node[i]);
307
308         trace_afs_make_vl_call(call);
309         return (struct afs_addr_list *)afs_make_call(&vc->ac, call, GFP_KERNEL, false);
310 }
311
312 /*
313  * Deliver reply data to an VL.GetCapabilities operation.
314  */
315 static int afs_deliver_vl_get_capabilities(struct afs_call *call)
316 {
317         u32 count;
318         int ret;
319
320         _enter("{%u,%zu/%u}",
321                call->unmarshall, iov_iter_count(call->_iter), call->count);
322
323         switch (call->unmarshall) {
324         case 0:
325                 afs_extract_to_tmp(call);
326                 call->unmarshall++;
327
328                 /* Fall through - and extract the capabilities word count */
329         case 1:
330                 ret = afs_extract_data(call, true);
331                 if (ret < 0)
332                         return ret;
333
334                 count = ntohl(call->tmp);
335                 call->count = count;
336                 call->count2 = count;
337
338                 call->unmarshall++;
339                 afs_extract_discard(call, count * sizeof(__be32));
340
341                 /* Fall through - and extract capabilities words */
342         case 2:
343                 ret = afs_extract_data(call, false);
344                 if (ret < 0)
345                         return ret;
346
347                 /* TODO: Examine capabilities */
348
349                 call->unmarshall++;
350                 break;
351         }
352
353         _leave(" = 0 [done]");
354         return 0;
355 }
356
357 static void afs_destroy_vl_get_capabilities(struct afs_call *call)
358 {
359         struct afs_vlserver *server = call->reply[0];
360
361         afs_put_vlserver(call->net, server);
362         afs_flat_call_destructor(call);
363 }
364
365 /*
366  * VL.GetCapabilities operation type
367  */
368 static const struct afs_call_type afs_RXVLGetCapabilities = {
369         .name           = "VL.GetCapabilities",
370         .op             = afs_VL_GetCapabilities,
371         .deliver        = afs_deliver_vl_get_capabilities,
372         .done           = afs_vlserver_probe_result,
373         .destructor     = afs_destroy_vl_get_capabilities,
374 };
375
376 /*
377  * Probe a volume server for the capabilities that it supports.  This can
378  * return up to 196 words.
379  *
380  * We use this to probe for service upgrade to determine what the server at the
381  * other end supports.
382  */
383 int afs_vl_get_capabilities(struct afs_net *net,
384                             struct afs_addr_cursor *ac,
385                             struct key *key,
386                             struct afs_vlserver *server,
387                             unsigned int server_index,
388                             bool async)
389 {
390         struct afs_call *call;
391         __be32 *bp;
392
393         _enter("");
394
395         call = afs_alloc_flat_call(net, &afs_RXVLGetCapabilities, 1 * 4, 16 * 4);
396         if (!call)
397                 return -ENOMEM;
398
399         call->key = key;
400         call->reply[0] = afs_get_vlserver(server);
401         call->reply[1] = (void *)(long)server_index;
402         call->upgrade = true;
403         call->want_reply_time = true;
404
405         /* marshall the parameters */
406         bp = call->request;
407         *bp++ = htonl(VLGETCAPABILITIES);
408
409         /* Can't take a ref on server */
410         trace_afs_make_vl_call(call);
411         return afs_make_call(ac, call, GFP_KERNEL, async);
412 }
413
414 /*
415  * Deliver reply data to a YFSVL.GetEndpoints call.
416  *
417  *      GetEndpoints(IN yfsServerAttributes *attr,
418  *                   OUT opr_uuid *uuid,
419  *                   OUT afs_int32 *uniquifier,
420  *                   OUT endpoints *fsEndpoints,
421  *                   OUT endpoints *volEndpoints)
422  */
423 static int afs_deliver_yfsvl_get_endpoints(struct afs_call *call)
424 {
425         struct afs_addr_list *alist;
426         __be32 *bp;
427         u32 uniquifier, size;
428         int ret;
429
430         _enter("{%u,%zu,%u}",
431                call->unmarshall, iov_iter_count(call->_iter), call->count2);
432
433         switch (call->unmarshall) {
434         case 0:
435                 afs_extract_to_buf(call, sizeof(uuid_t) + 3 * sizeof(__be32));
436                 call->unmarshall = 1;
437
438                 /* Extract the returned uuid, uniquifier, fsEndpoints count and
439                  * either the first fsEndpoint type or the volEndpoints
440                  * count if there are no fsEndpoints. */
441                 /* Fall through */
442         case 1:
443                 ret = afs_extract_data(call, true);
444                 if (ret < 0)
445                         return ret;
446
447                 bp = call->buffer + sizeof(uuid_t);
448                 uniquifier      = ntohl(*bp++);
449                 call->count     = ntohl(*bp++);
450                 call->count2    = ntohl(*bp); /* Type or next count */
451
452                 if (call->count > YFS_MAXENDPOINTS)
453                         return afs_protocol_error(call, -EBADMSG,
454                                                   afs_eproto_yvl_fsendpt_num);
455
456                 alist = afs_alloc_addrlist(call->count, FS_SERVICE, AFS_FS_PORT);
457                 if (!alist)
458                         return -ENOMEM;
459                 alist->version = uniquifier;
460                 call->reply[0] = alist;
461
462                 if (call->count == 0)
463                         goto extract_volendpoints;
464
465         next_fsendpoint:
466                 switch (call->count2) {
467                 case YFS_ENDPOINT_IPV4:
468                         size = sizeof(__be32) * (1 + 1 + 1);
469                         break;
470                 case YFS_ENDPOINT_IPV6:
471                         size = sizeof(__be32) * (1 + 4 + 1);
472                         break;
473                 default:
474                         return afs_protocol_error(call, -EBADMSG,
475                                                   afs_eproto_yvl_fsendpt_type);
476                 }
477
478                 size += sizeof(__be32);
479                 afs_extract_to_buf(call, size);
480                 call->unmarshall = 2;
481
482                 /* Fall through - and extract fsEndpoints[] entries */
483         case 2:
484                 ret = afs_extract_data(call, true);
485                 if (ret < 0)
486                         return ret;
487
488                 alist = call->reply[0];
489                 bp = call->buffer;
490                 switch (call->count2) {
491                 case YFS_ENDPOINT_IPV4:
492                         if (ntohl(bp[0]) != sizeof(__be32) * 2)
493                                 return afs_protocol_error(call, -EBADMSG,
494                                                           afs_eproto_yvl_fsendpt4_len);
495                         afs_merge_fs_addr4(alist, bp[1], ntohl(bp[2]));
496                         bp += 3;
497                         break;
498                 case YFS_ENDPOINT_IPV6:
499                         if (ntohl(bp[0]) != sizeof(__be32) * 5)
500                                 return afs_protocol_error(call, -EBADMSG,
501                                                           afs_eproto_yvl_fsendpt6_len);
502                         afs_merge_fs_addr6(alist, bp + 1, ntohl(bp[5]));
503                         bp += 6;
504                         break;
505                 default:
506                         return afs_protocol_error(call, -EBADMSG,
507                                                   afs_eproto_yvl_fsendpt_type);
508                 }
509
510                 /* Got either the type of the next entry or the count of
511                  * volEndpoints if no more fsEndpoints.
512                  */
513                 call->count2 = ntohl(*bp++);
514
515                 call->count--;
516                 if (call->count > 0)
517                         goto next_fsendpoint;
518
519         extract_volendpoints:
520                 /* Extract the list of volEndpoints. */
521                 call->count = call->count2;
522                 if (!call->count)
523                         goto end;
524                 if (call->count > YFS_MAXENDPOINTS)
525                         return afs_protocol_error(call, -EBADMSG,
526                                                   afs_eproto_yvl_vlendpt_type);
527
528                 afs_extract_to_buf(call, 1 * sizeof(__be32));
529                 call->unmarshall = 3;
530
531                 /* Extract the type of volEndpoints[0].  Normally we would
532                  * extract the type of the next endpoint when we extract the
533                  * data of the current one, but this is the first...
534                  */
535                 /* Fall through */
536         case 3:
537                 ret = afs_extract_data(call, true);
538                 if (ret < 0)
539                         return ret;
540
541                 bp = call->buffer;
542
543         next_volendpoint:
544                 call->count2 = ntohl(*bp++);
545                 switch (call->count2) {
546                 case YFS_ENDPOINT_IPV4:
547                         size = sizeof(__be32) * (1 + 1 + 1);
548                         break;
549                 case YFS_ENDPOINT_IPV6:
550                         size = sizeof(__be32) * (1 + 4 + 1);
551                         break;
552                 default:
553                         return afs_protocol_error(call, -EBADMSG,
554                                                   afs_eproto_yvl_vlendpt_type);
555                 }
556
557                 if (call->count > 1)
558                         size += sizeof(__be32); /* Get next type too */
559                 afs_extract_to_buf(call, size);
560                 call->unmarshall = 4;
561
562                 /* Fall through - and extract volEndpoints[] entries */
563         case 4:
564                 ret = afs_extract_data(call, true);
565                 if (ret < 0)
566                         return ret;
567
568                 bp = call->buffer;
569                 switch (call->count2) {
570                 case YFS_ENDPOINT_IPV4:
571                         if (ntohl(bp[0]) != sizeof(__be32) * 2)
572                                 return afs_protocol_error(call, -EBADMSG,
573                                                           afs_eproto_yvl_vlendpt4_len);
574                         bp += 3;
575                         break;
576                 case YFS_ENDPOINT_IPV6:
577                         if (ntohl(bp[0]) != sizeof(__be32) * 5)
578                                 return afs_protocol_error(call, -EBADMSG,
579                                                           afs_eproto_yvl_vlendpt6_len);
580                         bp += 6;
581                         break;
582                 default:
583                         return afs_protocol_error(call, -EBADMSG,
584                                                   afs_eproto_yvl_vlendpt_type);
585                 }
586
587                 /* Got either the type of the next entry or the count of
588                  * volEndpoints if no more fsEndpoints.
589                  */
590                 call->count--;
591                 if (call->count > 0)
592                         goto next_volendpoint;
593
594         end:
595                 afs_extract_discard(call, 0);
596                 call->unmarshall = 5;
597
598                 /* Fall through - Done */
599         case 5:
600                 ret = afs_extract_data(call, false);
601                 if (ret < 0)
602                         return ret;
603                 call->unmarshall = 6;
604
605         case 6:
606                 break;
607         }
608
609         alist = call->reply[0];
610         _leave(" = 0 [done]");
611         return 0;
612 }
613
614 /*
615  * YFSVL.GetEndpoints operation type.
616  */
617 static const struct afs_call_type afs_YFSVLGetEndpoints = {
618         .name           = "YFSVL.GetEndpoints",
619         .op             = afs_YFSVL_GetEndpoints,
620         .deliver        = afs_deliver_yfsvl_get_endpoints,
621         .destructor     = afs_vl_get_addrs_u_destructor,
622 };
623
624 /*
625  * Dispatch an operation to get the addresses for a server, where the server is
626  * nominated by UUID.
627  */
628 struct afs_addr_list *afs_yfsvl_get_endpoints(struct afs_vl_cursor *vc,
629                                               const uuid_t *uuid)
630 {
631         struct afs_call *call;
632         struct afs_net *net = vc->cell->net;
633         __be32 *bp;
634
635         _enter("");
636
637         call = afs_alloc_flat_call(net, &afs_YFSVLGetEndpoints,
638                                    sizeof(__be32) * 2 + sizeof(*uuid),
639                                    sizeof(struct in6_addr) + sizeof(__be32) * 3);
640         if (!call)
641                 return ERR_PTR(-ENOMEM);
642
643         call->key = vc->key;
644         call->reply[0] = NULL;
645         call->ret_reply0 = true;
646
647         /* Marshall the parameters */
648         bp = call->request;
649         *bp++ = htonl(YVLGETENDPOINTS);
650         *bp++ = htonl(YFS_SERVER_UUID);
651         memcpy(bp, uuid, sizeof(*uuid)); /* Type opr_uuid */
652
653         trace_afs_make_vl_call(call);
654         return (struct afs_addr_list *)afs_make_call(&vc->ac, call, GFP_KERNEL, false);
655 }