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
;
60 tmp
= ntohl(uvldb
->serverFlags
[i
]);
61 if (tmp
& AFS_VLSF_DONTUSE
||
62 (new_only
&& !(tmp
& AFS_VLSF_NEWREPSITE
)))
64 if (tmp
& AFS_VLSF_RWVOL
) {
65 entry
->fs_mask
[i
] |= AFS_VOL_VTM_RW
;
66 if (vlflags
& AFS_VLF_BACKEXISTS
)
67 entry
->fs_mask
[i
] |= AFS_VOL_VTM_BAK
;
69 if (tmp
& AFS_VLSF_ROVOL
)
70 entry
->fs_mask
[i
] |= AFS_VOL_VTM_RO
;
71 if (!entry
->fs_mask
[i
])
74 xdr
= &uvldb
->serverNumber
[i
];
75 uuid
= (struct afs_uuid
*)&entry
->fs_server
[i
];
76 uuid
->time_low
= xdr
->time_low
;
77 uuid
->time_mid
= htons(ntohl(xdr
->time_mid
));
78 uuid
->time_hi_and_version
= htons(ntohl(xdr
->time_hi_and_version
));
79 uuid
->clock_seq_hi_and_reserved
= (u8
)ntohl(xdr
->clock_seq_hi_and_reserved
);
80 uuid
->clock_seq_low
= (u8
)ntohl(xdr
->clock_seq_low
);
81 for (j
= 0; j
< 6; j
++)
82 uuid
->node
[j
] = (u8
)ntohl(xdr
->node
[j
]);
87 for (i
= 0; i
< AFS_MAXTYPES
; i
++)
88 entry
->vid
[i
] = ntohl(uvldb
->volumeId
[i
]);
90 if (vlflags
& AFS_VLF_RWEXISTS
)
91 __set_bit(AFS_VLDB_HAS_RW
, &entry
->flags
);
92 if (vlflags
& AFS_VLF_ROEXISTS
)
93 __set_bit(AFS_VLDB_HAS_RO
, &entry
->flags
);
94 if (vlflags
& AFS_VLF_BACKEXISTS
)
95 __set_bit(AFS_VLDB_HAS_BAK
, &entry
->flags
);
97 if (!(vlflags
& (AFS_VLF_RWEXISTS
| AFS_VLF_ROEXISTS
| AFS_VLF_BACKEXISTS
))) {
98 entry
->error
= -ENOMEDIUM
;
99 __set_bit(AFS_VLDB_QUERY_ERROR
, &entry
->flags
);
102 __set_bit(AFS_VLDB_QUERY_VALID
, &entry
->flags
);
103 _leave(" = 0 [done]");
107 static void afs_destroy_vl_get_entry_by_name_u(struct afs_call
*call
)
109 kfree(call
->ret_vldb
);
110 afs_flat_call_destructor(call
);
114 * VL.GetEntryByNameU operation type.
116 static const struct afs_call_type afs_RXVLGetEntryByNameU
= {
117 .name
= "VL.GetEntryByNameU",
118 .op
= afs_VL_GetEntryByNameU
,
119 .deliver
= afs_deliver_vl_get_entry_by_name_u
,
120 .destructor
= afs_destroy_vl_get_entry_by_name_u
,
124 * Dispatch a get volume entry by name or ID operation (uuid variant). If the
125 * volname is a decimal number then it's a volume ID not a volume name.
127 struct afs_vldb_entry
*afs_vl_get_entry_by_name_u(struct afs_vl_cursor
*vc
,
131 struct afs_vldb_entry
*entry
;
132 struct afs_call
*call
;
133 struct afs_net
*net
= vc
->cell
->net
;
139 padsz
= (4 - (volnamesz
& 3)) & 3;
140 reqsz
= 8 + volnamesz
+ padsz
;
142 entry
= kzalloc(sizeof(struct afs_vldb_entry
), GFP_KERNEL
);
144 return ERR_PTR(-ENOMEM
);
146 call
= afs_alloc_flat_call(net
, &afs_RXVLGetEntryByNameU
, reqsz
,
147 sizeof(struct afs_uvldbentry__xdr
));
150 return ERR_PTR(-ENOMEM
);
154 call
->ret_vldb
= entry
;
155 call
->max_lifespan
= AFS_VL_MAX_LIFESPAN
;
157 /* Marshall the parameters */
159 *bp
++ = htonl(VLGETENTRYBYNAMEU
);
160 *bp
++ = htonl(volnamesz
);
161 memcpy(bp
, volname
, volnamesz
);
163 memset((void *)bp
+ volnamesz
, 0, padsz
);
165 trace_afs_make_vl_call(call
);
166 afs_make_call(&vc
->ac
, call
, GFP_KERNEL
);
167 return (struct afs_vldb_entry
*)afs_wait_for_call_to_complete(call
, &vc
->ac
);
171 * Deliver reply data to a VL.GetAddrsU call.
173 * GetAddrsU(IN ListAddrByAttributes *inaddr,
174 * OUT afsUUID *uuidp1,
175 * OUT uint32_t *uniquifier,
176 * OUT uint32_t *nentries,
177 * OUT bulkaddrs *blkaddrs);
179 static int afs_deliver_vl_get_addrs_u(struct afs_call
*call
)
181 struct afs_addr_list
*alist
;
183 u32 uniquifier
, nentries
, count
;
186 _enter("{%u,%zu/%u}",
187 call
->unmarshall
, iov_iter_count(call
->_iter
), call
->count
);
189 switch (call
->unmarshall
) {
191 afs_extract_to_buf(call
,
192 sizeof(struct afs_uuid__xdr
) + 3 * sizeof(__be32
));
195 /* Extract the returned uuid, uniquifier, nentries and
199 ret
= afs_extract_data(call
, true);
203 bp
= call
->buffer
+ sizeof(struct afs_uuid__xdr
);
204 uniquifier
= ntohl(*bp
++);
205 nentries
= ntohl(*bp
++);
208 nentries
= min(nentries
, count
);
209 alist
= afs_alloc_addrlist(nentries
, FS_SERVICE
, AFS_FS_PORT
);
212 alist
->version
= uniquifier
;
213 call
->ret_alist
= alist
;
215 call
->count2
= nentries
;
219 count
= min(call
->count
, 4U);
220 afs_extract_to_buf(call
, count
* sizeof(__be32
));
222 /* Fall through - and extract entries */
224 ret
= afs_extract_data(call
, call
->count
> 4);
228 alist
= call
->ret_alist
;
230 count
= min(call
->count
, 4U);
231 for (i
= 0; i
< count
; i
++)
232 if (alist
->nr_addrs
< call
->count2
)
233 afs_merge_fs_addr4(alist
, *bp
++, AFS_FS_PORT
);
235 call
->count
-= count
;
242 _leave(" = 0 [done]");
246 static void afs_vl_get_addrs_u_destructor(struct afs_call
*call
)
248 afs_put_addrlist(call
->ret_alist
);
249 return afs_flat_call_destructor(call
);
253 * VL.GetAddrsU operation type.
255 static const struct afs_call_type afs_RXVLGetAddrsU
= {
256 .name
= "VL.GetAddrsU",
257 .op
= afs_VL_GetAddrsU
,
258 .deliver
= afs_deliver_vl_get_addrs_u
,
259 .destructor
= afs_vl_get_addrs_u_destructor
,
263 * Dispatch an operation to get the addresses for a server, where the server is
266 struct afs_addr_list
*afs_vl_get_addrs_u(struct afs_vl_cursor
*vc
,
269 struct afs_ListAddrByAttributes__xdr
*r
;
270 const struct afs_uuid
*u
= (const struct afs_uuid
*)uuid
;
271 struct afs_call
*call
;
272 struct afs_net
*net
= vc
->cell
->net
;
278 call
= afs_alloc_flat_call(net
, &afs_RXVLGetAddrsU
,
279 sizeof(__be32
) + sizeof(struct afs_ListAddrByAttributes__xdr
),
280 sizeof(struct afs_uuid__xdr
) + 3 * sizeof(__be32
));
282 return ERR_PTR(-ENOMEM
);
285 call
->ret_alist
= NULL
;
286 call
->max_lifespan
= AFS_VL_MAX_LIFESPAN
;
288 /* Marshall the parameters */
290 *bp
++ = htonl(VLGETADDRSU
);
291 r
= (struct afs_ListAddrByAttributes__xdr
*)bp
;
292 r
->Mask
= htonl(AFS_VLADDR_UUID
);
296 r
->uuid
.time_low
= u
->time_low
;
297 r
->uuid
.time_mid
= htonl(ntohs(u
->time_mid
));
298 r
->uuid
.time_hi_and_version
= htonl(ntohs(u
->time_hi_and_version
));
299 r
->uuid
.clock_seq_hi_and_reserved
= htonl(u
->clock_seq_hi_and_reserved
);
300 r
->uuid
.clock_seq_low
= htonl(u
->clock_seq_low
);
301 for (i
= 0; i
< 6; i
++)
302 r
->uuid
.node
[i
] = htonl(u
->node
[i
]);
304 trace_afs_make_vl_call(call
);
305 afs_make_call(&vc
->ac
, call
, GFP_KERNEL
);
306 return (struct afs_addr_list
*)afs_wait_for_call_to_complete(call
, &vc
->ac
);
310 * Deliver reply data to an VL.GetCapabilities operation.
312 static int afs_deliver_vl_get_capabilities(struct afs_call
*call
)
317 _enter("{%u,%zu/%u}",
318 call
->unmarshall
, iov_iter_count(call
->_iter
), call
->count
);
320 switch (call
->unmarshall
) {
322 afs_extract_to_tmp(call
);
325 /* Fall through - and extract the capabilities word count */
327 ret
= afs_extract_data(call
, true);
331 count
= ntohl(call
->tmp
);
333 call
->count2
= count
;
336 afs_extract_discard(call
, count
* sizeof(__be32
));
338 /* Fall through - and extract capabilities words */
340 ret
= afs_extract_data(call
, false);
344 /* TODO: Examine capabilities */
350 _leave(" = 0 [done]");
354 static void afs_destroy_vl_get_capabilities(struct afs_call
*call
)
356 afs_put_vlserver(call
->net
, call
->vlserver
);
357 afs_flat_call_destructor(call
);
361 * VL.GetCapabilities operation type
363 static const struct afs_call_type afs_RXVLGetCapabilities
= {
364 .name
= "VL.GetCapabilities",
365 .op
= afs_VL_GetCapabilities
,
366 .deliver
= afs_deliver_vl_get_capabilities
,
367 .done
= afs_vlserver_probe_result
,
368 .destructor
= afs_destroy_vl_get_capabilities
,
372 * Probe a volume server for the capabilities that it supports. This can
373 * return up to 196 words.
375 * We use this to probe for service upgrade to determine what the server at the
376 * other end supports.
378 struct afs_call
*afs_vl_get_capabilities(struct afs_net
*net
,
379 struct afs_addr_cursor
*ac
,
381 struct afs_vlserver
*server
,
382 unsigned int server_index
)
384 struct afs_call
*call
;
389 call
= afs_alloc_flat_call(net
, &afs_RXVLGetCapabilities
, 1 * 4, 16 * 4);
391 return ERR_PTR(-ENOMEM
);
394 call
->vlserver
= afs_get_vlserver(server
);
395 call
->server_index
= server_index
;
396 call
->upgrade
= true;
398 call
->max_lifespan
= AFS_PROBE_MAX_LIFESPAN
;
400 /* marshall the parameters */
402 *bp
++ = htonl(VLGETCAPABILITIES
);
404 /* Can't take a ref on server */
405 trace_afs_make_vl_call(call
);
406 afs_make_call(ac
, call
, GFP_KERNEL
);
411 * Deliver reply data to a YFSVL.GetEndpoints call.
413 * GetEndpoints(IN yfsServerAttributes *attr,
414 * OUT opr_uuid *uuid,
415 * OUT afs_int32 *uniquifier,
416 * OUT endpoints *fsEndpoints,
417 * OUT endpoints *volEndpoints)
419 static int afs_deliver_yfsvl_get_endpoints(struct afs_call
*call
)
421 struct afs_addr_list
*alist
;
423 u32 uniquifier
, size
;
426 _enter("{%u,%zu,%u}",
427 call
->unmarshall
, iov_iter_count(call
->_iter
), call
->count2
);
429 switch (call
->unmarshall
) {
431 afs_extract_to_buf(call
, sizeof(uuid_t
) + 3 * sizeof(__be32
));
432 call
->unmarshall
= 1;
434 /* Extract the returned uuid, uniquifier, fsEndpoints count and
435 * either the first fsEndpoint type or the volEndpoints
436 * count if there are no fsEndpoints. */
439 ret
= afs_extract_data(call
, true);
443 bp
= call
->buffer
+ sizeof(uuid_t
);
444 uniquifier
= ntohl(*bp
++);
445 call
->count
= ntohl(*bp
++);
446 call
->count2
= ntohl(*bp
); /* Type or next count */
448 if (call
->count
> YFS_MAXENDPOINTS
)
449 return afs_protocol_error(call
, -EBADMSG
,
450 afs_eproto_yvl_fsendpt_num
);
452 alist
= afs_alloc_addrlist(call
->count
, FS_SERVICE
, AFS_FS_PORT
);
455 alist
->version
= uniquifier
;
456 call
->ret_alist
= alist
;
458 if (call
->count
== 0)
459 goto extract_volendpoints
;
462 switch (call
->count2
) {
463 case YFS_ENDPOINT_IPV4
:
464 size
= sizeof(__be32
) * (1 + 1 + 1);
466 case YFS_ENDPOINT_IPV6
:
467 size
= sizeof(__be32
) * (1 + 4 + 1);
470 return afs_protocol_error(call
, -EBADMSG
,
471 afs_eproto_yvl_fsendpt_type
);
474 size
+= sizeof(__be32
);
475 afs_extract_to_buf(call
, size
);
476 call
->unmarshall
= 2;
478 /* Fall through - and extract fsEndpoints[] entries */
480 ret
= afs_extract_data(call
, true);
484 alist
= call
->ret_alist
;
486 switch (call
->count2
) {
487 case YFS_ENDPOINT_IPV4
:
488 if (ntohl(bp
[0]) != sizeof(__be32
) * 2)
489 return afs_protocol_error(call
, -EBADMSG
,
490 afs_eproto_yvl_fsendpt4_len
);
491 afs_merge_fs_addr4(alist
, bp
[1], ntohl(bp
[2]));
494 case YFS_ENDPOINT_IPV6
:
495 if (ntohl(bp
[0]) != sizeof(__be32
) * 5)
496 return afs_protocol_error(call
, -EBADMSG
,
497 afs_eproto_yvl_fsendpt6_len
);
498 afs_merge_fs_addr6(alist
, bp
+ 1, ntohl(bp
[5]));
502 return afs_protocol_error(call
, -EBADMSG
,
503 afs_eproto_yvl_fsendpt_type
);
506 /* Got either the type of the next entry or the count of
507 * volEndpoints if no more fsEndpoints.
509 call
->count2
= ntohl(*bp
++);
513 goto next_fsendpoint
;
515 extract_volendpoints
:
516 /* Extract the list of volEndpoints. */
517 call
->count
= call
->count2
;
520 if (call
->count
> YFS_MAXENDPOINTS
)
521 return afs_protocol_error(call
, -EBADMSG
,
522 afs_eproto_yvl_vlendpt_type
);
524 afs_extract_to_buf(call
, 1 * sizeof(__be32
));
525 call
->unmarshall
= 3;
527 /* Extract the type of volEndpoints[0]. Normally we would
528 * extract the type of the next endpoint when we extract the
529 * data of the current one, but this is the first...
533 ret
= afs_extract_data(call
, true);
540 call
->count2
= ntohl(*bp
++);
541 switch (call
->count2
) {
542 case YFS_ENDPOINT_IPV4
:
543 size
= sizeof(__be32
) * (1 + 1 + 1);
545 case YFS_ENDPOINT_IPV6
:
546 size
= sizeof(__be32
) * (1 + 4 + 1);
549 return afs_protocol_error(call
, -EBADMSG
,
550 afs_eproto_yvl_vlendpt_type
);
554 size
+= sizeof(__be32
); /* Get next type too */
555 afs_extract_to_buf(call
, size
);
556 call
->unmarshall
= 4;
558 /* Fall through - and extract volEndpoints[] entries */
560 ret
= afs_extract_data(call
, true);
565 switch (call
->count2
) {
566 case YFS_ENDPOINT_IPV4
:
567 if (ntohl(bp
[0]) != sizeof(__be32
) * 2)
568 return afs_protocol_error(call
, -EBADMSG
,
569 afs_eproto_yvl_vlendpt4_len
);
572 case YFS_ENDPOINT_IPV6
:
573 if (ntohl(bp
[0]) != sizeof(__be32
) * 5)
574 return afs_protocol_error(call
, -EBADMSG
,
575 afs_eproto_yvl_vlendpt6_len
);
579 return afs_protocol_error(call
, -EBADMSG
,
580 afs_eproto_yvl_vlendpt_type
);
583 /* Got either the type of the next entry or the count of
584 * volEndpoints if no more fsEndpoints.
588 goto next_volendpoint
;
591 afs_extract_discard(call
, 0);
592 call
->unmarshall
= 5;
594 /* Fall through - Done */
596 ret
= afs_extract_data(call
, false);
599 call
->unmarshall
= 6;
605 _leave(" = 0 [done]");
610 * YFSVL.GetEndpoints operation type.
612 static const struct afs_call_type afs_YFSVLGetEndpoints
= {
613 .name
= "YFSVL.GetEndpoints",
614 .op
= afs_YFSVL_GetEndpoints
,
615 .deliver
= afs_deliver_yfsvl_get_endpoints
,
616 .destructor
= afs_vl_get_addrs_u_destructor
,
620 * Dispatch an operation to get the addresses for a server, where the server is
623 struct afs_addr_list
*afs_yfsvl_get_endpoints(struct afs_vl_cursor
*vc
,
626 struct afs_call
*call
;
627 struct afs_net
*net
= vc
->cell
->net
;
632 call
= afs_alloc_flat_call(net
, &afs_YFSVLGetEndpoints
,
633 sizeof(__be32
) * 2 + sizeof(*uuid
),
634 sizeof(struct in6_addr
) + sizeof(__be32
) * 3);
636 return ERR_PTR(-ENOMEM
);
639 call
->ret_alist
= NULL
;
640 call
->max_lifespan
= AFS_VL_MAX_LIFESPAN
;
642 /* Marshall the parameters */
644 *bp
++ = htonl(YVLGETENDPOINTS
);
645 *bp
++ = htonl(YFS_SERVER_UUID
);
646 memcpy(bp
, uuid
, sizeof(*uuid
)); /* Type opr_uuid */
648 trace_afs_make_vl_call(call
);
649 afs_make_call(&vc
->ac
, call
, GFP_KERNEL
);
650 return (struct afs_addr_list
*)afs_wait_for_call_to_complete(call
, &vc
->ac
);