1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS Volume Location Service client
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
9 #include <linux/init.h>
10 #include <linux/sched.h>
15 * Deliver reply data to a VL.GetEntryByNameU call.
17 static int afs_deliver_vl_get_entry_by_name_u(struct afs_call
*call
)
19 struct afs_uvldbentry__xdr
*uvldb
;
20 struct afs_vldb_entry
*entry
;
21 bool new_only
= false;
22 u32 tmp
, nr_servers
, vlflags
;
27 ret
= afs_transfer_reply(call
);
31 /* unmarshall the reply once we've received all of it */
33 entry
= call
->ret_vldb
;
35 nr_servers
= ntohl(uvldb
->nServers
);
36 if (nr_servers
> AFS_NMAXNSERVERS
)
37 nr_servers
= AFS_NMAXNSERVERS
;
39 for (i
= 0; i
< ARRAY_SIZE(uvldb
->name
) - 1; i
++)
40 entry
->name
[i
] = (u8
)ntohl(uvldb
->name
[i
]);
42 entry
->name_len
= strlen(entry
->name
);
44 /* If there is a new replication site that we can use, ignore all the
45 * sites that aren't marked as new.
47 for (i
= 0; i
< nr_servers
; i
++) {
48 tmp
= ntohl(uvldb
->serverFlags
[i
]);
49 if (!(tmp
& AFS_VLSF_DONTUSE
) &&
50 (tmp
& AFS_VLSF_NEWREPSITE
))
54 vlflags
= ntohl(uvldb
->flags
);
55 for (i
= 0; i
< nr_servers
; i
++) {
56 struct afs_uuid__xdr
*xdr
;
57 struct afs_uuid
*uuid
;
59 int n
= entry
->nr_servers
;
61 tmp
= ntohl(uvldb
->serverFlags
[i
]);
62 if (tmp
& AFS_VLSF_DONTUSE
||
63 (new_only
&& !(tmp
& AFS_VLSF_NEWREPSITE
)))
65 if (tmp
& AFS_VLSF_RWVOL
) {
66 entry
->fs_mask
[n
] |= AFS_VOL_VTM_RW
;
67 if (vlflags
& AFS_VLF_BACKEXISTS
)
68 entry
->fs_mask
[n
] |= AFS_VOL_VTM_BAK
;
70 if (tmp
& AFS_VLSF_ROVOL
)
71 entry
->fs_mask
[n
] |= AFS_VOL_VTM_RO
;
72 if (!entry
->fs_mask
[n
])
75 xdr
= &uvldb
->serverNumber
[i
];
76 uuid
= (struct afs_uuid
*)&entry
->fs_server
[n
];
77 uuid
->time_low
= xdr
->time_low
;
78 uuid
->time_mid
= htons(ntohl(xdr
->time_mid
));
79 uuid
->time_hi_and_version
= htons(ntohl(xdr
->time_hi_and_version
));
80 uuid
->clock_seq_hi_and_reserved
= (u8
)ntohl(xdr
->clock_seq_hi_and_reserved
);
81 uuid
->clock_seq_low
= (u8
)ntohl(xdr
->clock_seq_low
);
82 for (j
= 0; j
< 6; j
++)
83 uuid
->node
[j
] = (u8
)ntohl(xdr
->node
[j
]);
88 for (i
= 0; i
< AFS_MAXTYPES
; i
++)
89 entry
->vid
[i
] = ntohl(uvldb
->volumeId
[i
]);
91 if (vlflags
& AFS_VLF_RWEXISTS
)
92 __set_bit(AFS_VLDB_HAS_RW
, &entry
->flags
);
93 if (vlflags
& AFS_VLF_ROEXISTS
)
94 __set_bit(AFS_VLDB_HAS_RO
, &entry
->flags
);
95 if (vlflags
& AFS_VLF_BACKEXISTS
)
96 __set_bit(AFS_VLDB_HAS_BAK
, &entry
->flags
);
98 if (!(vlflags
& (AFS_VLF_RWEXISTS
| AFS_VLF_ROEXISTS
| AFS_VLF_BACKEXISTS
))) {
99 entry
->error
= -ENOMEDIUM
;
100 __set_bit(AFS_VLDB_QUERY_ERROR
, &entry
->flags
);
103 __set_bit(AFS_VLDB_QUERY_VALID
, &entry
->flags
);
104 _leave(" = 0 [done]");
108 static void afs_destroy_vl_get_entry_by_name_u(struct afs_call
*call
)
110 kfree(call
->ret_vldb
);
111 afs_flat_call_destructor(call
);
115 * VL.GetEntryByNameU operation type.
117 static const struct afs_call_type afs_RXVLGetEntryByNameU
= {
118 .name
= "VL.GetEntryByNameU",
119 .op
= afs_VL_GetEntryByNameU
,
120 .deliver
= afs_deliver_vl_get_entry_by_name_u
,
121 .destructor
= afs_destroy_vl_get_entry_by_name_u
,
125 * Dispatch a get volume entry by name or ID operation (uuid variant). If the
126 * volname is a decimal number then it's a volume ID not a volume name.
128 struct afs_vldb_entry
*afs_vl_get_entry_by_name_u(struct afs_vl_cursor
*vc
,
132 struct afs_vldb_entry
*entry
;
133 struct afs_call
*call
;
134 struct afs_net
*net
= vc
->cell
->net
;
140 padsz
= (4 - (volnamesz
& 3)) & 3;
141 reqsz
= 8 + volnamesz
+ padsz
;
143 entry
= kzalloc(sizeof(struct afs_vldb_entry
), GFP_KERNEL
);
145 return ERR_PTR(-ENOMEM
);
147 call
= afs_alloc_flat_call(net
, &afs_RXVLGetEntryByNameU
, reqsz
,
148 sizeof(struct afs_uvldbentry__xdr
));
151 return ERR_PTR(-ENOMEM
);
155 call
->ret_vldb
= entry
;
156 call
->max_lifespan
= AFS_VL_MAX_LIFESPAN
;
158 /* Marshall the parameters */
160 *bp
++ = htonl(VLGETENTRYBYNAMEU
);
161 *bp
++ = htonl(volnamesz
);
162 memcpy(bp
, volname
, volnamesz
);
164 memset((void *)bp
+ volnamesz
, 0, padsz
);
166 trace_afs_make_vl_call(call
);
167 afs_make_call(&vc
->ac
, call
, GFP_KERNEL
);
168 return (struct afs_vldb_entry
*)afs_wait_for_call_to_complete(call
, &vc
->ac
);
172 * Deliver reply data to a VL.GetAddrsU call.
174 * GetAddrsU(IN ListAddrByAttributes *inaddr,
175 * OUT afsUUID *uuidp1,
176 * OUT uint32_t *uniquifier,
177 * OUT uint32_t *nentries,
178 * OUT bulkaddrs *blkaddrs);
180 static int afs_deliver_vl_get_addrs_u(struct afs_call
*call
)
182 struct afs_addr_list
*alist
;
184 u32 uniquifier
, nentries
, count
;
187 _enter("{%u,%zu/%u}",
188 call
->unmarshall
, iov_iter_count(call
->_iter
), call
->count
);
190 switch (call
->unmarshall
) {
192 afs_extract_to_buf(call
,
193 sizeof(struct afs_uuid__xdr
) + 3 * sizeof(__be32
));
196 /* Extract the returned uuid, uniquifier, nentries and
200 ret
= afs_extract_data(call
, true);
204 bp
= call
->buffer
+ sizeof(struct afs_uuid__xdr
);
205 uniquifier
= ntohl(*bp
++);
206 nentries
= ntohl(*bp
++);
209 nentries
= min(nentries
, count
);
210 alist
= afs_alloc_addrlist(nentries
, FS_SERVICE
, AFS_FS_PORT
);
213 alist
->version
= uniquifier
;
214 call
->ret_alist
= alist
;
216 call
->count2
= nentries
;
220 count
= min(call
->count
, 4U);
221 afs_extract_to_buf(call
, count
* sizeof(__be32
));
223 /* Fall through - and extract entries */
225 ret
= afs_extract_data(call
, call
->count
> 4);
229 alist
= call
->ret_alist
;
231 count
= min(call
->count
, 4U);
232 for (i
= 0; i
< count
; i
++)
233 if (alist
->nr_addrs
< call
->count2
)
234 afs_merge_fs_addr4(alist
, *bp
++, AFS_FS_PORT
);
236 call
->count
-= count
;
243 _leave(" = 0 [done]");
247 static void afs_vl_get_addrs_u_destructor(struct afs_call
*call
)
249 afs_put_addrlist(call
->ret_alist
);
250 return afs_flat_call_destructor(call
);
254 * VL.GetAddrsU operation type.
256 static const struct afs_call_type afs_RXVLGetAddrsU
= {
257 .name
= "VL.GetAddrsU",
258 .op
= afs_VL_GetAddrsU
,
259 .deliver
= afs_deliver_vl_get_addrs_u
,
260 .destructor
= afs_vl_get_addrs_u_destructor
,
264 * Dispatch an operation to get the addresses for a server, where the server is
267 struct afs_addr_list
*afs_vl_get_addrs_u(struct afs_vl_cursor
*vc
,
270 struct afs_ListAddrByAttributes__xdr
*r
;
271 const struct afs_uuid
*u
= (const struct afs_uuid
*)uuid
;
272 struct afs_call
*call
;
273 struct afs_net
*net
= vc
->cell
->net
;
279 call
= afs_alloc_flat_call(net
, &afs_RXVLGetAddrsU
,
280 sizeof(__be32
) + sizeof(struct afs_ListAddrByAttributes__xdr
),
281 sizeof(struct afs_uuid__xdr
) + 3 * sizeof(__be32
));
283 return ERR_PTR(-ENOMEM
);
286 call
->ret_alist
= NULL
;
287 call
->max_lifespan
= AFS_VL_MAX_LIFESPAN
;
289 /* Marshall the parameters */
291 *bp
++ = htonl(VLGETADDRSU
);
292 r
= (struct afs_ListAddrByAttributes__xdr
*)bp
;
293 r
->Mask
= htonl(AFS_VLADDR_UUID
);
297 r
->uuid
.time_low
= u
->time_low
;
298 r
->uuid
.time_mid
= htonl(ntohs(u
->time_mid
));
299 r
->uuid
.time_hi_and_version
= htonl(ntohs(u
->time_hi_and_version
));
300 r
->uuid
.clock_seq_hi_and_reserved
= htonl(u
->clock_seq_hi_and_reserved
);
301 r
->uuid
.clock_seq_low
= htonl(u
->clock_seq_low
);
302 for (i
= 0; i
< 6; i
++)
303 r
->uuid
.node
[i
] = htonl(u
->node
[i
]);
305 trace_afs_make_vl_call(call
);
306 afs_make_call(&vc
->ac
, call
, GFP_KERNEL
);
307 return (struct afs_addr_list
*)afs_wait_for_call_to_complete(call
, &vc
->ac
);
311 * Deliver reply data to an VL.GetCapabilities operation.
313 static int afs_deliver_vl_get_capabilities(struct afs_call
*call
)
318 _enter("{%u,%zu/%u}",
319 call
->unmarshall
, iov_iter_count(call
->_iter
), call
->count
);
321 switch (call
->unmarshall
) {
323 afs_extract_to_tmp(call
);
326 /* Fall through - and extract the capabilities word count */
328 ret
= afs_extract_data(call
, true);
332 count
= ntohl(call
->tmp
);
334 call
->count2
= count
;
337 afs_extract_discard(call
, count
* sizeof(__be32
));
339 /* Fall through - and extract capabilities words */
341 ret
= afs_extract_data(call
, false);
345 /* TODO: Examine capabilities */
351 _leave(" = 0 [done]");
355 static void afs_destroy_vl_get_capabilities(struct afs_call
*call
)
357 afs_put_vlserver(call
->net
, call
->vlserver
);
358 afs_flat_call_destructor(call
);
362 * VL.GetCapabilities operation type
364 static const struct afs_call_type afs_RXVLGetCapabilities
= {
365 .name
= "VL.GetCapabilities",
366 .op
= afs_VL_GetCapabilities
,
367 .deliver
= afs_deliver_vl_get_capabilities
,
368 .done
= afs_vlserver_probe_result
,
369 .destructor
= afs_destroy_vl_get_capabilities
,
373 * Probe a volume server for the capabilities that it supports. This can
374 * return up to 196 words.
376 * We use this to probe for service upgrade to determine what the server at the
377 * other end supports.
379 struct afs_call
*afs_vl_get_capabilities(struct afs_net
*net
,
380 struct afs_addr_cursor
*ac
,
382 struct afs_vlserver
*server
,
383 unsigned int server_index
)
385 struct afs_call
*call
;
390 call
= afs_alloc_flat_call(net
, &afs_RXVLGetCapabilities
, 1 * 4, 16 * 4);
392 return ERR_PTR(-ENOMEM
);
395 call
->vlserver
= afs_get_vlserver(server
);
396 call
->server_index
= server_index
;
397 call
->upgrade
= true;
399 call
->max_lifespan
= AFS_PROBE_MAX_LIFESPAN
;
401 /* marshall the parameters */
403 *bp
++ = htonl(VLGETCAPABILITIES
);
405 /* Can't take a ref on server */
406 trace_afs_make_vl_call(call
);
407 afs_make_call(ac
, call
, GFP_KERNEL
);
412 * Deliver reply data to a YFSVL.GetEndpoints call.
414 * GetEndpoints(IN yfsServerAttributes *attr,
415 * OUT opr_uuid *uuid,
416 * OUT afs_int32 *uniquifier,
417 * OUT endpoints *fsEndpoints,
418 * OUT endpoints *volEndpoints)
420 static int afs_deliver_yfsvl_get_endpoints(struct afs_call
*call
)
422 struct afs_addr_list
*alist
;
424 u32 uniquifier
, size
;
427 _enter("{%u,%zu,%u}",
428 call
->unmarshall
, iov_iter_count(call
->_iter
), call
->count2
);
430 switch (call
->unmarshall
) {
432 afs_extract_to_buf(call
, sizeof(uuid_t
) + 3 * sizeof(__be32
));
433 call
->unmarshall
= 1;
435 /* Extract the returned uuid, uniquifier, fsEndpoints count and
436 * either the first fsEndpoint type or the volEndpoints
437 * count if there are no fsEndpoints. */
440 ret
= afs_extract_data(call
, true);
444 bp
= call
->buffer
+ sizeof(uuid_t
);
445 uniquifier
= ntohl(*bp
++);
446 call
->count
= ntohl(*bp
++);
447 call
->count2
= ntohl(*bp
); /* Type or next count */
449 if (call
->count
> YFS_MAXENDPOINTS
)
450 return afs_protocol_error(call
, -EBADMSG
,
451 afs_eproto_yvl_fsendpt_num
);
453 alist
= afs_alloc_addrlist(call
->count
, FS_SERVICE
, AFS_FS_PORT
);
456 alist
->version
= uniquifier
;
457 call
->ret_alist
= alist
;
459 if (call
->count
== 0)
460 goto extract_volendpoints
;
463 switch (call
->count2
) {
464 case YFS_ENDPOINT_IPV4
:
465 size
= sizeof(__be32
) * (1 + 1 + 1);
467 case YFS_ENDPOINT_IPV6
:
468 size
= sizeof(__be32
) * (1 + 4 + 1);
471 return afs_protocol_error(call
, -EBADMSG
,
472 afs_eproto_yvl_fsendpt_type
);
475 size
+= sizeof(__be32
);
476 afs_extract_to_buf(call
, size
);
477 call
->unmarshall
= 2;
479 /* Fall through - and extract fsEndpoints[] entries */
481 ret
= afs_extract_data(call
, true);
485 alist
= call
->ret_alist
;
487 switch (call
->count2
) {
488 case YFS_ENDPOINT_IPV4
:
489 if (ntohl(bp
[0]) != sizeof(__be32
) * 2)
490 return afs_protocol_error(call
, -EBADMSG
,
491 afs_eproto_yvl_fsendpt4_len
);
492 afs_merge_fs_addr4(alist
, bp
[1], ntohl(bp
[2]));
495 case YFS_ENDPOINT_IPV6
:
496 if (ntohl(bp
[0]) != sizeof(__be32
) * 5)
497 return afs_protocol_error(call
, -EBADMSG
,
498 afs_eproto_yvl_fsendpt6_len
);
499 afs_merge_fs_addr6(alist
, bp
+ 1, ntohl(bp
[5]));
503 return afs_protocol_error(call
, -EBADMSG
,
504 afs_eproto_yvl_fsendpt_type
);
507 /* Got either the type of the next entry or the count of
508 * volEndpoints if no more fsEndpoints.
510 call
->count2
= ntohl(*bp
++);
514 goto next_fsendpoint
;
516 extract_volendpoints
:
517 /* Extract the list of volEndpoints. */
518 call
->count
= call
->count2
;
521 if (call
->count
> YFS_MAXENDPOINTS
)
522 return afs_protocol_error(call
, -EBADMSG
,
523 afs_eproto_yvl_vlendpt_type
);
525 afs_extract_to_buf(call
, 1 * sizeof(__be32
));
526 call
->unmarshall
= 3;
528 /* Extract the type of volEndpoints[0]. Normally we would
529 * extract the type of the next endpoint when we extract the
530 * data of the current one, but this is the first...
534 ret
= afs_extract_data(call
, true);
541 call
->count2
= ntohl(*bp
++);
542 switch (call
->count2
) {
543 case YFS_ENDPOINT_IPV4
:
544 size
= sizeof(__be32
) * (1 + 1 + 1);
546 case YFS_ENDPOINT_IPV6
:
547 size
= sizeof(__be32
) * (1 + 4 + 1);
550 return afs_protocol_error(call
, -EBADMSG
,
551 afs_eproto_yvl_vlendpt_type
);
555 size
+= sizeof(__be32
); /* Get next type too */
556 afs_extract_to_buf(call
, size
);
557 call
->unmarshall
= 4;
559 /* Fall through - and extract volEndpoints[] entries */
561 ret
= afs_extract_data(call
, true);
566 switch (call
->count2
) {
567 case YFS_ENDPOINT_IPV4
:
568 if (ntohl(bp
[0]) != sizeof(__be32
) * 2)
569 return afs_protocol_error(call
, -EBADMSG
,
570 afs_eproto_yvl_vlendpt4_len
);
573 case YFS_ENDPOINT_IPV6
:
574 if (ntohl(bp
[0]) != sizeof(__be32
) * 5)
575 return afs_protocol_error(call
, -EBADMSG
,
576 afs_eproto_yvl_vlendpt6_len
);
580 return afs_protocol_error(call
, -EBADMSG
,
581 afs_eproto_yvl_vlendpt_type
);
584 /* Got either the type of the next entry or the count of
585 * volEndpoints if no more fsEndpoints.
589 goto next_volendpoint
;
592 afs_extract_discard(call
, 0);
593 call
->unmarshall
= 5;
595 /* Fall through - Done */
597 ret
= afs_extract_data(call
, false);
600 call
->unmarshall
= 6;
606 _leave(" = 0 [done]");
611 * YFSVL.GetEndpoints operation type.
613 static const struct afs_call_type afs_YFSVLGetEndpoints
= {
614 .name
= "YFSVL.GetEndpoints",
615 .op
= afs_YFSVL_GetEndpoints
,
616 .deliver
= afs_deliver_yfsvl_get_endpoints
,
617 .destructor
= afs_vl_get_addrs_u_destructor
,
621 * Dispatch an operation to get the addresses for a server, where the server is
624 struct afs_addr_list
*afs_yfsvl_get_endpoints(struct afs_vl_cursor
*vc
,
627 struct afs_call
*call
;
628 struct afs_net
*net
= vc
->cell
->net
;
633 call
= afs_alloc_flat_call(net
, &afs_YFSVLGetEndpoints
,
634 sizeof(__be32
) * 2 + sizeof(*uuid
),
635 sizeof(struct in6_addr
) + sizeof(__be32
) * 3);
637 return ERR_PTR(-ENOMEM
);
640 call
->ret_alist
= NULL
;
641 call
->max_lifespan
= AFS_VL_MAX_LIFESPAN
;
643 /* Marshall the parameters */
645 *bp
++ = htonl(YVLGETENDPOINTS
);
646 *bp
++ = htonl(YFS_SERVER_UUID
);
647 memcpy(bp
, uuid
, sizeof(*uuid
)); /* Type opr_uuid */
649 trace_afs_make_vl_call(call
);
650 afs_make_call(&vc
->ac
, call
, GFP_KERNEL
);
651 return (struct afs_addr_list
*)afs_wait_for_call_to_complete(call
, &vc
->ac
);