1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31#ifndef lint
32/*static char sccsid[] = "from: @(#)nlm_prot.x 1.8 87/09/21 Copyr 1987 Sun Micro";*/
33/*static char sccsid[] = "from: * @(#)nlm_prot.x	2.1 88/08/01 4.0 RPCSRC";*/
34__RCSID("$NetBSD: nlm_prot.x,v 1.6 2000/06/07 14:30:15 bouyer Exp $");
35#endif /* not lint */
36__FBSDID("$FreeBSD$");
37
38#include <sys/param.h>
39#include <sys/malloc.h>
40#include <sys/systm.h>
41
42#include <nlm/nlm_prot.h>
43#include <nlm/nlm.h>
44
45/**********************************************************************/
46
47/*
48 * Convert between various versions of the protocol structures.
49 */
50
51static void
52nlm_convert_to_nlm4_lock(struct nlm4_lock *dst, struct nlm_lock *src)
53{
54
55	dst->caller_name = src->caller_name;
56	dst->fh = src->fh;
57	dst->oh = src->oh;
58	dst->svid = src->svid;
59	dst->l_offset = src->l_offset;
60	dst->l_len = src->l_len;
61}
62
63static void
64nlm_convert_to_nlm4_share(struct nlm4_share *dst, struct nlm_share *src)
65{
66
67	dst->caller_name = src->caller_name;
68	dst->fh = src->fh;
69	dst->oh = src->oh;
70	dst->mode = src->mode;
71	dst->access = src->access;
72}
73
74static void
75nlm_convert_to_nlm_holder(struct nlm_holder *dst, struct nlm4_holder *src)
76{
77
78	dst->exclusive = src->exclusive;
79	dst->svid = src->svid;
80	dst->oh = src->oh;
81	dst->l_offset = src->l_offset;
82	dst->l_len = src->l_len;
83}
84
85static void
86nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src)
87{
88
89	dst->exclusive = src->exclusive;
90	dst->svid = src->svid;
91	dst->oh = src->oh;
92	dst->l_offset = src->l_offset;
93	dst->l_len = src->l_len;
94}
95
96static enum nlm_stats
97nlm_convert_to_nlm_stats(enum nlm4_stats src)
98{
99	if (src > nlm4_deadlck)
100		return nlm_denied;
101	return (enum nlm_stats) src;
102}
103
104static void
105nlm_convert_to_nlm_res(struct nlm_res *dst, struct nlm4_res *src)
106{
107	dst->cookie = src->cookie;
108	dst->stat.stat = nlm_convert_to_nlm_stats(src->stat.stat);
109}
110
111static void
112nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src)
113{
114	dst->cookie = src->cookie;
115	dst->stat.stat = (enum nlm4_stats) src->stat.stat;
116}
117
118/**********************************************************************/
119
120/*
121 * RPC server stubs.
122 */
123
124bool_t
125nlm_sm_notify_0_svc(struct nlm_sm_status *argp, void *result, struct svc_req *rqstp)
126{
127	nlm_sm_notify(argp);
128
129	return (TRUE);
130}
131
132bool_t
133nlm_test_1_svc(struct nlm_testargs *argp, nlm_testres *result, struct svc_req *rqstp)
134{
135	bool_t retval;
136	nlm4_testargs args4;
137	nlm4_testres res4;
138
139	args4.cookie = argp->cookie;
140	args4.exclusive = argp->exclusive;
141	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
142
143	retval = nlm4_test_4_svc(&args4, &res4, rqstp);
144	if (retval) {
145		result->cookie = res4.cookie;
146		result->stat.stat = nlm_convert_to_nlm_stats(res4.stat.stat);
147		if (result->stat.stat == nlm_denied)
148			nlm_convert_to_nlm_holder(
149				&result->stat.nlm_testrply_u.holder,
150				&res4.stat.nlm4_testrply_u.holder);
151	}
152
153	return (retval);
154}
155
156bool_t
157nlm_lock_1_svc(struct nlm_lockargs *argp, nlm_res *result, struct svc_req *rqstp)
158{
159	bool_t retval;
160	nlm4_lockargs args4;
161	nlm4_res res4;
162
163	args4.cookie = argp->cookie;
164	args4.block = argp->block;
165	args4.exclusive = argp->exclusive;
166	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
167	args4.reclaim = argp->reclaim;
168	args4.state = argp->state;
169
170	retval = nlm4_lock_4_svc(&args4, &res4, rqstp);
171	if (retval)
172		nlm_convert_to_nlm_res(result, &res4);
173
174	return (retval);
175}
176
177bool_t
178nlm_cancel_1_svc(struct nlm_cancargs *argp, nlm_res *result, struct svc_req *rqstp)
179{
180	bool_t retval;
181	nlm4_cancargs args4;
182	nlm4_res res4;
183
184	args4.cookie = argp->cookie;
185	args4.block = argp->block;
186	args4.exclusive = argp->exclusive;
187	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
188
189	retval = nlm4_cancel_4_svc(&args4, &res4, rqstp);
190	if (retval)
191		nlm_convert_to_nlm_res(result, &res4);
192
193	return (retval);
194}
195
196bool_t
197nlm_unlock_1_svc(struct nlm_unlockargs *argp, nlm_res *result, struct svc_req *rqstp)
198{
199	bool_t retval;
200	nlm4_unlockargs args4;
201	nlm4_res res4;
202
203	args4.cookie = argp->cookie;
204	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
205
206	retval = nlm4_unlock_4_svc(&args4, &res4, rqstp);
207	if (retval)
208		nlm_convert_to_nlm_res(result, &res4);
209
210	return (retval);
211}
212
213bool_t
214nlm_granted_1_svc(struct nlm_testargs *argp, nlm_res *result, struct svc_req *rqstp)
215{
216	bool_t retval;
217	nlm4_testargs args4;
218	nlm4_res res4;
219
220	args4.cookie = argp->cookie;
221	args4.exclusive = argp->exclusive;
222	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
223
224	retval = nlm4_granted_4_svc(&args4, &res4, rqstp);
225	if (retval)
226		nlm_convert_to_nlm_res(result, &res4);
227
228	return (retval);
229}
230
231bool_t
232nlm_test_msg_1_svc(struct nlm_testargs *argp, void *result, struct svc_req *rqstp)
233{
234	nlm4_testargs args4;
235	nlm4_testres res4;
236	nlm_testres res;
237	CLIENT *rpc;
238	char dummy;
239
240	args4.cookie = argp->cookie;
241	args4.exclusive = argp->exclusive;
242	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
243
244	if (nlm_do_test(&args4, &res4, rqstp, &rpc))
245		return (FALSE);
246
247	res.cookie = res4.cookie;
248	res.stat.stat = nlm_convert_to_nlm_stats(res4.stat.stat);
249	if (res.stat.stat == nlm_denied)
250		nlm_convert_to_nlm_holder(
251			&res.stat.nlm_testrply_u.holder,
252			&res4.stat.nlm4_testrply_u.holder);
253
254	if (rpc) {
255		nlm_test_res_1(&res, &dummy, rpc, NULL, nlm_zero_tv);
256		CLNT_RELEASE(rpc);
257	}
258	xdr_free((xdrproc_t) xdr_nlm_testres, &res);
259
260	return (FALSE);
261}
262
263bool_t
264nlm_lock_msg_1_svc(struct nlm_lockargs *argp, void *result, struct svc_req *rqstp)
265{
266	nlm4_lockargs args4;
267	nlm4_res res4;
268	nlm_res res;
269	CLIENT *rpc;
270	char dummy;
271
272	args4.cookie = argp->cookie;
273	args4.block = argp->block;
274	args4.exclusive = argp->exclusive;
275	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
276	args4.reclaim = argp->reclaim;
277	args4.state = argp->state;
278
279	if (nlm_do_lock(&args4, &res4, rqstp, TRUE, &rpc))
280		return (FALSE);
281
282	nlm_convert_to_nlm_res(&res, &res4);
283
284	if (rpc) {
285		nlm_lock_res_1(&res, &dummy, rpc, NULL, nlm_zero_tv);
286		CLNT_RELEASE(rpc);
287	}
288	xdr_free((xdrproc_t) xdr_nlm_res, &res);
289
290	return (FALSE);
291}
292
293bool_t
294nlm_cancel_msg_1_svc(struct nlm_cancargs *argp, void *result, struct svc_req *rqstp)
295{
296	nlm4_cancargs args4;
297	nlm4_res res4;
298	nlm_res res;
299	CLIENT *rpc;
300	char dummy;
301
302	args4.cookie = argp->cookie;
303	args4.block = argp->block;
304	args4.exclusive = argp->exclusive;
305	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
306
307	if (nlm_do_cancel(&args4, &res4, rqstp, &rpc))
308		return (FALSE);
309
310	nlm_convert_to_nlm_res(&res, &res4);
311
312	if (rpc) {
313		nlm_cancel_res_1(&res, &dummy, rpc, NULL, nlm_zero_tv);
314		CLNT_RELEASE(rpc);
315	}
316	xdr_free((xdrproc_t) xdr_nlm_res, &res);
317
318	return (FALSE);
319}
320
321bool_t
322nlm_unlock_msg_1_svc(struct nlm_unlockargs *argp, void *result, struct svc_req *rqstp)
323{
324	nlm4_unlockargs args4;
325	nlm4_res res4;
326	nlm_res res;
327	CLIENT *rpc;
328	char dummy;
329
330	args4.cookie = argp->cookie;
331	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
332
333	if (nlm_do_unlock(&args4, &res4, rqstp, &rpc))
334		return (FALSE);
335
336	nlm_convert_to_nlm_res(&res, &res4);
337
338	if (rpc) {
339		nlm_unlock_res_1(&res, &dummy, rpc, NULL, nlm_zero_tv);
340		CLNT_RELEASE(rpc);
341	}
342	xdr_free((xdrproc_t) xdr_nlm_res, &res);
343
344	return (FALSE);
345}
346
347bool_t
348nlm_granted_msg_1_svc(struct nlm_testargs *argp, void *result, struct svc_req *rqstp)
349{
350	nlm4_testargs args4;
351	nlm4_res res4;
352	nlm_res res;
353	CLIENT *rpc;
354	char dummy;
355
356	args4.cookie = argp->cookie;
357	args4.exclusive = argp->exclusive;
358	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
359
360	if (nlm_do_granted(&args4, &res4, rqstp, &rpc))
361		return (FALSE);
362
363	nlm_convert_to_nlm_res(&res, &res4);
364
365	if (rpc) {
366		nlm_granted_res_1(&res, &dummy, rpc, NULL, nlm_zero_tv);
367		CLNT_RELEASE(rpc);
368	}
369	xdr_free((xdrproc_t) xdr_nlm_res, &res);
370
371	return (FALSE);
372}
373
374bool_t
375nlm_test_res_1_svc(nlm_testres *argp, void *result, struct svc_req *rqstp)
376{
377	nlm4_testres args4;
378
379	args4.cookie = argp->cookie;
380	if (argp->stat.stat == nlm_denied)
381		nlm_convert_to_nlm4_holder(
382			&args4.stat.nlm4_testrply_u.holder,
383			&argp->stat.nlm_testrply_u.holder);
384
385	return (nlm4_test_res_4_svc(&args4, result, rqstp));
386}
387
388bool_t
389nlm_lock_res_1_svc(nlm_res *argp, void *result, struct svc_req *rqstp)
390{
391	nlm4_res arg4;
392
393	nlm_convert_to_nlm4_res(&arg4, argp);
394	return (nlm4_lock_res_4_svc(&arg4, result, rqstp));
395}
396
397bool_t
398nlm_cancel_res_1_svc(nlm_res *argp, void *result, struct svc_req *rqstp)
399{
400	nlm4_res arg4;
401
402	nlm_convert_to_nlm4_res(&arg4, argp);
403	return (nlm4_cancel_res_4_svc(&arg4, result, rqstp));
404}
405
406bool_t
407nlm_unlock_res_1_svc(nlm_res *argp, void *result, struct svc_req *rqstp)
408{
409	nlm4_res arg4;
410
411	nlm_convert_to_nlm4_res(&arg4, argp);
412	return (nlm4_unlock_res_4_svc(&arg4, result, rqstp));
413}
414
415bool_t
416nlm_granted_res_1_svc(nlm_res *argp, void *result, struct svc_req *rqstp)
417{
418	nlm4_res arg4;
419
420	nlm_convert_to_nlm4_res(&arg4, argp);
421	return (nlm4_granted_res_4_svc(&arg4, result, rqstp));
422}
423
424int
425nlm_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
426{
427
428	(void) xdr_free(xdr_result, result);
429	return (TRUE);
430}
431
432bool_t
433nlm_share_3_svc(nlm_shareargs *argp, nlm_shareres *result, struct svc_req *rqstp)
434{
435	bool_t retval;
436	nlm4_shareargs args4;
437	nlm4_shareres res4;
438
439	args4.cookie = argp->cookie;
440	nlm_convert_to_nlm4_share(&args4.share, &argp->share);
441	args4.reclaim = argp->reclaim;
442
443	retval = nlm4_share_4_svc(&args4, &res4, rqstp);
444	if (retval) {
445		result->cookie = res4.cookie;
446		result->stat = nlm_convert_to_nlm_stats(res4.stat);
447		result->sequence = res4.sequence;
448	}
449
450	return (retval);
451}
452
453bool_t
454nlm_unshare_3_svc(nlm_shareargs *argp, nlm_shareres *result, struct svc_req *rqstp)
455{
456	bool_t retval;
457	nlm4_shareargs args4;
458	nlm4_shareres res4;
459
460	args4.cookie = argp->cookie;
461	nlm_convert_to_nlm4_share(&args4.share, &argp->share);
462	args4.reclaim = argp->reclaim;
463
464	retval = nlm4_unshare_4_svc(&args4, &res4, rqstp);
465	if (retval) {
466		result->cookie = res4.cookie;
467		result->stat = nlm_convert_to_nlm_stats(res4.stat);
468		result->sequence = res4.sequence;
469	}
470
471	return (retval);
472}
473
474bool_t
475nlm_nm_lock_3_svc(nlm_lockargs *argp, nlm_res *result, struct svc_req *rqstp)
476{
477	bool_t retval;
478	nlm4_lockargs args4;
479	nlm4_res res4;
480
481	args4.cookie = argp->cookie;
482	args4.block = argp->block;
483	args4.exclusive = argp->exclusive;
484	nlm_convert_to_nlm4_lock(&args4.alock, &argp->alock);
485	args4.reclaim = argp->reclaim;
486	args4.state = argp->state;
487
488	retval = nlm4_nm_lock_4_svc(&args4, &res4, rqstp);
489	if (retval)
490		nlm_convert_to_nlm_res(result, &res4);
491
492	return (retval);
493}
494
495bool_t
496nlm_free_all_3_svc(nlm_notify *argp, void *result, struct svc_req *rqstp)
497{
498	struct nlm4_notify args4;
499
500	args4.name = argp->name;
501	args4.state = argp->state;
502
503	return (nlm4_free_all_4_svc(&args4, result, rqstp));
504}
505
506int
507nlm_prog_3_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
508{
509
510	(void) xdr_free(xdr_result, result);
511	return (TRUE);
512}
513
514bool_t
515nlm4_test_4_svc(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp)
516{
517
518	nlm_do_test(argp, result, rqstp, NULL);
519	return (TRUE);
520}
521
522bool_t
523nlm4_lock_4_svc(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp)
524{
525
526	nlm_do_lock(argp, result, rqstp, TRUE, NULL);
527	return (TRUE);
528}
529
530bool_t
531nlm4_cancel_4_svc(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp)
532{
533
534	nlm_do_cancel(argp, result, rqstp, NULL);
535	return (TRUE);
536}
537
538bool_t
539nlm4_unlock_4_svc(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp)
540{
541
542	nlm_do_unlock(argp, result, rqstp, NULL);
543	return (TRUE);
544}
545
546bool_t
547nlm4_granted_4_svc(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp)
548{
549
550	nlm_do_granted(argp, result, rqstp, NULL);
551	return (TRUE);
552}
553
554bool_t
555nlm4_test_msg_4_svc(nlm4_testargs *argp, void *result, struct svc_req *rqstp)
556{
557	nlm4_testres res4;
558	CLIENT *rpc;
559	char dummy;
560
561	if (nlm_do_test(argp, &res4, rqstp, &rpc))
562		return (FALSE);
563	if (rpc) {
564		nlm4_test_res_4(&res4, &dummy, rpc, NULL, nlm_zero_tv);
565		CLNT_RELEASE(rpc);
566	}
567	xdr_free((xdrproc_t) xdr_nlm4_testres, &res4);
568
569	return (FALSE);
570}
571
572bool_t
573nlm4_lock_msg_4_svc(nlm4_lockargs *argp, void *result, struct svc_req *rqstp)
574{
575	nlm4_res res4;
576	CLIENT *rpc;
577	char dummy;
578
579	if (nlm_do_lock(argp, &res4, rqstp, TRUE, &rpc))
580		return (FALSE);
581	if (rpc) {
582		nlm4_lock_res_4(&res4, &dummy, rpc, NULL, nlm_zero_tv);
583		CLNT_RELEASE(rpc);
584	}
585	xdr_free((xdrproc_t) xdr_nlm4_res, &res4);
586
587	return (FALSE);
588}
589
590bool_t
591nlm4_cancel_msg_4_svc(nlm4_cancargs *argp, void *result, struct svc_req *rqstp)
592{
593	nlm4_res res4;
594	CLIENT *rpc;
595	char dummy;
596
597	if (nlm_do_cancel(argp, &res4, rqstp, &rpc))
598		return (FALSE);
599	if (rpc) {
600		nlm4_cancel_res_4(&res4, &dummy, rpc, NULL, nlm_zero_tv);
601		CLNT_RELEASE(rpc);
602	}
603	xdr_free((xdrproc_t) xdr_nlm4_res, &res4);
604
605	return (FALSE);
606}
607
608bool_t
609nlm4_unlock_msg_4_svc(nlm4_unlockargs *argp, void *result, struct svc_req *rqstp)
610{
611	nlm4_res res4;
612	CLIENT *rpc;
613	char dummy;
614
615	if (nlm_do_unlock(argp, &res4, rqstp, &rpc))
616		return (FALSE);
617	if (rpc) {
618		nlm4_unlock_res_4(&res4, &dummy, rpc, NULL, nlm_zero_tv);
619		CLNT_RELEASE(rpc);
620	}
621	xdr_free((xdrproc_t) xdr_nlm4_res, &res4);
622
623	return (FALSE);
624}
625
626bool_t
627nlm4_granted_msg_4_svc(nlm4_testargs *argp, void *result, struct svc_req *rqstp)
628{
629	nlm4_res res4;
630	CLIENT *rpc;
631	char dummy;
632
633	if (nlm_do_granted(argp, &res4, rqstp, &rpc))
634		return (FALSE);
635	if (rpc) {
636		nlm4_granted_res_4(&res4, &dummy, rpc, NULL, nlm_zero_tv);
637		CLNT_RELEASE(rpc);
638	}
639	xdr_free((xdrproc_t) xdr_nlm4_res, &res4);
640
641	return (FALSE);
642}
643
644bool_t
645nlm4_test_res_4_svc(nlm4_testres *argp, void *result, struct svc_req *rqstp)
646{
647
648	return (FALSE);
649}
650
651bool_t
652nlm4_lock_res_4_svc(nlm4_res *argp, void *result, struct svc_req *rqstp)
653{
654
655	return (FALSE);
656}
657
658bool_t
659nlm4_cancel_res_4_svc(nlm4_res *argp, void *result, struct svc_req *rqstp)
660{
661
662	return (FALSE);
663}
664
665bool_t
666nlm4_unlock_res_4_svc(nlm4_res *argp, void *result, struct svc_req *rqstp)
667{
668
669	return (FALSE);
670}
671
672bool_t
673nlm4_granted_res_4_svc(nlm4_res *argp, void *result, struct svc_req *rqstp)
674{
675
676	nlm_do_granted_res(argp, rqstp);
677	return (FALSE);
678}
679
680bool_t
681nlm4_share_4_svc(nlm4_shareargs *argp, nlm4_shareres *result, struct svc_req *rqstp)
682{
683
684	memset(result, 0, sizeof(*result));
685	result->stat = nlm4_denied;
686	return (TRUE);
687}
688
689bool_t
690nlm4_unshare_4_svc(nlm4_shareargs *argp, nlm4_shareres *result, struct svc_req *rqstp)
691{
692
693	memset(result, 0, sizeof(*result));
694	result->stat = nlm4_denied;
695	return (TRUE);
696}
697
698bool_t
699nlm4_nm_lock_4_svc(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp)
700{
701
702	nlm_do_lock(argp, result, rqstp, FALSE, NULL);
703	return (TRUE);
704}
705
706bool_t
707nlm4_free_all_4_svc(nlm4_notify *argp, void *result, struct svc_req *rqstp)
708{
709
710	nlm_do_free_all(argp);
711	return (TRUE);
712}
713
714int
715nlm_prog_4_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
716{
717
718	(void) xdr_free(xdr_result, result);
719	return (TRUE);
720}
721