drm/i915: Fix hangs on some broadwell machines
[dragonfly.git] / contrib / amd / amd / amq_subr.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1997-1999 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * %W% (Berkeley) %G%
40 *
41 * $Id: amq_subr.c,v 1.5 1999/08/24 21:31:06 ezk Exp $
42 * $FreeBSD: src/contrib/amd/amd/amq_subr.c,v 1.5 1999/09/15 05:45:13 obrien Exp $
1de703da 43 * $DragonFly: src/contrib/amd/amd/amq_subr.c,v 1.2 2003/06/17 04:23:56 dillon Exp $
984263bc
MD
44 *
45 */
46/*
47 * Auxiliary routines for amq tool
48 */
49
50#ifdef HAVE_CONFIG_H
51# include <config.h>
52#endif /* HAVE_CONFIG_H */
53#include <am_defs.h>
54#include <amd.h>
55
56/* forward definitions */
57bool_t xdr_amq_mount_tree_node(XDR *xdrs, amq_mount_tree *objp);
58bool_t xdr_amq_mount_subtree(XDR *xdrs, amq_mount_tree *objp);
59
60
61voidp
62amqproc_null_1_svc(voidp argp, struct svc_req *rqstp)
63{
64 static char res;
65
66 return (voidp) &res;
67}
68
69
70/*
71 * Return a sub-tree of mounts
72 */
73amq_mount_tree_p *
74amqproc_mnttree_1_svc(voidp argp, struct svc_req *rqstp)
75{
76 static am_node *mp;
77
78 mp = find_ap(*(char **) argp);
79 return (amq_mount_tree_p *) &mp;
80}
81
82
83/*
84 * Unmount a single node
85 */
86voidp
87amqproc_umnt_1_svc(voidp argp, struct svc_req *rqstp)
88{
89 static char res;
90 am_node *mp = find_ap(*(char **) argp);
91
92 if (mp)
93 forcibly_timeout_mp(mp);
94
95 return (voidp) &res;
96}
97
98
99/*
100 * Return global statistics
101 */
102amq_mount_stats *
103amqproc_stats_1_svc(voidp argp, struct svc_req *rqstp)
104{
105 return (amq_mount_stats *) &amd_stats;
106}
107
108
109/*
110 * Return the entire tree of mount nodes
111 */
112amq_mount_tree_list *
113amqproc_export_1_svc(voidp argp, struct svc_req *rqstp)
114{
115 static amq_mount_tree_list aml;
116
117 aml.amq_mount_tree_list_val = (amq_mount_tree_p *) &exported_ap[0];
118 aml.amq_mount_tree_list_len = 1; /* XXX */
119
120 return &aml;
121}
122
123
124int *
125amqproc_setopt_1_svc(voidp argp, struct svc_req *rqstp)
126{
127 static int rc;
128 amq_setopt *opt = (amq_setopt *) argp;
129
130 rc = 0;
131
132 switch (opt->as_opt) {
133
134 case AMOPT_DEBUG:
135#ifdef DEBUG
136 if (debug_option(opt->as_str))
137#endif /* DEBUG */
138 rc = EINVAL;
139 break;
140
141 case AMOPT_LOGFILE:
142 if (gopt.logfile && opt->as_str
143 && STREQ(gopt.logfile, opt->as_str)) {
144 if (switch_to_logfile(opt->as_str, orig_umask))
145 rc = EINVAL;
146 } else {
147 rc = EACCES;
148 }
149 break;
150
151 case AMOPT_XLOG:
152 if (switch_option(opt->as_str))
153 rc = EINVAL;
154 break;
155
156 case AMOPT_FLUSHMAPC:
157 if (amd_state == Run) {
158 plog(XLOG_INFO, "amq says flush cache");
159 do_mapc_reload = 0;
160 flush_nfs_fhandle_cache((fserver *) 0);
161 flush_srvr_nfs_cache();
162 }
163 break;
164 }
165
166 return &rc;
167}
168
169
170amq_mount_info_list *
171amqproc_getmntfs_1_svc(voidp argp, struct svc_req *rqstp)
172{
173 return (amq_mount_info_list *) &mfhead; /* XXX */
174}
175
176#ifdef ENABLE_AMQ_MOUNT
177/*
178 * This is code that is vulnerable to IP spoofing attacks. Unless you
179 * absolutely need it, I suggest you do not enable it
180 * (using configure --enable-amq-mount)
181 */
182static int
183ok_security(struct svc_req *rqstp)
184{
185 struct sockaddr_in *sin = (struct sockaddr_in *) NULL;
186
187 if ((sin = amu_svc_getcaller(rqstp->rq_xprt)) == NULL) {
188 plog(XLOG_ERROR, "amu_svc_getcaller returned NULL");
189 return(0); /* assume security is therefore not OK */
190 }
191
192 if (ntohs(sin->sin_port) >= IPPORT_RESERVED ||
193 !(sin->sin_addr.s_addr == htonl(0x7f000001) ||
194 sin->sin_addr.s_addr == myipaddr.s_addr)) {
195 char dq[20];
196 plog(XLOG_INFO, "AMQ request from %s.%d DENIED",
197 inet_dquad(dq, sin->sin_addr.s_addr),
198 ntohs(sin->sin_port));
199 return (0);
200 }
201
202 return (1);
203}
204
205
206int *
207amqproc_mount_1_svc(voidp argp, struct svc_req *rqstp)
208{
209 static int rc = EINVAL;
210 char s[AMQ_STRLEN];
211 char *cp;
212 char dq[20];
213 struct sockaddr_in *sin;
214
215 if ((sin = amu_svc_getcaller(rqstp->rq_xprt)) == NULL) {
216 plog(XLOG_ERROR, "amu_svc_getcaller returned NULL");
217 return &rc;
218 }
219
220 strncpy(s, *(amq_string *) argp, AMQ_STRLEN-1);
221 s[AMQ_STRLEN-1] = '\0'; /* null terminate, to be sure */
222 plog(XLOG_ERROR,
223 "amq requested mount of %s from %s.%d",
224 s, inet_dquad(dq, sin->sin_addr.s_addr),
225 ntohs(sin->sin_port));
226
227 /*
228 * Minimalist security check.
229 */
230 if (!ok_security(rqstp)) {
231 rc = EACCES;
232 return &rc;
233 }
234 /*
235 * Find end of key
236 */
237 for (cp = (char *) s; *cp && (!isascii(*cp) || !isspace(*cp)); cp++) ;
238
239 if (!*cp) {
240 plog(XLOG_INFO, "amqproc_mount: Invalid arguments");
241 rc = EINVAL;
242 return &rc;
243 }
244 *cp++ = '\0';
245
246 /*
247 * Find start of value
248 */
249 while (*cp && isascii(*cp) && isspace(*cp))
250 cp++;
251
252 root_newmap(s, cp, (char *) 0, NULL);
253 rc = mount_auto_node(s, (voidp) root_node);
254 if (rc < 0)
255 return 0;
256 return &rc;
257}
258#endif /* ENABLE_AMQ_MOUNT */
259
260
261amq_string *
262amqproc_getvers_1_svc(voidp argp, struct svc_req *rqstp)
263{
264 static amq_string res;
265
266 res = get_version_string();
267 return &res;
268}
269
270
271/* get PID of remote amd */
272int *
273amqproc_getpid_1_svc(voidp argp, struct svc_req *rqstp)
274{
275 static int res;
276
277 res = getpid();
278 return &res;
279}
280
281
282/*
283 * XDR routines.
284 */
285
286
287bool_t
288xdr_amq_setopt(XDR *xdrs, amq_setopt *objp)
289{
290 if (!xdr_enum(xdrs, (enum_t *) & objp->as_opt)) {
291 return (FALSE);
292 }
293 if (!xdr_string(xdrs, &objp->as_str, AMQ_STRLEN)) {
294 return (FALSE);
295 }
296 return (TRUE);
297}
298
299
300/*
301 * More XDR routines - Should be used for OUTPUT ONLY.
302 */
303bool_t
304xdr_amq_mount_tree_node(XDR *xdrs, amq_mount_tree *objp)
305{
306 am_node *mp = (am_node *) objp;
307 long mtime;
308
309 if (!xdr_amq_string(xdrs, &mp->am_mnt->mf_info)) {
310 return (FALSE);
311 }
312 if (!xdr_amq_string(xdrs, &mp->am_path)) {
313 return (FALSE);
314 }
315 if (!xdr_amq_string(xdrs, mp->am_link ? &mp->am_link : &mp->am_mnt->mf_mount)) {
316 return (FALSE);
317 }
318 if (!xdr_amq_string(xdrs, &mp->am_mnt->mf_ops->fs_type)) {
319 return (FALSE);
320 }
321 mtime = mp->am_stats.s_mtime;
322 if (!xdr_long(xdrs, &mtime)) {
323 return (FALSE);
324 }
325 if (!xdr_u_short(xdrs, &mp->am_stats.s_uid)) {
326 return (FALSE);
327 }
328 if (!xdr_int(xdrs, &mp->am_stats.s_getattr)) {
329 return (FALSE);
330 }
331 if (!xdr_int(xdrs, &mp->am_stats.s_lookup)) {
332 return (FALSE);
333 }
334 if (!xdr_int(xdrs, &mp->am_stats.s_readdir)) {
335 return (FALSE);
336 }
337 if (!xdr_int(xdrs, &mp->am_stats.s_readlink)) {
338 return (FALSE);
339 }
340 if (!xdr_int(xdrs, &mp->am_stats.s_statfs)) {
341 return (FALSE);
342 }
343 return (TRUE);
344}
345
346
347bool_t
348xdr_amq_mount_subtree(XDR *xdrs, amq_mount_tree *objp)
349{
350 am_node *mp = (am_node *) objp;
351
352 if (!xdr_amq_mount_tree_node(xdrs, objp)) {
353 return (FALSE);
354 }
355 if (!xdr_pointer(xdrs, (char **) &mp->am_osib, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
356 return (FALSE);
357 }
358 if (!xdr_pointer(xdrs, (char **) &mp->am_child, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
359 return (FALSE);
360 }
361 return (TRUE);
362}
363
364
365bool_t
366xdr_amq_mount_tree(XDR *xdrs, amq_mount_tree *objp)
367{
368 am_node *mp = (am_node *) objp;
369 am_node *mnil = 0;
370
371 if (!xdr_amq_mount_tree_node(xdrs, objp)) {
372 return (FALSE);
373 }
374 if (!xdr_pointer(xdrs, (char **) &mnil, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
375 return (FALSE);
376 }
377 if (!xdr_pointer(xdrs, (char **) &mp->am_child, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
378 return (FALSE);
379 }
380 return (TRUE);
381}
382
383
384bool_t
385xdr_amq_mount_tree_p(XDR *xdrs, amq_mount_tree_p *objp)
386{
387 if (!xdr_pointer(xdrs, (char **) objp, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_tree)) {
388 return (FALSE);
389 }
390 return (TRUE);
391}
392
393
394bool_t
395xdr_amq_mount_stats(XDR *xdrs, amq_mount_stats *objp)
396{
397 if (!xdr_int(xdrs, &objp->as_drops)) {
398 return (FALSE);
399 }
400 if (!xdr_int(xdrs, &objp->as_stale)) {
401 return (FALSE);
402 }
403 if (!xdr_int(xdrs, &objp->as_mok)) {
404 return (FALSE);
405 }
406 if (!xdr_int(xdrs, &objp->as_merr)) {
407 return (FALSE);
408 }
409 if (!xdr_int(xdrs, &objp->as_uerr)) {
410 return (FALSE);
411 }
412 return (TRUE);
413}
414
415
416
417bool_t
418xdr_amq_mount_tree_list(XDR *xdrs, amq_mount_tree_list *objp)
419{
420 if (!xdr_array(xdrs,
421 (char **) &objp->amq_mount_tree_list_val,
422 (u_int *) &objp->amq_mount_tree_list_len,
423 ~0,
424 sizeof(amq_mount_tree_p),
425 (XDRPROC_T_TYPE) xdr_amq_mount_tree_p)) {
426 return (FALSE);
427 }
428 return (TRUE);
429}
430
431
432
433/*
434 * Compute length of list
435 */
436bool_t
437xdr_amq_mount_info_qelem(XDR *xdrs, qelem *qhead)
438{
439 mntfs *mf;
440 u_int len = 0;
441
442 for (mf = AM_LAST(mntfs, qhead); mf != HEAD(mntfs, qhead); mf = PREV(mntfs, mf)) {
443 if (!(mf->mf_ops->fs_flags & FS_AMQINFO))
444 continue;
445 len++;
446 }
447 xdr_u_int(xdrs, &len);
448
449 /*
450 * Send individual data items
451 */
452 for (mf = AM_LAST(mntfs, qhead); mf != HEAD(mntfs, qhead); mf = PREV(mntfs, mf)) {
453 int up;
454 if (!(mf->mf_ops->fs_flags & FS_AMQINFO))
455 continue;
456
457 if (!xdr_amq_string(xdrs, &mf->mf_ops->fs_type)) {
458 return (FALSE);
459 }
460 if (!xdr_amq_string(xdrs, &mf->mf_mount)) {
461 return (FALSE);
462 }
463 if (!xdr_amq_string(xdrs, &mf->mf_info)) {
464 return (FALSE);
465 }
466 if (!xdr_amq_string(xdrs, &mf->mf_server->fs_host)) {
467 return (FALSE);
468 }
469 if (!xdr_int(xdrs, &mf->mf_error)) {
470 return (FALSE);
471 }
472 if (!xdr_int(xdrs, &mf->mf_refc)) {
473 return (FALSE);
474 }
475 if (mf->mf_server->fs_flags & FSF_ERROR)
476 up = 0;
477 else
478 switch (mf->mf_server->fs_flags & (FSF_DOWN | FSF_VALID)) {
479 case FSF_DOWN | FSF_VALID:
480 up = 0;
481 break;
482 case FSF_VALID:
483 up = 1;
484 break;
485 default:
486 up = -1;
487 break;
488 }
489 if (!xdr_int(xdrs, &up)) {
490 return (FALSE);
491 }
492 }
493 return (TRUE);
494}
495
496
497bool_t
498xdr_pri_free(XDRPROC_T_TYPE xdr_args, caddr_t args_ptr)
499{
500 XDR xdr;
501
502 xdr.x_op = XDR_FREE;
503 return ((*xdr_args) (&xdr, (caddr_t *) args_ptr));
504}