afs: Fix the maximum lifespan of VL and probe calls
[linux-2.6-microblaze.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         call->max_lifespan = AFS_VL_MAX_LIFESPAN;
161
162         /* Marshall the parameters */
163         bp = call->request;
164         *bp++ = htonl(VLGETENTRYBYNAMEU);
165         *bp++ = htonl(volnamesz);
166         memcpy(bp, volname, volnamesz);
167         if (padsz > 0)
168                 memset((void *)bp + volnamesz, 0, padsz);
169
170         trace_afs_make_vl_call(call);
171         afs_make_call(&vc->ac, call, GFP_KERNEL);
172         return (struct afs_vldb_entry *)afs_wait_for_call_to_complete(call, &vc->ac);
173 }
174
175 /*
176  * Deliver reply data to a VL.GetAddrsU call.
177  *
178  *      GetAddrsU(IN ListAddrByAttributes *inaddr,
179  *                OUT afsUUID *uuidp1,
180  *                OUT uint32_t *uniquifier,
181  *                OUT uint32_t *nentries,
182  *                OUT bulkaddrs *blkaddrs);
183  */
184 static int afs_deliver_vl_get_addrs_u(struct afs_call *call)
185 {
186         struct afs_addr_list *alist;
187         __be32 *bp;
188         u32 uniquifier, nentries, count;
189         int i, ret;
190
191         _enter("{%u,%zu/%u}",
192                call->unmarshall, iov_iter_count(call->_iter), call->count);
193
194         switch (call->unmarshall) {
195         case 0:
196                 afs_extract_to_buf(call,
197                                    sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
198                 call->unmarshall++;
199
200                 /* Extract the returned uuid, uniquifier, nentries and
201                  * blkaddrs size */
202                 /* Fall through */
203         case 1:
204                 ret = afs_extract_data(call, true);
205                 if (ret < 0)
206                         return ret;
207
208                 bp = call->buffer + sizeof(struct afs_uuid__xdr);
209                 uniquifier      = ntohl(*bp++);
210                 nentries        = ntohl(*bp++);
211                 count           = ntohl(*bp);
212
213                 nentries = min(nentries, count);
214                 alist = afs_alloc_addrlist(nentries, FS_SERVICE, AFS_FS_PORT);
215                 if (!alist)
216                         return -ENOMEM;
217                 alist->version = uniquifier;
218                 call->reply[0] = alist;
219                 call->count = count;
220                 call->count2 = nentries;
221                 call->unmarshall++;
222
223         more_entries:
224                 count = min(call->count, 4U);
225                 afs_extract_to_buf(call, count * sizeof(__be32));
226
227                 /* Fall through - and extract entries */
228         case 2:
229                 ret = afs_extract_data(call, call->count > 4);
230                 if (ret < 0)
231                         return ret;
232
233                 alist = call->reply[0];
234                 bp = call->buffer;
235                 count = min(call->count, 4U);
236                 for (i = 0; i < count; i++)
237                         if (alist->nr_addrs < call->count2)
238                                 afs_merge_fs_addr4(alist, *bp++, AFS_FS_PORT);
239
240                 call->count -= count;
241                 if (call->count > 0)
242                         goto more_entries;
243                 call->unmarshall++;
244                 break;
245         }
246
247         _leave(" = 0 [done]");
248         return 0;
249 }
250
251 static void afs_vl_get_addrs_u_destructor(struct afs_call *call)
252 {
253         afs_put_server(call->net, (struct afs_server *)call->reply[0]);
254         kfree(call->reply[1]);
255         return afs_flat_call_destructor(call);
256 }
257
258 /*
259  * VL.GetAddrsU operation type.
260  */
261 static const struct afs_call_type afs_RXVLGetAddrsU = {
262         .name           = "VL.GetAddrsU",
263         .op             = afs_VL_GetAddrsU,
264         .deliver        = afs_deliver_vl_get_addrs_u,
265         .destructor     = afs_vl_get_addrs_u_destructor,
266 };
267
268 /*
269  * Dispatch an operation to get the addresses for a server, where the server is
270  * nominated by UUID.
271  */
272 struct afs_addr_list *afs_vl_get_addrs_u(struct afs_vl_cursor *vc,
273                                          const uuid_t *uuid)
274 {
275         struct afs_ListAddrByAttributes__xdr *r;
276         const struct afs_uuid *u = (const struct afs_uuid *)uuid;
277         struct afs_call *call;
278         struct afs_net *net = vc->cell->net;
279         __be32 *bp;
280         int i;
281
282         _enter("");
283
284         call = afs_alloc_flat_call(net, &afs_RXVLGetAddrsU,
285                                    sizeof(__be32) + sizeof(struct afs_ListAddrByAttributes__xdr),
286                                    sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
287         if (!call)
288                 return ERR_PTR(-ENOMEM);
289
290         call->key = vc->key;
291         call->reply[0] = NULL;
292         call->ret_reply0 = true;
293         call->max_lifespan = AFS_VL_MAX_LIFESPAN;
294
295         /* Marshall the parameters */
296         bp = call->request;
297         *bp++ = htonl(VLGETADDRSU);
298         r = (struct afs_ListAddrByAttributes__xdr *)bp;
299         r->Mask         = htonl(AFS_VLADDR_UUID);
300         r->ipaddr       = 0;
301         r->index        = 0;
302         r->spare        = 0;
303         r->uuid.time_low                        = u->time_low;
304         r->uuid.time_mid                        = htonl(ntohs(u->time_mid));
305         r->uuid.time_hi_and_version             = htonl(ntohs(u->time_hi_and_version));
306         r->uuid.clock_seq_hi_and_reserved       = htonl(u->clock_seq_hi_and_reserved);
307         r->uuid.clock_seq_low                   = htonl(u->clock_seq_low);
308         for (i = 0; i < 6; i++)
309                 r->uuid.node[i] = htonl(u->node[i]);
310
311         trace_afs_make_vl_call(call);
312         afs_make_call(&vc->ac, call, GFP_KERNEL);
313         return (struct afs_addr_list *)afs_wait_for_call_to_complete(call, &vc->ac);
314 }
315
316 /*
317  * Deliver reply data to an VL.GetCapabilities operation.
318  */
319 static int afs_deliver_vl_get_capabilities(struct afs_call *call)
320 {
321         u32 count;
322         int ret;
323
324         _enter("{%u,%zu/%u}",
325                call->unmarshall, iov_iter_count(call->_iter), call->count);
326
327         switch (call->unmarshall) {
328         case 0:
329                 afs_extract_to_tmp(call);
330                 call->unmarshall++;
331
332                 /* Fall through - and extract the capabilities word count */
333         case 1:
334                 ret = afs_extract_data(call, true);
335                 if (ret < 0)
336                         return ret;
337
338                 count = ntohl(call->tmp);
339                 call->count = count;
340                 call->count2 = count;
341
342                 call->unmarshall++;
343                 afs_extract_discard(call, count * sizeof(__be32));
344
345                 /* Fall through - and extract capabilities words */
346         case 2:
347                 ret = afs_extract_data(call, false);
348                 if (ret < 0)
349                         return ret;
350
351                 /* TODO: Examine capabilities */
352
353                 call->unmarshall++;
354                 break;
355         }
356
357         _leave(" = 0 [done]");
358         return 0;
359 }
360
361 static void afs_destroy_vl_get_capabilities(struct afs_call *call)
362 {
363         struct afs_vlserver *server = call->reply[0];
364
365         afs_put_vlserver(call->net, server);
366         afs_flat_call_destructor(call);
367 }
368
369 /*
370  * VL.GetCapabilities operation type
371  */
372 static const struct afs_call_type afs_RXVLGetCapabilities = {
373         .name           = "VL.GetCapabilities",
374         .op             = afs_VL_GetCapabilities,
375         .deliver        = afs_deliver_vl_get_capabilities,
376         .done           = afs_vlserver_probe_result,
377         .destructor     = afs_destroy_vl_get_capabilities,
378 };
379
380 /*
381  * Probe a volume server for the capabilities that it supports.  This can
382  * return up to 196 words.
383  *
384  * We use this to probe for service upgrade to determine what the server at the
385  * other end supports.
386  */
387 struct afs_call *afs_vl_get_capabilities(struct afs_net *net,
388                                          struct afs_addr_cursor *ac,
389                                          struct key *key,
390                                          struct afs_vlserver *server,
391                                          unsigned int server_index)
392 {
393         struct afs_call *call;
394         __be32 *bp;
395
396         _enter("");
397
398         call = afs_alloc_flat_call(net, &afs_RXVLGetCapabilities, 1 * 4, 16 * 4);
399         if (!call)
400                 return ERR_PTR(-ENOMEM);
401
402         call->key = key;
403         call->reply[0] = afs_get_vlserver(server);
404         call->reply[1] = (void *)(long)server_index;
405         call->upgrade = true;
406         call->want_reply_time = true;
407         call->async = true;
408         call->max_lifespan = AFS_PROBE_MAX_LIFESPAN;
409
410         /* marshall the parameters */
411         bp = call->request;
412         *bp++ = htonl(VLGETCAPABILITIES);
413
414         /* Can't take a ref on server */
415         trace_afs_make_vl_call(call);
416         afs_make_call(ac, call, GFP_KERNEL);
417         return call;
418 }
419
420 /*
421  * Deliver reply data to a YFSVL.GetEndpoints call.
422  *
423  *      GetEndpoints(IN yfsServerAttributes *attr,
424  *                   OUT opr_uuid *uuid,
425  *                   OUT afs_int32 *uniquifier,
426  *                   OUT endpoints *fsEndpoints,
427  *                   OUT endpoints *volEndpoints)
428  */
429 static int afs_deliver_yfsvl_get_endpoints(struct afs_call *call)
430 {
431         struct afs_addr_list *alist;
432         __be32 *bp;
433         u32 uniquifier, size;
434         int ret;
435
436         _enter("{%u,%zu,%u}",
437                call->unmarshall, iov_iter_count(call->_iter), call->count2);
438
439         switch (call->unmarshall) {
440         case 0:
441                 afs_extract_to_buf(call, sizeof(uuid_t) + 3 * sizeof(__be32));
442                 call->unmarshall = 1;
443
444                 /* Extract the returned uuid, uniquifier, fsEndpoints count and
445                  * either the first fsEndpoint type or the volEndpoints
446                  * count if there are no fsEndpoints. */
447                 /* Fall through */
448         case 1:
449                 ret = afs_extract_data(call, true);
450                 if (ret < 0)
451                         return ret;
452
453                 bp = call->buffer + sizeof(uuid_t);
454                 uniquifier      = ntohl(*bp++);
455                 call->count     = ntohl(*bp++);
456                 call->count2    = ntohl(*bp); /* Type or next count */
457
458                 if (call->count > YFS_MAXENDPOINTS)
459                         return afs_protocol_error(call, -EBADMSG,
460                                                   afs_eproto_yvl_fsendpt_num);
461
462                 alist = afs_alloc_addrlist(call->count, FS_SERVICE, AFS_FS_PORT);
463                 if (!alist)
464                         return -ENOMEM;
465                 alist->version = uniquifier;
466                 call->reply[0] = alist;
467
468                 if (call->count == 0)
469                         goto extract_volendpoints;
470
471         next_fsendpoint:
472                 switch (call->count2) {
473                 case YFS_ENDPOINT_IPV4:
474                         size = sizeof(__be32) * (1 + 1 + 1);
475                         break;
476                 case YFS_ENDPOINT_IPV6:
477                         size = sizeof(__be32) * (1 + 4 + 1);
478                         break;
479                 default:
480                         return afs_protocol_error(call, -EBADMSG,
481                                                   afs_eproto_yvl_fsendpt_type);
482                 }
483
484                 size += sizeof(__be32);
485                 afs_extract_to_buf(call, size);
486                 call->unmarshall = 2;
487
488                 /* Fall through - and extract fsEndpoints[] entries */
489         case 2:
490                 ret = afs_extract_data(call, true);
491                 if (ret < 0)
492                         return ret;
493
494                 alist = call->reply[0];
495                 bp = call->buffer;
496                 switch (call->count2) {
497                 case YFS_ENDPOINT_IPV4:
498                         if (ntohl(bp[0]) != sizeof(__be32) * 2)
499                                 return afs_protocol_error(call, -EBADMSG,
500                                                           afs_eproto_yvl_fsendpt4_len);
501                         afs_merge_fs_addr4(alist, bp[1], ntohl(bp[2]));
502                         bp += 3;
503                         break;
504                 case YFS_ENDPOINT_IPV6:
505                         if (ntohl(bp[0]) != sizeof(__be32) * 5)
506                                 return afs_protocol_error(call, -EBADMSG,
507                                                           afs_eproto_yvl_fsendpt6_len);
508                         afs_merge_fs_addr6(alist, bp + 1, ntohl(bp[5]));
509                         bp += 6;
510                         break;
511                 default:
512                         return afs_protocol_error(call, -EBADMSG,
513                                                   afs_eproto_yvl_fsendpt_type);
514                 }
515
516                 /* Got either the type of the next entry or the count of
517                  * volEndpoints if no more fsEndpoints.
518                  */
519                 call->count2 = ntohl(*bp++);
520
521                 call->count--;
522                 if (call->count > 0)
523                         goto next_fsendpoint;
524
525         extract_volendpoints:
526                 /* Extract the list of volEndpoints. */
527                 call->count = call->count2;
528                 if (!call->count)
529                         goto end;
530                 if (call->count > YFS_MAXENDPOINTS)
531                         return afs_protocol_error(call, -EBADMSG,
532                                                   afs_eproto_yvl_vlendpt_type);
533
534                 afs_extract_to_buf(call, 1 * sizeof(__be32));
535                 call->unmarshall = 3;
536
537                 /* Extract the type of volEndpoints[0].  Normally we would
538                  * extract the type of the next endpoint when we extract the
539                  * data of the current one, but this is the first...
540                  */
541                 /* Fall through */
542         case 3:
543                 ret = afs_extract_data(call, true);
544                 if (ret < 0)
545                         return ret;
546
547                 bp = call->buffer;
548
549         next_volendpoint:
550                 call->count2 = ntohl(*bp++);
551                 switch (call->count2) {
552                 case YFS_ENDPOINT_IPV4:
553                         size = sizeof(__be32) * (1 + 1 + 1);
554                         break;
555                 case YFS_ENDPOINT_IPV6:
556                         size = sizeof(__be32) * (1 + 4 + 1);
557                         break;
558                 default:
559                         return afs_protocol_error(call, -EBADMSG,
560                                                   afs_eproto_yvl_vlendpt_type);
561                 }
562
563                 if (call->count > 1)
564                         size += sizeof(__be32); /* Get next type too */
565                 afs_extract_to_buf(call, size);
566                 call->unmarshall = 4;
567
568                 /* Fall through - and extract volEndpoints[] entries */
569         case 4:
570                 ret = afs_extract_data(call, true);
571                 if (ret < 0)
572                         return ret;
573
574                 bp = call->buffer;
575                 switch (call->count2) {
576                 case YFS_ENDPOINT_IPV4:
577                         if (ntohl(bp[0]) != sizeof(__be32) * 2)
578                                 return afs_protocol_error(call, -EBADMSG,
579                                                           afs_eproto_yvl_vlendpt4_len);
580                         bp += 3;
581                         break;
582                 case YFS_ENDPOINT_IPV6:
583                         if (ntohl(bp[0]) != sizeof(__be32) * 5)
584                                 return afs_protocol_error(call, -EBADMSG,
585                                                           afs_eproto_yvl_vlendpt6_len);
586                         bp += 6;
587                         break;
588                 default:
589                         return afs_protocol_error(call, -EBADMSG,
590                                                   afs_eproto_yvl_vlendpt_type);
591                 }
592
593                 /* Got either the type of the next entry or the count of
594                  * volEndpoints if no more fsEndpoints.
595                  */
596                 call->count--;
597                 if (call->count > 0)
598                         goto next_volendpoint;
599
600         end:
601                 afs_extract_discard(call, 0);
602                 call->unmarshall = 5;
603
604                 /* Fall through - Done */
605         case 5:
606                 ret = afs_extract_data(call, false);
607                 if (ret < 0)
608                         return ret;
609                 call->unmarshall = 6;
610
611         case 6:
612                 break;
613         }
614
615         alist = call->reply[0];
616         _leave(" = 0 [done]");
617         return 0;
618 }
619
620 /*
621  * YFSVL.GetEndpoints operation type.
622  */
623 static const struct afs_call_type afs_YFSVLGetEndpoints = {
624         .name           = "YFSVL.GetEndpoints",
625         .op             = afs_YFSVL_GetEndpoints,
626         .deliver        = afs_deliver_yfsvl_get_endpoints,
627         .destructor     = afs_vl_get_addrs_u_destructor,
628 };
629
630 /*
631  * Dispatch an operation to get the addresses for a server, where the server is
632  * nominated by UUID.
633  */
634 struct afs_addr_list *afs_yfsvl_get_endpoints(struct afs_vl_cursor *vc,
635                                               const uuid_t *uuid)
636 {
637         struct afs_call *call;
638         struct afs_net *net = vc->cell->net;
639         __be32 *bp;
640
641         _enter("");
642
643         call = afs_alloc_flat_call(net, &afs_YFSVLGetEndpoints,
644                                    sizeof(__be32) * 2 + sizeof(*uuid),
645                                    sizeof(struct in6_addr) + sizeof(__be32) * 3);
646         if (!call)
647                 return ERR_PTR(-ENOMEM);
648
649         call->key = vc->key;
650         call->reply[0] = NULL;
651         call->ret_reply0 = true;
652         call->max_lifespan = AFS_VL_MAX_LIFESPAN;
653
654         /* Marshall the parameters */
655         bp = call->request;
656         *bp++ = htonl(YVLGETENDPOINTS);
657         *bp++ = htonl(YFS_SERVER_UUID);
658         memcpy(bp, uuid, sizeof(*uuid)); /* Type opr_uuid */
659
660         trace_afs_make_vl_call(call);
661         afs_make_call(&vc->ac, call, GFP_KERNEL);
662         return (struct afs_addr_list *)afs_wait_for_call_to_complete(call, &vc->ac);
663 }