1/*	$NetBSD: nfs_prot_svc.c,v 1.1.1.2 2009/03/20 20:26:55 christos Exp $	*/
2
3/*
4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1989 Jan-Simon Pendry
6 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1989 The Regents of the University of California.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgment:
23 *      This product includes software developed by the University of
24 *      California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *
42 * File: am-utils/hlfsd/nfs_prot_svc.c
43 *
44 */
45
46#ifdef HAVE_CONFIG_H
47# include <config.h>
48#endif /* HAVE_CONFIG_H */
49#include <am_defs.h>
50#include <hlfsd.h>
51
52/* EXTERNAL FUNCTIONS */
53extern voidp nfsproc_null_2_svc(voidp, struct svc_req *);
54extern nfsattrstat *nfsproc_getattr_2_svc(am_nfs_fh *, struct svc_req *);
55extern nfsattrstat *nfsproc_setattr_2_svc(nfssattrargs *, struct svc_req *);
56extern voidp nfsproc_root_2_svc(voidp, struct svc_req *);
57extern nfsdiropres *nfsproc_lookup_2_svc(nfsdiropargs *, struct svc_req *);
58extern nfsreadlinkres *nfsproc_readlink_2_svc(am_nfs_fh *, struct svc_req *);
59extern nfsreadres *nfsproc_read_2_svc(nfsreadargs *, struct svc_req *);
60extern voidp nfsproc_writecache_2_svc(voidp, struct svc_req *);
61extern nfsattrstat *nfsproc_write_2_svc(nfswriteargs *, struct svc_req *);
62extern nfsdiropres *nfsproc_create_2_svc(nfscreateargs *, struct svc_req *);
63extern nfsstat *nfsproc_remove_2_svc(nfsdiropargs *, struct svc_req *);
64extern nfsstat *nfsproc_rename_2_svc(nfsrenameargs *, struct svc_req *);
65extern nfsstat *nfsproc_link_2_svc(nfslinkargs *, struct svc_req *);
66extern nfsstat *nfsproc_symlink_2_svc(nfssymlinkargs *, struct svc_req *);
67extern nfsdiropres *nfsproc_mkdir_2_svc(nfscreateargs *, struct svc_req *);
68extern nfsstat *nfsproc_rmdir_2_svc(nfsdiropargs *, struct svc_req *);
69extern nfsreaddirres *nfsproc_readdir_2_svc(nfsreaddirargs *, struct svc_req *);
70extern nfsstatfsres *nfsproc_statfs_2_svc(am_nfs_fh *, struct svc_req *);
71
72/* GLOBALS */
73SVCXPRT *nfs_program_2_transp;
74
75/* TYPEDEFS */
76typedef char *(*nfssvcproc_t)(voidp, struct svc_req *);
77
78
79void
80nfs_program_2(struct svc_req *rqstp, SVCXPRT *transp)
81{
82  union {
83    am_nfs_fh		nfsproc_getattr_2_arg;
84    nfssattrargs	nfsproc_setattr_2_arg;
85    nfsdiropargs	nfsproc_lookup_2_arg;
86    am_nfs_fh		nfsproc_readlink_2_arg;
87    nfsreadargs		nfsproc_read_2_arg;
88    nfswriteargs	nfsproc_write_2_arg;
89    nfscreateargs	nfsproc_create_2_arg;
90    nfsdiropargs	nfsproc_remove_2_arg;
91    nfsrenameargs	nfsproc_rename_2_arg;
92    nfslinkargs		nfsproc_link_2_arg;
93    nfssymlinkargs	nfsproc_symlink_2_arg;
94    nfscreateargs	nfsproc_mkdir_2_arg;
95    nfsdiropargs	nfsproc_rmdir_2_arg;
96    nfsreaddirargs	nfsproc_readdir_2_arg;
97    am_nfs_fh		nfsproc_statfs_2_arg;
98  } argument;
99  char *result;
100  xdrproc_t xdr_argument, xdr_result;
101  nfssvcproc_t local;
102
103  nfs_program_2_transp = NULL;
104
105  switch (rqstp->rq_proc) {
106
107  case NFSPROC_NULL:
108    xdr_argument = (xdrproc_t) xdr_void;
109    xdr_result = (xdrproc_t) xdr_void;
110    local = (nfssvcproc_t) nfsproc_null_2_svc;
111    break;
112
113  case NFSPROC_GETATTR:
114    xdr_argument = (xdrproc_t) xdr_nfs_fh;
115    xdr_result = (xdrproc_t) xdr_attrstat;
116    local = (nfssvcproc_t) nfsproc_getattr_2_svc;
117    break;
118
119  case NFSPROC_SETATTR:
120    xdr_argument = (xdrproc_t) xdr_sattrargs;
121    xdr_result = (xdrproc_t) xdr_attrstat;
122    local = (nfssvcproc_t) nfsproc_setattr_2_svc;
123    break;
124
125  case NFSPROC_ROOT:
126    xdr_argument = (xdrproc_t) xdr_void;
127    xdr_result = (xdrproc_t) xdr_void;
128    local = (nfssvcproc_t) nfsproc_root_2_svc;
129    break;
130
131  case NFSPROC_LOOKUP:
132    xdr_argument = (xdrproc_t) xdr_diropargs;
133    xdr_result = (xdrproc_t) xdr_diropres;
134    local = (nfssvcproc_t) nfsproc_lookup_2_svc;
135    /*
136     * Cheap way to pass transp down to afs_lookuppn so it can
137     * be stored in the am_node structure and later used for
138     * quick_reply().
139     */
140    nfs_program_2_transp = transp;
141    break;
142
143  case NFSPROC_READLINK:
144    xdr_argument = (xdrproc_t) xdr_nfs_fh;
145    xdr_result = (xdrproc_t) xdr_readlinkres;
146    local = (nfssvcproc_t) nfsproc_readlink_2_svc;
147    break;
148
149  case NFSPROC_READ:
150    xdr_argument = (xdrproc_t) xdr_readargs;
151    xdr_result = (xdrproc_t) xdr_readres;
152    local = (nfssvcproc_t) nfsproc_read_2_svc;
153    break;
154
155  case NFSPROC_WRITECACHE:
156    xdr_argument = (xdrproc_t) xdr_void;
157    xdr_result = (xdrproc_t) xdr_void;
158    local = (nfssvcproc_t) nfsproc_writecache_2_svc;
159    break;
160
161  case NFSPROC_WRITE:
162    xdr_argument = (xdrproc_t) xdr_writeargs;
163    xdr_result = (xdrproc_t) xdr_attrstat;
164    local = (nfssvcproc_t) nfsproc_write_2_svc;
165    break;
166
167  case NFSPROC_CREATE:
168    xdr_argument = (xdrproc_t) xdr_createargs;
169    xdr_result = (xdrproc_t) xdr_diropres;
170    local = (nfssvcproc_t) nfsproc_create_2_svc;
171    break;
172
173  case NFSPROC_REMOVE:
174    xdr_argument = (xdrproc_t) xdr_diropargs;
175    xdr_result = (xdrproc_t) xdr_nfsstat;
176    local = (nfssvcproc_t) nfsproc_remove_2_svc;
177    break;
178
179  case NFSPROC_RENAME:
180    xdr_argument = (xdrproc_t) xdr_renameargs;
181    xdr_result = (xdrproc_t) xdr_nfsstat;
182    local = (nfssvcproc_t) nfsproc_rename_2_svc;
183    break;
184
185  case NFSPROC_LINK:
186    xdr_argument = (xdrproc_t) xdr_linkargs;
187    xdr_result = (xdrproc_t) xdr_nfsstat;
188    local = (nfssvcproc_t) nfsproc_link_2_svc;
189    break;
190
191  case NFSPROC_SYMLINK:
192    xdr_argument = (xdrproc_t) xdr_symlinkargs;
193    xdr_result = (xdrproc_t) xdr_nfsstat;
194    local = (nfssvcproc_t) nfsproc_symlink_2_svc;
195    break;
196
197  case NFSPROC_MKDIR:
198    xdr_argument = (xdrproc_t) xdr_createargs;
199    xdr_result = (xdrproc_t) xdr_diropres;
200    local = (nfssvcproc_t) nfsproc_mkdir_2_svc;
201    break;
202
203  case NFSPROC_RMDIR:
204    xdr_argument = (xdrproc_t) xdr_diropargs;
205    xdr_result = (xdrproc_t) xdr_nfsstat;
206    local = (nfssvcproc_t) nfsproc_rmdir_2_svc;
207    break;
208
209  case NFSPROC_READDIR:
210    xdr_argument = (xdrproc_t) xdr_readdirargs;
211    xdr_result = (xdrproc_t) xdr_readdirres;
212    local = (nfssvcproc_t) nfsproc_readdir_2_svc;
213    break;
214
215  case NFSPROC_STATFS:
216    xdr_argument = (xdrproc_t) xdr_nfs_fh;
217    xdr_result = (xdrproc_t) xdr_statfsres;
218    local = (nfssvcproc_t) nfsproc_statfs_2_svc;
219    break;
220
221  default:
222    svcerr_noproc(transp);
223    return;
224  }
225
226  memset((char *) &argument, 0, sizeof(argument));
227  if (!svc_getargs(transp,
228		   (XDRPROC_T_TYPE) xdr_argument,
229		   (SVC_IN_ARG_TYPE) &argument)) {
230    plog(XLOG_ERROR,
231	 "NFS xdr decode failed for %d %d %d",
232	 (int) rqstp->rq_prog, (int) rqstp->rq_vers, (int) rqstp->rq_proc);
233    svcerr_decode(transp);
234    return;
235  }
236  result = (*local) (&argument, rqstp);
237
238  nfs_program_2_transp = NULL;
239
240  if (result != NULL && !svc_sendreply(transp,
241				       (XDRPROC_T_TYPE) xdr_result,
242				       result)) {
243    svcerr_systemerr(transp);
244  }
245  if (!svc_freeargs(transp,
246		    (XDRPROC_T_TYPE) xdr_argument,
247		    (SVC_IN_ARG_TYPE) & argument)) {
248    plog(XLOG_FATAL, "unable to free rpc arguments in nfs_program_2");
249    going_down(1);
250  }
251}
252