1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/ksynch.h>
#include <sys/cmn_err.h>
#include <sys/kmem.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/cred.h>
#include <sys/conf.h>
#include <sys/modctl.h>
#include <sys/errno.h>
#include <sys/unistat/spcs_s.h>
#include <sys/unistat/spcs_s_k.h>
#include <sys/unistat/spcs_errors.h>
#ifdef _SunOS_2_6
/*
* on 2.6 both dki_lock.h and rpc/types.h define bool_t so we
* define enum_t here as it is all we need from rpc/types.h
* anyway and make it look like we included it. Yuck.
*/
#define _RPC_TYPES_H
typedef int enum_t;
#else
#ifndef DS_DDICT
#include <rpc/types.h>
#endif
#endif /* _SunOS_2_6 */
#ifndef DS_DDICT
#include <rpc/auth.h>
#include <rpc/svc.h>
#include <rpc/xdr.h>
#else
#include "../contract.h"
#endif
#include <sys/ddi.h>
#include <sys/nsc_thread.h>
#include <sys/nsctl/nsctl.h>
#include <sys/nsctl/nsvers.h>
#include "rdc_io.h"
#include "rdc_stub.h"
#include "rdc_ioctl.h"
#include "rdcsrv.h"
#if defined(_SunOS_5_6) || defined(_SunOS_5_7)
static void rdcsrv_xprtclose(const SVCXPRT *xprt);
#else /* SunOS 5.8 or later */
/*
* SunOS 5.8 or later.
*
* RDC callout table
*
* This table is used by svc_getreq to dispatch a request with a given
* prog/vers pair to an approriate service provider.
*/
static SVC_CALLOUT rdcsrv_sc[] = {
{ RDC_PROGRAM, RDC_VERS_MIN, RDC_VERS_MAX, rdcstub_dispatch }
};
static SVC_CALLOUT_TABLE rdcsrv_sct = {
sizeof (rdcsrv_sc) / sizeof (rdcsrv_sc[0]), FALSE, rdcsrv_sc
};
#endif /* SunOS 5.8 or later */
static kmutex_t rdcsrv_lock;
static int rdcsrv_dup_error;
static int rdcsrv_registered;
static int rdcsrv_closing;
static int rdcsrv_refcnt;
long rdc_svc_count = 0;
static rdcsrv_t *rdcsrv_disptab;
/*
* Solaris module setup.
*/
extern struct mod_ops mod_miscops;
static struct modlmisc modlmisc = {
&mod_miscops, /* Type of module */
"nws:Remote Mirror kRPC:" ISS_VERSION_STR
};
static struct modlinkage modlinkage = {
MODREV_1,
&modlmisc,
NULL
};
int
_init(void)
{
int rc;
mutex_init(&rdcsrv_lock, NULL, MUTEX_DRIVER, NULL);
if ((rc = mod_install(&modlinkage)) != DDI_SUCCESS)
mutex_destroy(&rdcsrv_lock);
return (rc);
}
int
_fini(void)
{
int rc;
if ((rc = mod_remove(&modlinkage)) == DDI_SUCCESS)
mutex_destroy(&rdcsrv_lock);
return (rc);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/*
* RDC kRPC server stub.
*/
void
rdcsrv_noproc(void)
{
;
}
static int
rdcsrv_dispdup(struct svc_req *req, SVCXPRT *xprt)
{
rdc_disptab_t *disp;
struct dupreq *dr;
rdcsrv_t *srvp;
void (*fn)();
int dupstat;
srvp = &rdcsrv_disptab[req->rq_vers - RDC_VERS_MIN];
disp = &srvp->disptab[req->rq_proc];
fn = disp->dispfn;
dupstat = SVC_DUP(xprt, req, 0, 0, &dr);
switch (dupstat) {
case DUP_ERROR:
/* svcerr_systemerr does a freeargs */
svcerr_systemerr(xprt);
rdcsrv_dup_error++;
break;
case DUP_INPROGRESS:
rdcsrv_dup_error++;
break;
case DUP_NEW:
case DUP_DROP:
(*fn)(xprt, req);
SVC_DUPDONE(xprt, dr, 0, 0, DUP_DONE);
break;
case DUP_DONE:
break;
}
return (dupstat);
}
/*
* rdcsrv_dispatch is the dispatcher routine for the RDC RPC protocol
*/
void
rdcsrv_dispatch(struct svc_req *req, SVCXPRT *xprt)
{
rdc_disptab_t *disp;
rdcsrv_t *srvp;
mutex_enter(&rdcsrv_lock);
rdcsrv_refcnt++;
if (!rdcsrv_registered || rdcsrv_closing || !rdcsrv_disptab) {
mutex_exit(&rdcsrv_lock);
goto outdisp;
}
mutex_exit(&rdcsrv_lock);
if ((req->rq_vers < RDC_VERS_MIN) || (req->rq_vers > RDC_VERS_MAX)) {
svcerr_noproc(xprt);
cmn_err(CE_NOTE, "!rdcsrv_dispatch: unknown version %d",
req->rq_vers);
/* svcerr_noproc does a freeargs on xprt */
goto done;
}
srvp = &rdcsrv_disptab[req->rq_vers - RDC_VERS_MIN];
disp = &srvp->disptab[req->rq_proc];
if (req->rq_proc >= srvp->nprocs ||
disp->dispfn == rdcsrv_noproc) {
svcerr_noproc(xprt);
cmn_err(CE_NOTE, "!rdcsrv_dispatch: bad proc number %d",
req->rq_proc);
/* svcerr_noproc does a freeargs on xprt */
goto done;
} else if (disp->clone) {
switch (rdcsrv_dispdup(req, xprt)) {
case DUP_ERROR:
goto done;
/* NOTREACHED */
case DUP_INPROGRESS:
goto outdisp;
/* NOTREACHED */
default:
break;
}
} else {
(*disp->dispfn)(xprt, req);
rdc_svc_count++;
}
outdisp:
if (!SVC_FREEARGS(xprt, (xdrproc_t)0, (caddr_t)0))
cmn_err(CE_NOTE, "!rdcsrv_dispatch: bad freeargs");
done:
mutex_enter(&rdcsrv_lock);
rdcsrv_refcnt--;
mutex_exit(&rdcsrv_lock);
}
static int
rdcsrv_create(file_t *fp, rdc_svc_args_t *args, int mode)
{
/*LINTED*/
int rc, error = 0;
/*LINTED*/
rpcvers_t vers;
struct netbuf addrmask;
#if defined(_SunOS_5_6) || defined(_SunOS_5_7)
SVCXPRT *xprt;
#else
SVCMASTERXPRT *xprt;
#endif
STRUCT_HANDLE(rdc_svc_args, uap);
STRUCT_SET_HANDLE(uap, mode, args);
addrmask.len = STRUCT_FGET(uap, addrmask.len);
addrmask.maxlen = STRUCT_FGET(uap, addrmask.maxlen);
addrmask.buf = kmem_alloc(addrmask.maxlen, KM_SLEEP);
error = ddi_copyin(STRUCT_FGETP(uap, addrmask.buf), addrmask.buf,
addrmask.len, mode);
if (error) {
kmem_free(addrmask.buf, addrmask.maxlen);
#ifdef DEBUG
cmn_err(CE_WARN, "!addrmask copyin failed %p", (void *) args);
#endif
return (error);
}
/*
* Set rdcstub's dispatch handle to rdcsrv_dispatch
*/
rdcstub_set_dispatch(rdcsrv_dispatch);
/*
* Create a transport endpoint and create one kernel thread to run the
* rdc service loop
*/
#if defined(_SunOS_5_6) || defined(_SunOS_5_7)
error = svc_tli_kcreate(fp, RDC_RPC_MAX,
STRUCT_FGETP(uap, netid), &addrmask, STRUCT_FGET(uap, nthr), &xprt);
#else
{
#if defined(_SunOS_5_8)
struct svcpool_args p;
p.id = RDC_SVCPOOL_ID;
p.maxthreads = STRUCT_FGET(uap, nthr);
p.redline = 0;
p.qsize = 0;
p.timeout = 0;
p.stksize = 0;
p.max_same_xprt = 0;
error = svc_pool_create(&p);
if (error) {
cmn_err(CE_NOTE,
"!rdcsrv_create: svc_pool_create failed %d", error);
return (error);
}
#endif
error = svc_tli_kcreate(fp, RDC_RPC_MAX,
STRUCT_FGETP(uap, netid), &addrmask,
&xprt, &rdcsrv_sct, NULL, RDC_SVCPOOL_ID, FALSE);
}
#endif
if (error) {
cmn_err(CE_NOTE, "!rdcsrv_create: svc_tli_kcreate failed %d",
error);
return (error);
}
#if defined(_SunOS_5_6) || defined(_SunOS_5_7)
if (xprt == NULL) {
cmn_err(CE_NOTE, "!xprt in rdcsrv_create is NULL");
} else {
/*
* Register a cleanup routine in case the transport gets
* destroyed. If the registration fails for some reason,
* it means that the transport is already being destroyed.
* This shouldn't happen, but it's probably not worth a
* panic.
*/
if (!svc_control(xprt, SVCSET_CLOSEPROC,
(void *)rdcsrv_xprtclose)) {
cmn_err(
#ifdef DEBUG
CE_PANIC,
#else
CE_WARN,
#endif
"!rdcsrv_create: couldn't set xprt callback");
error = EBADF;
goto done;
}
}
for (vers = RDC_VERS_MIN; vers <= RDC_VERS_MAX; vers++) {
rc = svc_register(xprt, (ulong_t)RDC_PROGRAM, vers,
rdcstub_dispatch, 0);
if (!rc) {
cmn_err(CE_NOTE,
"!rdcsrv_create: svc_register(%d, %lu) failed",
RDC_PROGRAM, vers);
if (!error) {
error = EBADF;
}
}
}
#endif /* 5.6 or 5.7 */
if (!error) {
/* mark as registered with the kRPC subsystem */
rdcsrv_registered = 1;
}
done:
return (error);
}
#if defined(_SunOS_5_6) || defined(_SunOS_5_7)
/*
* Callback routine for when a transport is closed.
*/
static void
rdcsrv_xprtclose(const SVCXPRT *xprt)
{
}
#endif
/*
* Private interface from the main RDC module.
*/
int
rdcsrv_load(file_t *fp, rdcsrv_t *disptab, rdc_svc_args_t *args, int mode)
{
int rc = 0;
mutex_enter(&rdcsrv_lock);
rc = rdcsrv_create(fp, args, mode);
if (rc == 0) {
rdcsrv_disptab = disptab;
}
mutex_exit(&rdcsrv_lock);
return (rc);
}
void
rdcsrv_unload(void)
{
mutex_enter(&rdcsrv_lock);
/* Unset rdcstub's dispatch handle */
rdcstub_unset_dispatch();
rdcsrv_closing = 1;
while (rdcsrv_refcnt > 0) {
mutex_exit(&rdcsrv_lock);
delay(drv_usectohz(25));
mutex_enter(&rdcsrv_lock);
}
rdcsrv_closing = 0;
rdcsrv_disptab = 0;
mutex_exit(&rdcsrv_lock);
}
|