Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/nfs/nfs_server.c
+++ new/usr/src/uts/common/fs/nfs/nfs_server.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 24 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
25 25 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
26 26 * Copyright (c) 2013 by Delphix. All rights reserved.
27 27 */
28 28
29 29 /*
30 30 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
31 31 * All rights reserved.
32 32 * Use is subject to license terms.
33 33 */
34 34
35 35 #include <sys/param.h>
36 36 #include <sys/types.h>
37 37 #include <sys/systm.h>
38 38 #include <sys/cred.h>
39 39 #include <sys/proc.h>
40 40 #include <sys/user.h>
41 41 #include <sys/buf.h>
42 42 #include <sys/vfs.h>
43 43 #include <sys/vnode.h>
44 44 #include <sys/pathname.h>
45 45 #include <sys/uio.h>
46 46 #include <sys/file.h>
47 47 #include <sys/stat.h>
48 48 #include <sys/errno.h>
49 49 #include <sys/socket.h>
50 50 #include <sys/sysmacros.h>
51 51 #include <sys/siginfo.h>
52 52 #include <sys/tiuser.h>
53 53 #include <sys/statvfs.h>
54 54 #include <sys/stream.h>
55 55 #include <sys/strsun.h>
56 56 #include <sys/strsubr.h>
57 57 #include <sys/stropts.h>
58 58 #include <sys/timod.h>
59 59 #include <sys/t_kuser.h>
60 60 #include <sys/kmem.h>
61 61 #include <sys/kstat.h>
62 62 #include <sys/dirent.h>
63 63 #include <sys/cmn_err.h>
64 64 #include <sys/debug.h>
65 65 #include <sys/unistd.h>
66 66 #include <sys/vtrace.h>
67 67 #include <sys/mode.h>
68 68 #include <sys/acl.h>
69 69 #include <sys/sdt.h>
70 70
71 71 #include <rpc/types.h>
72 72 #include <rpc/auth.h>
73 73 #include <rpc/auth_unix.h>
74 74 #include <rpc/auth_des.h>
75 75 #include <rpc/svc.h>
76 76 #include <rpc/xdr.h>
77 77 #include <rpc/rpc_rdma.h>
78 78
79 79 #include <nfs/nfs.h>
80 80 #include <nfs/export.h>
81 81 #include <nfs/nfssys.h>
82 82 #include <nfs/nfs_clnt.h>
83 83 #include <nfs/nfs_acl.h>
84 84 #include <nfs/nfs_log.h>
85 85 #include <nfs/nfs_cmd.h>
86 86 #include <nfs/lm.h>
87 87 #include <nfs/nfs_dispatch.h>
88 88 #include <nfs/nfs4_drc.h>
89 89
90 90 #include <sys/modctl.h>
91 91 #include <sys/cladm.h>
92 92 #include <sys/clconf.h>
93 93
94 94 #include <sys/tsol/label.h>
95 95
96 96 #define MAXHOST 32
97 97 const char *kinet_ntop6(uchar_t *, char *, size_t);
↓ open down ↓ |
97 lines elided |
↑ open up ↑ |
98 98
99 99 /*
100 100 * Module linkage information.
101 101 */
102 102
103 103 static struct modlmisc modlmisc = {
104 104 &mod_miscops, "NFS server module"
105 105 };
106 106
107 107 static struct modlinkage modlinkage = {
108 - MODREV_1, (void *)&modlmisc, NULL
108 + MODREV_1, { (void *)&modlmisc, NULL }
109 109 };
110 110
111 111 kmem_cache_t *nfs_xuio_cache;
112 112 int nfs_loaned_buffers = 0;
113 113
114 114 int
115 115 _init(void)
116 116 {
117 117 int status;
118 118
119 119 if ((status = nfs_srvinit()) != 0) {
120 120 cmn_err(CE_WARN, "_init: nfs_srvinit failed");
121 121 return (status);
122 122 }
123 123
124 124 status = mod_install((struct modlinkage *)&modlinkage);
125 125 if (status != 0) {
126 126 /*
127 127 * Could not load module, cleanup previous
128 128 * initialization work.
129 129 */
130 130 nfs_srvfini();
131 131
132 132 return (status);
133 133 }
134 134
135 135 /*
136 136 * Initialise some placeholders for nfssys() calls. These have
137 137 * to be declared by the nfs module, since that handles nfssys()
138 138 * calls - also used by NFS clients - but are provided by this
139 139 * nfssrv module. These also then serve as confirmation to the
140 140 * relevant code in nfs that nfssrv has been loaded, as they're
141 141 * initially NULL.
142 142 */
143 143 nfs_srv_quiesce_func = nfs_srv_quiesce_all;
144 144 nfs_srv_dss_func = rfs4_dss_setpaths;
145 145
146 146 /* setup DSS paths here; must be done before initial server startup */
147 147 rfs4_dss_paths = rfs4_dss_oldpaths = NULL;
148 148
149 149 /* initialize the copy reduction caches */
150 150
151 151 nfs_xuio_cache = kmem_cache_create("nfs_xuio_cache",
152 152 sizeof (nfs_xuio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
153 153
154 154 return (status);
155 155 }
156 156
157 157 int
158 158 _fini()
159 159 {
160 160 return (EBUSY);
161 161 }
162 162
163 163 int
164 164 _info(struct modinfo *modinfop)
165 165 {
166 166 return (mod_info(&modlinkage, modinfop));
167 167 }
168 168
169 169 /*
170 170 * PUBLICFH_CHECK() checks if the dispatch routine supports
171 171 * RPC_PUBLICFH_OK, if the filesystem is exported public, and if the
172 172 * incoming request is using the public filehandle. The check duplicates
173 173 * the exportmatch() call done in checkexport(), and we should consider
174 174 * modifying those routines to avoid the duplication. For now, we optimize
175 175 * by calling exportmatch() only after checking that the dispatch routine
176 176 * supports RPC_PUBLICFH_OK, and if the filesystem is explicitly exported
177 177 * public (i.e., not the placeholder).
178 178 */
179 179 #define PUBLICFH_CHECK(disp, exi, fsid, xfid) \
180 180 ((disp->dis_flags & RPC_PUBLICFH_OK) && \
181 181 ((exi->exi_export.ex_flags & EX_PUBLIC) || \
182 182 (exi == exi_public && exportmatch(exi_root, \
183 183 fsid, xfid))))
184 184
185 185 static void nfs_srv_shutdown_all(int);
186 186 static void rfs4_server_start(int);
187 187 static void nullfree(void);
188 188 static void rfs_dispatch(struct svc_req *, SVCXPRT *);
189 189 static void acl_dispatch(struct svc_req *, SVCXPRT *);
190 190 static void common_dispatch(struct svc_req *, SVCXPRT *,
191 191 rpcvers_t, rpcvers_t, char *,
192 192 struct rpc_disptable *);
193 193 static void hanfsv4_failover(void);
194 194 static int checkauth(struct exportinfo *, struct svc_req *, cred_t *, int,
195 195 bool_t, bool_t *);
196 196 static char *client_name(struct svc_req *req);
197 197 static char *client_addr(struct svc_req *req, char *buf);
198 198 extern int sec_svc_getcred(struct svc_req *, cred_t *cr, char **, int *);
199 199 extern bool_t sec_svc_inrootlist(int, caddr_t, int, caddr_t *);
200 200
201 201 #define NFSLOG_COPY_NETBUF(exi, xprt, nb) { \
202 202 (nb)->maxlen = (xprt)->xp_rtaddr.maxlen; \
203 203 (nb)->len = (xprt)->xp_rtaddr.len; \
204 204 (nb)->buf = kmem_alloc((nb)->len, KM_SLEEP); \
205 205 bcopy((xprt)->xp_rtaddr.buf, (nb)->buf, (nb)->len); \
206 206 }
207 207
208 208 /*
209 209 * Public Filehandle common nfs routines
210 210 */
211 211 static int MCLpath(char **);
212 212 static void URLparse(char *);
213 213
214 214 /*
215 215 * NFS callout table.
216 216 * This table is used by svc_getreq() to dispatch a request with
217 217 * a given prog/vers pair to an appropriate service provider
218 218 * dispatch routine.
219 219 *
220 220 * NOTE: ordering is relied upon below when resetting the version min/max
221 221 * for NFS_PROGRAM. Careful, if this is ever changed.
222 222 */
223 223 static SVC_CALLOUT __nfs_sc_clts[] = {
224 224 { NFS_PROGRAM, NFS_VERSMIN, NFS_VERSMAX, rfs_dispatch },
225 225 { NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX, acl_dispatch }
226 226 };
227 227
228 228 static SVC_CALLOUT_TABLE nfs_sct_clts = {
229 229 sizeof (__nfs_sc_clts) / sizeof (__nfs_sc_clts[0]), FALSE,
230 230 __nfs_sc_clts
231 231 };
232 232
233 233 static SVC_CALLOUT __nfs_sc_cots[] = {
234 234 { NFS_PROGRAM, NFS_VERSMIN, NFS_VERSMAX, rfs_dispatch },
235 235 { NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX, acl_dispatch }
236 236 };
237 237
238 238 static SVC_CALLOUT_TABLE nfs_sct_cots = {
239 239 sizeof (__nfs_sc_cots) / sizeof (__nfs_sc_cots[0]), FALSE, __nfs_sc_cots
240 240 };
241 241
242 242 static SVC_CALLOUT __nfs_sc_rdma[] = {
243 243 { NFS_PROGRAM, NFS_VERSMIN, NFS_VERSMAX, rfs_dispatch },
244 244 { NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX, acl_dispatch }
245 245 };
246 246
247 247 static SVC_CALLOUT_TABLE nfs_sct_rdma = {
248 248 sizeof (__nfs_sc_rdma) / sizeof (__nfs_sc_rdma[0]), FALSE, __nfs_sc_rdma
249 249 };
250 250 rpcvers_t nfs_versmin = NFS_VERSMIN_DEFAULT;
251 251 rpcvers_t nfs_versmax = NFS_VERSMAX_DEFAULT;
252 252
253 253 /*
254 254 * Used to track the state of the server so that initialization
255 255 * can be done properly.
256 256 */
257 257 typedef enum {
258 258 NFS_SERVER_STOPPED, /* server state destroyed */
259 259 NFS_SERVER_STOPPING, /* server state being destroyed */
260 260 NFS_SERVER_RUNNING,
261 261 NFS_SERVER_QUIESCED, /* server state preserved */
262 262 NFS_SERVER_OFFLINE /* server pool offline */
263 263 } nfs_server_running_t;
264 264
265 265 static nfs_server_running_t nfs_server_upordown;
266 266 static kmutex_t nfs_server_upordown_lock;
267 267 static kcondvar_t nfs_server_upordown_cv;
268 268
269 269 /*
270 270 * DSS: distributed stable storage
271 271 * lists of all DSS paths: current, and before last warmstart
272 272 */
273 273 nvlist_t *rfs4_dss_paths, *rfs4_dss_oldpaths;
274 274
275 275 int rfs4_dispatch(struct rpcdisp *, struct svc_req *, SVCXPRT *, char *,
276 276 size_t *);
277 277 bool_t rfs4_minorvers_mismatch(struct svc_req *, SVCXPRT *, void *);
278 278
279 279 /*
280 280 * RDMA wait variables.
281 281 */
282 282 static kcondvar_t rdma_wait_cv;
283 283 static kmutex_t rdma_wait_mutex;
284 284
285 285 /*
286 286 * Will be called at the point the server pool is being unregistered
287 287 * from the pool list. From that point onwards, the pool is waiting
288 288 * to be drained and as such the server state is stale and pertains
289 289 * to the old instantiation of the NFS server pool.
290 290 */
291 291 void
292 292 nfs_srv_offline(void)
293 293 {
294 294 mutex_enter(&nfs_server_upordown_lock);
295 295 if (nfs_server_upordown == NFS_SERVER_RUNNING) {
296 296 nfs_server_upordown = NFS_SERVER_OFFLINE;
297 297 }
298 298 mutex_exit(&nfs_server_upordown_lock);
299 299 }
300 300
301 301 /*
302 302 * Will be called at the point the server pool is being destroyed so
303 303 * all transports have been closed and no service threads are in
304 304 * existence.
305 305 *
306 306 * If we quiesce the server, we're shutting it down without destroying the
307 307 * server state. This allows it to warm start subsequently.
308 308 */
309 309 void
310 310 nfs_srv_stop_all(void)
311 311 {
312 312 int quiesce = 0;
313 313 nfs_srv_shutdown_all(quiesce);
314 314 }
315 315
316 316 /*
317 317 * This alternative shutdown routine can be requested via nfssys()
318 318 */
319 319 void
320 320 nfs_srv_quiesce_all(void)
321 321 {
322 322 int quiesce = 1;
323 323 nfs_srv_shutdown_all(quiesce);
324 324 }
325 325
326 326 static void
327 327 nfs_srv_shutdown_all(int quiesce) {
328 328 mutex_enter(&nfs_server_upordown_lock);
329 329 if (quiesce) {
330 330 if (nfs_server_upordown == NFS_SERVER_RUNNING ||
331 331 nfs_server_upordown == NFS_SERVER_OFFLINE) {
332 332 nfs_server_upordown = NFS_SERVER_QUIESCED;
333 333 cv_signal(&nfs_server_upordown_cv);
334 334
335 335 /* reset DSS state, for subsequent warm restart */
336 336 rfs4_dss_numnewpaths = 0;
337 337 rfs4_dss_newpaths = NULL;
338 338
339 339 cmn_err(CE_NOTE, "nfs_server: server is now quiesced; "
340 340 "NFSv4 state has been preserved");
341 341 }
342 342 } else {
343 343 if (nfs_server_upordown == NFS_SERVER_OFFLINE) {
344 344 nfs_server_upordown = NFS_SERVER_STOPPING;
345 345 mutex_exit(&nfs_server_upordown_lock);
346 346 rfs4_state_fini();
347 347 rfs4_fini_drc(nfs4_drc);
348 348 mutex_enter(&nfs_server_upordown_lock);
349 349 nfs_server_upordown = NFS_SERVER_STOPPED;
350 350 cv_signal(&nfs_server_upordown_cv);
351 351 }
352 352 }
353 353 mutex_exit(&nfs_server_upordown_lock);
354 354 }
355 355
356 356 static int
357 357 nfs_srv_set_sc_versions(struct file *fp, SVC_CALLOUT_TABLE **sctpp,
358 358 rpcvers_t versmin, rpcvers_t versmax)
359 359 {
360 360 struct strioctl strioc;
361 361 struct T_info_ack tinfo;
362 362 int error, retval;
363 363
364 364 /*
365 365 * Find out what type of transport this is.
366 366 */
367 367 strioc.ic_cmd = TI_GETINFO;
368 368 strioc.ic_timout = -1;
369 369 strioc.ic_len = sizeof (tinfo);
370 370 strioc.ic_dp = (char *)&tinfo;
371 371 tinfo.PRIM_type = T_INFO_REQ;
372 372
373 373 error = strioctl(fp->f_vnode, I_STR, (intptr_t)&strioc, 0, K_TO_K,
374 374 CRED(), &retval);
375 375 if (error || retval)
376 376 return (error);
377 377
378 378 /*
379 379 * Based on our query of the transport type...
380 380 *
381 381 * Reset the min/max versions based on the caller's request
382 382 * NOTE: This assumes that NFS_PROGRAM is first in the array!!
383 383 * And the second entry is the NFS_ACL_PROGRAM.
384 384 */
385 385 switch (tinfo.SERV_type) {
386 386 case T_CLTS:
387 387 if (versmax == NFS_V4)
388 388 return (EINVAL);
389 389 __nfs_sc_clts[0].sc_versmin = versmin;
390 390 __nfs_sc_clts[0].sc_versmax = versmax;
391 391 __nfs_sc_clts[1].sc_versmin = versmin;
392 392 __nfs_sc_clts[1].sc_versmax = versmax;
393 393 *sctpp = &nfs_sct_clts;
394 394 break;
395 395 case T_COTS:
396 396 case T_COTS_ORD:
397 397 __nfs_sc_cots[0].sc_versmin = versmin;
398 398 __nfs_sc_cots[0].sc_versmax = versmax;
399 399 /* For the NFS_ACL program, check the max version */
400 400 if (versmax > NFS_ACL_VERSMAX)
401 401 versmax = NFS_ACL_VERSMAX;
402 402 __nfs_sc_cots[1].sc_versmin = versmin;
403 403 __nfs_sc_cots[1].sc_versmax = versmax;
404 404 *sctpp = &nfs_sct_cots;
405 405 break;
406 406 default:
407 407 error = EINVAL;
408 408 }
409 409
410 410 return (error);
411 411 }
412 412
413 413 /*
414 414 * NFS Server system call.
415 415 * Does all of the work of running a NFS server.
416 416 * uap->fd is the fd of an open transport provider
417 417 */
418 418 int
419 419 nfs_svc(struct nfs_svc_args *arg, model_t model)
420 420 {
421 421 file_t *fp;
422 422 SVCMASTERXPRT *xprt;
423 423 int error;
424 424 int readsize;
425 425 char buf[KNC_STRSIZE];
426 426 size_t len;
427 427 STRUCT_HANDLE(nfs_svc_args, uap);
428 428 struct netbuf addrmask;
429 429 SVC_CALLOUT_TABLE *sctp = NULL;
430 430
431 431 #ifdef lint
432 432 model = model; /* STRUCT macros don't always refer to it */
433 433 #endif
434 434
435 435 STRUCT_SET_HANDLE(uap, model, arg);
436 436
437 437 /* Check privileges in nfssys() */
438 438
439 439 if ((fp = getf(STRUCT_FGET(uap, fd))) == NULL)
440 440 return (EBADF);
441 441
442 442 /*
443 443 * Set read buffer size to rsize
444 444 * and add room for RPC headers.
445 445 */
446 446 readsize = nfs3tsize() + (RPC_MAXDATASIZE - NFS_MAXDATA);
447 447 if (readsize < RPC_MAXDATASIZE)
448 448 readsize = RPC_MAXDATASIZE;
449 449
450 450 error = copyinstr((const char *)STRUCT_FGETP(uap, netid), buf,
451 451 KNC_STRSIZE, &len);
452 452 if (error) {
453 453 releasef(STRUCT_FGET(uap, fd));
454 454 return (error);
455 455 }
456 456
457 457 addrmask.len = STRUCT_FGET(uap, addrmask.len);
458 458 addrmask.maxlen = STRUCT_FGET(uap, addrmask.maxlen);
459 459 addrmask.buf = kmem_alloc(addrmask.maxlen, KM_SLEEP);
460 460 error = copyin(STRUCT_FGETP(uap, addrmask.buf), addrmask.buf,
461 461 addrmask.len);
462 462 if (error) {
463 463 releasef(STRUCT_FGET(uap, fd));
464 464 kmem_free(addrmask.buf, addrmask.maxlen);
465 465 return (error);
466 466 }
467 467
468 468 nfs_versmin = STRUCT_FGET(uap, versmin);
469 469 nfs_versmax = STRUCT_FGET(uap, versmax);
470 470
471 471 /* Double check the vers min/max ranges */
472 472 if ((nfs_versmin > nfs_versmax) ||
473 473 (nfs_versmin < NFS_VERSMIN) ||
474 474 (nfs_versmax > NFS_VERSMAX)) {
475 475 nfs_versmin = NFS_VERSMIN_DEFAULT;
476 476 nfs_versmax = NFS_VERSMAX_DEFAULT;
477 477 }
478 478
479 479 if (error =
480 480 nfs_srv_set_sc_versions(fp, &sctp, nfs_versmin, nfs_versmax)) {
481 481 releasef(STRUCT_FGET(uap, fd));
482 482 kmem_free(addrmask.buf, addrmask.maxlen);
483 483 return (error);
484 484 }
485 485
486 486 /* Initialize nfsv4 server */
487 487 if (nfs_versmax == (rpcvers_t)NFS_V4)
488 488 rfs4_server_start(STRUCT_FGET(uap, delegation));
489 489
490 490 /* Create a transport handle. */
491 491 error = svc_tli_kcreate(fp, readsize, buf, &addrmask, &xprt,
492 492 sctp, NULL, NFS_SVCPOOL_ID, TRUE);
493 493
494 494 if (error)
495 495 kmem_free(addrmask.buf, addrmask.maxlen);
496 496
497 497 releasef(STRUCT_FGET(uap, fd));
498 498
499 499 /* HA-NFSv4: save the cluster nodeid */
500 500 if (cluster_bootflags & CLUSTER_BOOTED)
501 501 lm_global_nlmid = clconf_get_nodeid();
502 502
503 503 return (error);
504 504 }
505 505
506 506 static void
507 507 rfs4_server_start(int nfs4_srv_delegation)
508 508 {
509 509 /*
510 510 * Determine if the server has previously been "started" and
511 511 * if not, do the per instance initialization
512 512 */
513 513 mutex_enter(&nfs_server_upordown_lock);
514 514
515 515 if (nfs_server_upordown != NFS_SERVER_RUNNING) {
516 516 /* Do we need to stop and wait on the previous server? */
517 517 while (nfs_server_upordown == NFS_SERVER_STOPPING ||
518 518 nfs_server_upordown == NFS_SERVER_OFFLINE)
519 519 cv_wait(&nfs_server_upordown_cv,
520 520 &nfs_server_upordown_lock);
521 521
522 522 if (nfs_server_upordown != NFS_SERVER_RUNNING) {
523 523 (void) svc_pool_control(NFS_SVCPOOL_ID,
524 524 SVCPSET_UNREGISTER_PROC, (void *)&nfs_srv_offline);
525 525 (void) svc_pool_control(NFS_SVCPOOL_ID,
526 526 SVCPSET_SHUTDOWN_PROC, (void *)&nfs_srv_stop_all);
527 527
528 528 /* is this an nfsd warm start? */
529 529 if (nfs_server_upordown == NFS_SERVER_QUIESCED) {
530 530 cmn_err(CE_NOTE, "nfs_server: "
531 531 "server was previously quiesced; "
532 532 "existing NFSv4 state will be re-used");
533 533
534 534 /*
535 535 * HA-NFSv4: this is also the signal
536 536 * that a Resource Group failover has
537 537 * occurred.
538 538 */
539 539 if (cluster_bootflags & CLUSTER_BOOTED)
540 540 hanfsv4_failover();
541 541 } else {
542 542 /* cold start */
543 543 rfs4_state_init();
544 544 nfs4_drc = rfs4_init_drc(nfs4_drc_max,
545 545 nfs4_drc_hash);
546 546 }
547 547
548 548 /*
549 549 * Check to see if delegation is to be
550 550 * enabled at the server
551 551 */
552 552 if (nfs4_srv_delegation != FALSE)
553 553 rfs4_set_deleg_policy(SRV_NORMAL_DELEGATE);
554 554
555 555 nfs_server_upordown = NFS_SERVER_RUNNING;
556 556 }
557 557 cv_signal(&nfs_server_upordown_cv);
558 558 }
559 559 mutex_exit(&nfs_server_upordown_lock);
560 560 }
561 561
562 562 /*
563 563 * If RDMA device available,
564 564 * start RDMA listener.
565 565 */
566 566 int
567 567 rdma_start(struct rdma_svc_args *rsa)
568 568 {
569 569 int error;
570 570 rdma_xprt_group_t started_rdma_xprts;
571 571 rdma_stat stat;
572 572 int svc_state = 0;
573 573
574 574 /* Double check the vers min/max ranges */
575 575 if ((rsa->nfs_versmin > rsa->nfs_versmax) ||
576 576 (rsa->nfs_versmin < NFS_VERSMIN) ||
577 577 (rsa->nfs_versmax > NFS_VERSMAX)) {
578 578 rsa->nfs_versmin = NFS_VERSMIN_DEFAULT;
579 579 rsa->nfs_versmax = NFS_VERSMAX_DEFAULT;
580 580 }
581 581 nfs_versmin = rsa->nfs_versmin;
582 582 nfs_versmax = rsa->nfs_versmax;
583 583
584 584 /* Set the versions in the callout table */
585 585 __nfs_sc_rdma[0].sc_versmin = rsa->nfs_versmin;
586 586 __nfs_sc_rdma[0].sc_versmax = rsa->nfs_versmax;
587 587 /* For the NFS_ACL program, check the max version */
588 588 __nfs_sc_rdma[1].sc_versmin = rsa->nfs_versmin;
589 589 if (rsa->nfs_versmax > NFS_ACL_VERSMAX)
590 590 __nfs_sc_rdma[1].sc_versmax = NFS_ACL_VERSMAX;
591 591 else
592 592 __nfs_sc_rdma[1].sc_versmax = rsa->nfs_versmax;
593 593
594 594 /* Initialize nfsv4 server */
595 595 if (rsa->nfs_versmax == (rpcvers_t)NFS_V4)
596 596 rfs4_server_start(rsa->delegation);
597 597
598 598 started_rdma_xprts.rtg_count = 0;
599 599 started_rdma_xprts.rtg_listhead = NULL;
600 600 started_rdma_xprts.rtg_poolid = rsa->poolid;
601 601
602 602 restart:
603 603 error = svc_rdma_kcreate(rsa->netid, &nfs_sct_rdma, rsa->poolid,
604 604 &started_rdma_xprts);
605 605
606 606 svc_state = !error;
607 607
608 608 while (!error) {
609 609
610 610 /*
611 611 * wait till either interrupted by a signal on
612 612 * nfs service stop/restart or signalled by a
613 613 * rdma plugin attach/detatch.
614 614 */
615 615
616 616 stat = rdma_kwait();
617 617
618 618 /*
619 619 * stop services if running -- either on a HCA detach event
620 620 * or if the nfs service is stopped/restarted.
621 621 */
622 622
623 623 if ((stat == RDMA_HCA_DETACH || stat == RDMA_INTR) &&
624 624 svc_state) {
625 625 rdma_stop(&started_rdma_xprts);
626 626 svc_state = 0;
627 627 }
628 628
629 629 /*
630 630 * nfs service stop/restart, break out of the
631 631 * wait loop and return;
632 632 */
633 633 if (stat == RDMA_INTR)
634 634 return (0);
635 635
636 636 /*
637 637 * restart stopped services on a HCA attach event
638 638 * (if not already running)
639 639 */
640 640
641 641 if ((stat == RDMA_HCA_ATTACH) && (svc_state == 0))
642 642 goto restart;
643 643
644 644 /*
645 645 * loop until a nfs service stop/restart
646 646 */
647 647 }
648 648
649 649 return (error);
650 650 }
651 651
652 652 /* ARGSUSED */
653 653 void
654 654 rpc_null(caddr_t *argp, caddr_t *resp, struct exportinfo *exi,
655 655 struct svc_req *req, cred_t *cr, bool_t ro)
656 656 {
657 657 }
658 658
659 659 /* ARGSUSED */
660 660 void
661 661 rpc_null_v3(caddr_t *argp, caddr_t *resp, struct exportinfo *exi,
662 662 struct svc_req *req, cred_t *cr, bool_t ro)
663 663 {
664 664 DTRACE_NFSV3_3(op__null__start, struct svc_req *, req,
665 665 cred_t *, cr, vnode_t *, NULL);
666 666 DTRACE_NFSV3_3(op__null__done, struct svc_req *, req,
667 667 cred_t *, cr, vnode_t *, NULL);
668 668 }
669 669
670 670 /* ARGSUSED */
671 671 static void
672 672 rfs_error(caddr_t *argp, caddr_t *resp, struct exportinfo *exi,
673 673 struct svc_req *req, cred_t *cr, bool_t ro)
674 674 {
675 675 /* return (EOPNOTSUPP); */
676 676 }
677 677
678 678 static void
679 679 nullfree(void)
680 680 {
681 681 }
682 682
683 683 static char *rfscallnames_v2[] = {
684 684 "RFS2_NULL",
685 685 "RFS2_GETATTR",
686 686 "RFS2_SETATTR",
687 687 "RFS2_ROOT",
688 688 "RFS2_LOOKUP",
689 689 "RFS2_READLINK",
690 690 "RFS2_READ",
691 691 "RFS2_WRITECACHE",
692 692 "RFS2_WRITE",
693 693 "RFS2_CREATE",
694 694 "RFS2_REMOVE",
695 695 "RFS2_RENAME",
696 696 "RFS2_LINK",
697 697 "RFS2_SYMLINK",
698 698 "RFS2_MKDIR",
699 699 "RFS2_RMDIR",
700 700 "RFS2_READDIR",
701 701 "RFS2_STATFS"
702 702 };
703 703
704 704 static struct rpcdisp rfsdisptab_v2[] = {
705 705 /*
706 706 * NFS VERSION 2
707 707 */
708 708
709 709 /* RFS_NULL = 0 */
710 710 {rpc_null,
711 711 xdr_void, NULL_xdrproc_t, 0,
712 712 xdr_void, NULL_xdrproc_t, 0,
713 713 nullfree, RPC_IDEMPOTENT,
714 714 0},
715 715
716 716 /* RFS_GETATTR = 1 */
717 717 {rfs_getattr,
718 718 xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
719 719 xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
720 720 nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
721 721 rfs_getattr_getfh},
722 722
723 723 /* RFS_SETATTR = 2 */
724 724 {rfs_setattr,
725 725 xdr_saargs, NULL_xdrproc_t, sizeof (struct nfssaargs),
726 726 xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
727 727 nullfree, RPC_MAPRESP,
728 728 rfs_setattr_getfh},
729 729
730 730 /* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
731 731 {rfs_error,
732 732 xdr_void, NULL_xdrproc_t, 0,
733 733 xdr_void, NULL_xdrproc_t, 0,
734 734 nullfree, RPC_IDEMPOTENT,
735 735 0},
736 736
737 737 /* RFS_LOOKUP = 4 */
738 738 {rfs_lookup,
739 739 xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
740 740 xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
741 741 nullfree, RPC_IDEMPOTENT|RPC_MAPRESP|RPC_PUBLICFH_OK,
742 742 rfs_lookup_getfh},
743 743
744 744 /* RFS_READLINK = 5 */
745 745 {rfs_readlink,
746 746 xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
747 747 xdr_rdlnres, NULL_xdrproc_t, sizeof (struct nfsrdlnres),
748 748 rfs_rlfree, RPC_IDEMPOTENT,
749 749 rfs_readlink_getfh},
750 750
751 751 /* RFS_READ = 6 */
752 752 {rfs_read,
753 753 xdr_readargs, NULL_xdrproc_t, sizeof (struct nfsreadargs),
754 754 xdr_rdresult, NULL_xdrproc_t, sizeof (struct nfsrdresult),
755 755 rfs_rdfree, RPC_IDEMPOTENT,
756 756 rfs_read_getfh},
757 757
758 758 /* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
759 759 {rfs_error,
760 760 xdr_void, NULL_xdrproc_t, 0,
761 761 xdr_void, NULL_xdrproc_t, 0,
762 762 nullfree, RPC_IDEMPOTENT,
763 763 0},
764 764
765 765 /* RFS_WRITE = 8 */
766 766 {rfs_write,
767 767 xdr_writeargs, NULL_xdrproc_t, sizeof (struct nfswriteargs),
768 768 xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
769 769 nullfree, RPC_MAPRESP,
770 770 rfs_write_getfh},
771 771
772 772 /* RFS_CREATE = 9 */
773 773 {rfs_create,
774 774 xdr_creatargs, NULL_xdrproc_t, sizeof (struct nfscreatargs),
775 775 xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
776 776 nullfree, RPC_MAPRESP,
777 777 rfs_create_getfh},
778 778
779 779 /* RFS_REMOVE = 10 */
780 780 {rfs_remove,
781 781 xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
782 782 #ifdef _LITTLE_ENDIAN
783 783 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
784 784 #else
785 785 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
786 786 #endif
787 787 nullfree, RPC_MAPRESP,
788 788 rfs_remove_getfh},
789 789
790 790 /* RFS_RENAME = 11 */
791 791 {rfs_rename,
792 792 xdr_rnmargs, NULL_xdrproc_t, sizeof (struct nfsrnmargs),
793 793 #ifdef _LITTLE_ENDIAN
794 794 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
795 795 #else
796 796 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
797 797 #endif
798 798 nullfree, RPC_MAPRESP,
799 799 rfs_rename_getfh},
800 800
801 801 /* RFS_LINK = 12 */
802 802 {rfs_link,
803 803 xdr_linkargs, NULL_xdrproc_t, sizeof (struct nfslinkargs),
804 804 #ifdef _LITTLE_ENDIAN
805 805 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
806 806 #else
807 807 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
808 808 #endif
809 809 nullfree, RPC_MAPRESP,
810 810 rfs_link_getfh},
811 811
812 812 /* RFS_SYMLINK = 13 */
813 813 {rfs_symlink,
814 814 xdr_slargs, NULL_xdrproc_t, sizeof (struct nfsslargs),
815 815 #ifdef _LITTLE_ENDIAN
816 816 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
817 817 #else
818 818 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
819 819 #endif
820 820 nullfree, RPC_MAPRESP,
821 821 rfs_symlink_getfh},
822 822
823 823 /* RFS_MKDIR = 14 */
824 824 {rfs_mkdir,
825 825 xdr_creatargs, NULL_xdrproc_t, sizeof (struct nfscreatargs),
826 826 xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
827 827 nullfree, RPC_MAPRESP,
828 828 rfs_mkdir_getfh},
829 829
830 830 /* RFS_RMDIR = 15 */
831 831 {rfs_rmdir,
832 832 xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
833 833 #ifdef _LITTLE_ENDIAN
834 834 xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
835 835 #else
836 836 xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
837 837 #endif
838 838 nullfree, RPC_MAPRESP,
839 839 rfs_rmdir_getfh},
840 840
841 841 /* RFS_READDIR = 16 */
842 842 {rfs_readdir,
843 843 xdr_rddirargs, NULL_xdrproc_t, sizeof (struct nfsrddirargs),
844 844 xdr_putrddirres, NULL_xdrproc_t, sizeof (struct nfsrddirres),
845 845 rfs_rddirfree, RPC_IDEMPOTENT,
846 846 rfs_readdir_getfh},
847 847
848 848 /* RFS_STATFS = 17 */
849 849 {rfs_statfs,
850 850 xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
851 851 xdr_statfs, xdr_faststatfs, sizeof (struct nfsstatfs),
852 852 nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
853 853 rfs_statfs_getfh},
854 854 };
855 855
856 856 static char *rfscallnames_v3[] = {
857 857 "RFS3_NULL",
858 858 "RFS3_GETATTR",
859 859 "RFS3_SETATTR",
860 860 "RFS3_LOOKUP",
861 861 "RFS3_ACCESS",
862 862 "RFS3_READLINK",
863 863 "RFS3_READ",
864 864 "RFS3_WRITE",
865 865 "RFS3_CREATE",
866 866 "RFS3_MKDIR",
867 867 "RFS3_SYMLINK",
868 868 "RFS3_MKNOD",
869 869 "RFS3_REMOVE",
870 870 "RFS3_RMDIR",
871 871 "RFS3_RENAME",
872 872 "RFS3_LINK",
873 873 "RFS3_READDIR",
874 874 "RFS3_READDIRPLUS",
875 875 "RFS3_FSSTAT",
876 876 "RFS3_FSINFO",
877 877 "RFS3_PATHCONF",
878 878 "RFS3_COMMIT"
879 879 };
880 880
881 881 static struct rpcdisp rfsdisptab_v3[] = {
882 882 /*
883 883 * NFS VERSION 3
884 884 */
885 885
886 886 /* RFS_NULL = 0 */
887 887 {rpc_null_v3,
888 888 xdr_void, NULL_xdrproc_t, 0,
889 889 xdr_void, NULL_xdrproc_t, 0,
890 890 nullfree, RPC_IDEMPOTENT,
891 891 0},
892 892
893 893 /* RFS3_GETATTR = 1 */
894 894 {rfs3_getattr,
895 895 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (GETATTR3args),
896 896 xdr_GETATTR3res, NULL_xdrproc_t, sizeof (GETATTR3res),
897 897 nullfree, (RPC_IDEMPOTENT | RPC_ALLOWANON),
898 898 rfs3_getattr_getfh},
899 899
900 900 /* RFS3_SETATTR = 2 */
901 901 {rfs3_setattr,
902 902 xdr_SETATTR3args, NULL_xdrproc_t, sizeof (SETATTR3args),
903 903 xdr_SETATTR3res, NULL_xdrproc_t, sizeof (SETATTR3res),
904 904 nullfree, 0,
905 905 rfs3_setattr_getfh},
906 906
907 907 /* RFS3_LOOKUP = 3 */
908 908 {rfs3_lookup,
909 909 xdr_diropargs3, NULL_xdrproc_t, sizeof (LOOKUP3args),
910 910 xdr_LOOKUP3res, NULL_xdrproc_t, sizeof (LOOKUP3res),
911 911 nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK),
912 912 rfs3_lookup_getfh},
913 913
914 914 /* RFS3_ACCESS = 4 */
915 915 {rfs3_access,
916 916 xdr_ACCESS3args, NULL_xdrproc_t, sizeof (ACCESS3args),
917 917 xdr_ACCESS3res, NULL_xdrproc_t, sizeof (ACCESS3res),
918 918 nullfree, RPC_IDEMPOTENT,
919 919 rfs3_access_getfh},
920 920
921 921 /* RFS3_READLINK = 5 */
922 922 {rfs3_readlink,
923 923 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (READLINK3args),
924 924 xdr_READLINK3res, NULL_xdrproc_t, sizeof (READLINK3res),
925 925 rfs3_readlink_free, RPC_IDEMPOTENT,
926 926 rfs3_readlink_getfh},
927 927
928 928 /* RFS3_READ = 6 */
929 929 {rfs3_read,
930 930 xdr_READ3args, NULL_xdrproc_t, sizeof (READ3args),
931 931 xdr_READ3res, NULL_xdrproc_t, sizeof (READ3res),
932 932 rfs3_read_free, RPC_IDEMPOTENT,
933 933 rfs3_read_getfh},
934 934
935 935 /* RFS3_WRITE = 7 */
936 936 {rfs3_write,
937 937 xdr_WRITE3args, NULL_xdrproc_t, sizeof (WRITE3args),
938 938 xdr_WRITE3res, NULL_xdrproc_t, sizeof (WRITE3res),
939 939 nullfree, 0,
940 940 rfs3_write_getfh},
941 941
942 942 /* RFS3_CREATE = 8 */
943 943 {rfs3_create,
944 944 xdr_CREATE3args, NULL_xdrproc_t, sizeof (CREATE3args),
945 945 xdr_CREATE3res, NULL_xdrproc_t, sizeof (CREATE3res),
946 946 nullfree, 0,
947 947 rfs3_create_getfh},
948 948
949 949 /* RFS3_MKDIR = 9 */
950 950 {rfs3_mkdir,
951 951 xdr_MKDIR3args, NULL_xdrproc_t, sizeof (MKDIR3args),
952 952 xdr_MKDIR3res, NULL_xdrproc_t, sizeof (MKDIR3res),
953 953 nullfree, 0,
954 954 rfs3_mkdir_getfh},
955 955
956 956 /* RFS3_SYMLINK = 10 */
957 957 {rfs3_symlink,
958 958 xdr_SYMLINK3args, NULL_xdrproc_t, sizeof (SYMLINK3args),
959 959 xdr_SYMLINK3res, NULL_xdrproc_t, sizeof (SYMLINK3res),
960 960 nullfree, 0,
961 961 rfs3_symlink_getfh},
962 962
963 963 /* RFS3_MKNOD = 11 */
964 964 {rfs3_mknod,
965 965 xdr_MKNOD3args, NULL_xdrproc_t, sizeof (MKNOD3args),
966 966 xdr_MKNOD3res, NULL_xdrproc_t, sizeof (MKNOD3res),
967 967 nullfree, 0,
968 968 rfs3_mknod_getfh},
969 969
970 970 /* RFS3_REMOVE = 12 */
971 971 {rfs3_remove,
972 972 xdr_diropargs3, NULL_xdrproc_t, sizeof (REMOVE3args),
973 973 xdr_REMOVE3res, NULL_xdrproc_t, sizeof (REMOVE3res),
974 974 nullfree, 0,
975 975 rfs3_remove_getfh},
976 976
977 977 /* RFS3_RMDIR = 13 */
978 978 {rfs3_rmdir,
979 979 xdr_diropargs3, NULL_xdrproc_t, sizeof (RMDIR3args),
980 980 xdr_RMDIR3res, NULL_xdrproc_t, sizeof (RMDIR3res),
981 981 nullfree, 0,
982 982 rfs3_rmdir_getfh},
983 983
984 984 /* RFS3_RENAME = 14 */
985 985 {rfs3_rename,
986 986 xdr_RENAME3args, NULL_xdrproc_t, sizeof (RENAME3args),
987 987 xdr_RENAME3res, NULL_xdrproc_t, sizeof (RENAME3res),
988 988 nullfree, 0,
989 989 rfs3_rename_getfh},
990 990
991 991 /* RFS3_LINK = 15 */
992 992 {rfs3_link,
993 993 xdr_LINK3args, NULL_xdrproc_t, sizeof (LINK3args),
994 994 xdr_LINK3res, NULL_xdrproc_t, sizeof (LINK3res),
995 995 nullfree, 0,
996 996 rfs3_link_getfh},
997 997
998 998 /* RFS3_READDIR = 16 */
999 999 {rfs3_readdir,
1000 1000 xdr_READDIR3args, NULL_xdrproc_t, sizeof (READDIR3args),
1001 1001 xdr_READDIR3res, NULL_xdrproc_t, sizeof (READDIR3res),
1002 1002 rfs3_readdir_free, RPC_IDEMPOTENT,
1003 1003 rfs3_readdir_getfh},
1004 1004
1005 1005 /* RFS3_READDIRPLUS = 17 */
1006 1006 {rfs3_readdirplus,
1007 1007 xdr_READDIRPLUS3args, NULL_xdrproc_t, sizeof (READDIRPLUS3args),
1008 1008 xdr_READDIRPLUS3res, NULL_xdrproc_t, sizeof (READDIRPLUS3res),
1009 1009 rfs3_readdirplus_free, RPC_AVOIDWORK,
1010 1010 rfs3_readdirplus_getfh},
1011 1011
1012 1012 /* RFS3_FSSTAT = 18 */
1013 1013 {rfs3_fsstat,
1014 1014 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (FSSTAT3args),
1015 1015 xdr_FSSTAT3res, NULL_xdrproc_t, sizeof (FSSTAT3res),
1016 1016 nullfree, RPC_IDEMPOTENT,
1017 1017 rfs3_fsstat_getfh},
1018 1018
1019 1019 /* RFS3_FSINFO = 19 */
1020 1020 {rfs3_fsinfo,
1021 1021 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (FSINFO3args),
1022 1022 xdr_FSINFO3res, NULL_xdrproc_t, sizeof (FSINFO3res),
1023 1023 nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON,
1024 1024 rfs3_fsinfo_getfh},
1025 1025
1026 1026 /* RFS3_PATHCONF = 20 */
1027 1027 {rfs3_pathconf,
1028 1028 xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (PATHCONF3args),
1029 1029 xdr_PATHCONF3res, NULL_xdrproc_t, sizeof (PATHCONF3res),
1030 1030 nullfree, RPC_IDEMPOTENT,
1031 1031 rfs3_pathconf_getfh},
1032 1032
1033 1033 /* RFS3_COMMIT = 21 */
1034 1034 {rfs3_commit,
1035 1035 xdr_COMMIT3args, NULL_xdrproc_t, sizeof (COMMIT3args),
1036 1036 xdr_COMMIT3res, NULL_xdrproc_t, sizeof (COMMIT3res),
1037 1037 nullfree, RPC_IDEMPOTENT,
1038 1038 rfs3_commit_getfh},
1039 1039 };
1040 1040
1041 1041 static char *rfscallnames_v4[] = {
1042 1042 "RFS4_NULL",
1043 1043 "RFS4_COMPOUND",
1044 1044 "RFS4_NULL",
1045 1045 "RFS4_NULL",
1046 1046 "RFS4_NULL",
1047 1047 "RFS4_NULL",
1048 1048 "RFS4_NULL",
1049 1049 "RFS4_NULL",
1050 1050 "RFS4_CREATE"
1051 1051 };
1052 1052
1053 1053 static struct rpcdisp rfsdisptab_v4[] = {
1054 1054 /*
1055 1055 * NFS VERSION 4
1056 1056 */
1057 1057
1058 1058 /* RFS_NULL = 0 */
1059 1059 {rpc_null,
1060 1060 xdr_void, NULL_xdrproc_t, 0,
1061 1061 xdr_void, NULL_xdrproc_t, 0,
1062 1062 nullfree, RPC_IDEMPOTENT, 0},
1063 1063
1064 1064 /* RFS4_compound = 1 */
1065 1065 {rfs4_compound,
1066 1066 xdr_COMPOUND4args_srv, NULL_xdrproc_t, sizeof (COMPOUND4args),
1067 1067 xdr_COMPOUND4res_srv, NULL_xdrproc_t, sizeof (COMPOUND4res),
1068 1068 rfs4_compound_free, 0, 0},
1069 1069 };
1070 1070
1071 1071 union rfs_args {
1072 1072 /*
1073 1073 * NFS VERSION 2
1074 1074 */
1075 1075
1076 1076 /* RFS_NULL = 0 */
1077 1077
1078 1078 /* RFS_GETATTR = 1 */
1079 1079 fhandle_t nfs2_getattr_args;
1080 1080
1081 1081 /* RFS_SETATTR = 2 */
1082 1082 struct nfssaargs nfs2_setattr_args;
1083 1083
1084 1084 /* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
1085 1085
1086 1086 /* RFS_LOOKUP = 4 */
1087 1087 struct nfsdiropargs nfs2_lookup_args;
1088 1088
1089 1089 /* RFS_READLINK = 5 */
1090 1090 fhandle_t nfs2_readlink_args;
1091 1091
1092 1092 /* RFS_READ = 6 */
1093 1093 struct nfsreadargs nfs2_read_args;
1094 1094
1095 1095 /* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
1096 1096
1097 1097 /* RFS_WRITE = 8 */
1098 1098 struct nfswriteargs nfs2_write_args;
1099 1099
1100 1100 /* RFS_CREATE = 9 */
1101 1101 struct nfscreatargs nfs2_create_args;
1102 1102
1103 1103 /* RFS_REMOVE = 10 */
1104 1104 struct nfsdiropargs nfs2_remove_args;
1105 1105
1106 1106 /* RFS_RENAME = 11 */
1107 1107 struct nfsrnmargs nfs2_rename_args;
1108 1108
1109 1109 /* RFS_LINK = 12 */
1110 1110 struct nfslinkargs nfs2_link_args;
1111 1111
1112 1112 /* RFS_SYMLINK = 13 */
1113 1113 struct nfsslargs nfs2_symlink_args;
1114 1114
1115 1115 /* RFS_MKDIR = 14 */
1116 1116 struct nfscreatargs nfs2_mkdir_args;
1117 1117
1118 1118 /* RFS_RMDIR = 15 */
1119 1119 struct nfsdiropargs nfs2_rmdir_args;
1120 1120
1121 1121 /* RFS_READDIR = 16 */
1122 1122 struct nfsrddirargs nfs2_readdir_args;
1123 1123
1124 1124 /* RFS_STATFS = 17 */
1125 1125 fhandle_t nfs2_statfs_args;
1126 1126
1127 1127 /*
1128 1128 * NFS VERSION 3
1129 1129 */
1130 1130
1131 1131 /* RFS_NULL = 0 */
1132 1132
1133 1133 /* RFS3_GETATTR = 1 */
1134 1134 GETATTR3args nfs3_getattr_args;
1135 1135
1136 1136 /* RFS3_SETATTR = 2 */
1137 1137 SETATTR3args nfs3_setattr_args;
1138 1138
1139 1139 /* RFS3_LOOKUP = 3 */
1140 1140 LOOKUP3args nfs3_lookup_args;
1141 1141
1142 1142 /* RFS3_ACCESS = 4 */
1143 1143 ACCESS3args nfs3_access_args;
1144 1144
1145 1145 /* RFS3_READLINK = 5 */
1146 1146 READLINK3args nfs3_readlink_args;
1147 1147
1148 1148 /* RFS3_READ = 6 */
1149 1149 READ3args nfs3_read_args;
1150 1150
1151 1151 /* RFS3_WRITE = 7 */
1152 1152 WRITE3args nfs3_write_args;
1153 1153
1154 1154 /* RFS3_CREATE = 8 */
1155 1155 CREATE3args nfs3_create_args;
1156 1156
1157 1157 /* RFS3_MKDIR = 9 */
1158 1158 MKDIR3args nfs3_mkdir_args;
1159 1159
1160 1160 /* RFS3_SYMLINK = 10 */
1161 1161 SYMLINK3args nfs3_symlink_args;
1162 1162
1163 1163 /* RFS3_MKNOD = 11 */
1164 1164 MKNOD3args nfs3_mknod_args;
1165 1165
1166 1166 /* RFS3_REMOVE = 12 */
1167 1167 REMOVE3args nfs3_remove_args;
1168 1168
1169 1169 /* RFS3_RMDIR = 13 */
1170 1170 RMDIR3args nfs3_rmdir_args;
1171 1171
1172 1172 /* RFS3_RENAME = 14 */
1173 1173 RENAME3args nfs3_rename_args;
1174 1174
1175 1175 /* RFS3_LINK = 15 */
1176 1176 LINK3args nfs3_link_args;
1177 1177
1178 1178 /* RFS3_READDIR = 16 */
1179 1179 READDIR3args nfs3_readdir_args;
1180 1180
1181 1181 /* RFS3_READDIRPLUS = 17 */
1182 1182 READDIRPLUS3args nfs3_readdirplus_args;
1183 1183
1184 1184 /* RFS3_FSSTAT = 18 */
1185 1185 FSSTAT3args nfs3_fsstat_args;
1186 1186
1187 1187 /* RFS3_FSINFO = 19 */
1188 1188 FSINFO3args nfs3_fsinfo_args;
1189 1189
1190 1190 /* RFS3_PATHCONF = 20 */
1191 1191 PATHCONF3args nfs3_pathconf_args;
1192 1192
1193 1193 /* RFS3_COMMIT = 21 */
1194 1194 COMMIT3args nfs3_commit_args;
1195 1195
1196 1196 /*
1197 1197 * NFS VERSION 4
1198 1198 */
1199 1199
1200 1200 /* RFS_NULL = 0 */
1201 1201
1202 1202 /* COMPUND = 1 */
1203 1203 COMPOUND4args nfs4_compound_args;
1204 1204 };
1205 1205
1206 1206 union rfs_res {
1207 1207 /*
1208 1208 * NFS VERSION 2
1209 1209 */
1210 1210
1211 1211 /* RFS_NULL = 0 */
1212 1212
1213 1213 /* RFS_GETATTR = 1 */
1214 1214 struct nfsattrstat nfs2_getattr_res;
1215 1215
1216 1216 /* RFS_SETATTR = 2 */
1217 1217 struct nfsattrstat nfs2_setattr_res;
1218 1218
1219 1219 /* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
1220 1220
1221 1221 /* RFS_LOOKUP = 4 */
1222 1222 struct nfsdiropres nfs2_lookup_res;
1223 1223
1224 1224 /* RFS_READLINK = 5 */
1225 1225 struct nfsrdlnres nfs2_readlink_res;
1226 1226
1227 1227 /* RFS_READ = 6 */
1228 1228 struct nfsrdresult nfs2_read_res;
1229 1229
1230 1230 /* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
1231 1231
1232 1232 /* RFS_WRITE = 8 */
1233 1233 struct nfsattrstat nfs2_write_res;
1234 1234
1235 1235 /* RFS_CREATE = 9 */
1236 1236 struct nfsdiropres nfs2_create_res;
1237 1237
1238 1238 /* RFS_REMOVE = 10 */
1239 1239 enum nfsstat nfs2_remove_res;
1240 1240
1241 1241 /* RFS_RENAME = 11 */
1242 1242 enum nfsstat nfs2_rename_res;
1243 1243
1244 1244 /* RFS_LINK = 12 */
1245 1245 enum nfsstat nfs2_link_res;
1246 1246
1247 1247 /* RFS_SYMLINK = 13 */
1248 1248 enum nfsstat nfs2_symlink_res;
1249 1249
1250 1250 /* RFS_MKDIR = 14 */
1251 1251 struct nfsdiropres nfs2_mkdir_res;
1252 1252
1253 1253 /* RFS_RMDIR = 15 */
1254 1254 enum nfsstat nfs2_rmdir_res;
1255 1255
1256 1256 /* RFS_READDIR = 16 */
1257 1257 struct nfsrddirres nfs2_readdir_res;
1258 1258
1259 1259 /* RFS_STATFS = 17 */
1260 1260 struct nfsstatfs nfs2_statfs_res;
1261 1261
1262 1262 /*
1263 1263 * NFS VERSION 3
1264 1264 */
1265 1265
1266 1266 /* RFS_NULL = 0 */
1267 1267
1268 1268 /* RFS3_GETATTR = 1 */
1269 1269 GETATTR3res nfs3_getattr_res;
1270 1270
1271 1271 /* RFS3_SETATTR = 2 */
1272 1272 SETATTR3res nfs3_setattr_res;
1273 1273
1274 1274 /* RFS3_LOOKUP = 3 */
1275 1275 LOOKUP3res nfs3_lookup_res;
1276 1276
1277 1277 /* RFS3_ACCESS = 4 */
1278 1278 ACCESS3res nfs3_access_res;
1279 1279
1280 1280 /* RFS3_READLINK = 5 */
1281 1281 READLINK3res nfs3_readlink_res;
1282 1282
1283 1283 /* RFS3_READ = 6 */
1284 1284 READ3res nfs3_read_res;
1285 1285
1286 1286 /* RFS3_WRITE = 7 */
1287 1287 WRITE3res nfs3_write_res;
1288 1288
1289 1289 /* RFS3_CREATE = 8 */
1290 1290 CREATE3res nfs3_create_res;
1291 1291
1292 1292 /* RFS3_MKDIR = 9 */
1293 1293 MKDIR3res nfs3_mkdir_res;
1294 1294
1295 1295 /* RFS3_SYMLINK = 10 */
1296 1296 SYMLINK3res nfs3_symlink_res;
1297 1297
1298 1298 /* RFS3_MKNOD = 11 */
1299 1299 MKNOD3res nfs3_mknod_res;
1300 1300
1301 1301 /* RFS3_REMOVE = 12 */
1302 1302 REMOVE3res nfs3_remove_res;
1303 1303
1304 1304 /* RFS3_RMDIR = 13 */
1305 1305 RMDIR3res nfs3_rmdir_res;
1306 1306
1307 1307 /* RFS3_RENAME = 14 */
1308 1308 RENAME3res nfs3_rename_res;
1309 1309
1310 1310 /* RFS3_LINK = 15 */
1311 1311 LINK3res nfs3_link_res;
1312 1312
1313 1313 /* RFS3_READDIR = 16 */
1314 1314 READDIR3res nfs3_readdir_res;
1315 1315
1316 1316 /* RFS3_READDIRPLUS = 17 */
1317 1317 READDIRPLUS3res nfs3_readdirplus_res;
1318 1318
1319 1319 /* RFS3_FSSTAT = 18 */
1320 1320 FSSTAT3res nfs3_fsstat_res;
1321 1321
1322 1322 /* RFS3_FSINFO = 19 */
1323 1323 FSINFO3res nfs3_fsinfo_res;
1324 1324
1325 1325 /* RFS3_PATHCONF = 20 */
1326 1326 PATHCONF3res nfs3_pathconf_res;
1327 1327
1328 1328 /* RFS3_COMMIT = 21 */
1329 1329 COMMIT3res nfs3_commit_res;
1330 1330
1331 1331 /*
1332 1332 * NFS VERSION 4
1333 1333 */
1334 1334
1335 1335 /* RFS_NULL = 0 */
1336 1336
1337 1337 /* RFS4_COMPOUND = 1 */
1338 1338 COMPOUND4res nfs4_compound_res;
1339 1339
1340 1340 };
1341 1341
1342 1342 static struct rpc_disptable rfs_disptable[] = {
1343 1343 {sizeof (rfsdisptab_v2) / sizeof (rfsdisptab_v2[0]),
1344 1344 rfscallnames_v2,
1345 1345 &rfsproccnt_v2_ptr, &rfsprocio_v2_ptr, rfsdisptab_v2},
1346 1346 {sizeof (rfsdisptab_v3) / sizeof (rfsdisptab_v3[0]),
1347 1347 rfscallnames_v3,
1348 1348 &rfsproccnt_v3_ptr, &rfsprocio_v3_ptr, rfsdisptab_v3},
1349 1349 {sizeof (rfsdisptab_v4) / sizeof (rfsdisptab_v4[0]),
1350 1350 rfscallnames_v4,
1351 1351 &rfsproccnt_v4_ptr, &rfsprocio_v4_ptr, rfsdisptab_v4},
1352 1352 };
1353 1353
1354 1354 /*
1355 1355 * If nfs_portmon is set, then clients are required to use privileged
1356 1356 * ports (ports < IPPORT_RESERVED) in order to get NFS services.
1357 1357 *
1358 1358 * N.B.: this attempt to carry forward the already ill-conceived notion
1359 1359 * of privileged ports for TCP/UDP is really quite ineffectual. Not only
1360 1360 * is it transport-dependent, it's laughably easy to spoof. If you're
1361 1361 * really interested in security, you must start with secure RPC instead.
1362 1362 */
1363 1363 static int nfs_portmon = 0;
1364 1364
1365 1365 #ifdef DEBUG
1366 1366 static int cred_hits = 0;
1367 1367 static int cred_misses = 0;
1368 1368 #endif
1369 1369
1370 1370
1371 1371 #ifdef DEBUG
1372 1372 /*
1373 1373 * Debug code to allow disabling of rfs_dispatch() use of
1374 1374 * fastxdrargs() and fastxdrres() calls for testing purposes.
1375 1375 */
1376 1376 static int rfs_no_fast_xdrargs = 0;
1377 1377 static int rfs_no_fast_xdrres = 0;
1378 1378 #endif
1379 1379
1380 1380 union acl_args {
1381 1381 /*
1382 1382 * ACL VERSION 2
1383 1383 */
1384 1384
1385 1385 /* ACL2_NULL = 0 */
1386 1386
1387 1387 /* ACL2_GETACL = 1 */
1388 1388 GETACL2args acl2_getacl_args;
1389 1389
1390 1390 /* ACL2_SETACL = 2 */
1391 1391 SETACL2args acl2_setacl_args;
1392 1392
1393 1393 /* ACL2_GETATTR = 3 */
1394 1394 GETATTR2args acl2_getattr_args;
1395 1395
1396 1396 /* ACL2_ACCESS = 4 */
1397 1397 ACCESS2args acl2_access_args;
1398 1398
1399 1399 /* ACL2_GETXATTRDIR = 5 */
1400 1400 GETXATTRDIR2args acl2_getxattrdir_args;
1401 1401
1402 1402 /*
1403 1403 * ACL VERSION 3
1404 1404 */
1405 1405
1406 1406 /* ACL3_NULL = 0 */
1407 1407
1408 1408 /* ACL3_GETACL = 1 */
1409 1409 GETACL3args acl3_getacl_args;
1410 1410
1411 1411 /* ACL3_SETACL = 2 */
1412 1412 SETACL3args acl3_setacl;
1413 1413
1414 1414 /* ACL3_GETXATTRDIR = 3 */
1415 1415 GETXATTRDIR3args acl3_getxattrdir_args;
1416 1416
1417 1417 };
1418 1418
1419 1419 union acl_res {
1420 1420 /*
1421 1421 * ACL VERSION 2
1422 1422 */
1423 1423
1424 1424 /* ACL2_NULL = 0 */
1425 1425
1426 1426 /* ACL2_GETACL = 1 */
1427 1427 GETACL2res acl2_getacl_res;
1428 1428
1429 1429 /* ACL2_SETACL = 2 */
1430 1430 SETACL2res acl2_setacl_res;
1431 1431
1432 1432 /* ACL2_GETATTR = 3 */
1433 1433 GETATTR2res acl2_getattr_res;
1434 1434
1435 1435 /* ACL2_ACCESS = 4 */
1436 1436 ACCESS2res acl2_access_res;
1437 1437
1438 1438 /* ACL2_GETXATTRDIR = 5 */
1439 1439 GETXATTRDIR2args acl2_getxattrdir_res;
1440 1440
1441 1441 /*
1442 1442 * ACL VERSION 3
1443 1443 */
1444 1444
1445 1445 /* ACL3_NULL = 0 */
1446 1446
1447 1447 /* ACL3_GETACL = 1 */
1448 1448 GETACL3res acl3_getacl_res;
1449 1449
1450 1450 /* ACL3_SETACL = 2 */
1451 1451 SETACL3res acl3_setacl_res;
1452 1452
1453 1453 /* ACL3_GETXATTRDIR = 3 */
1454 1454 GETXATTRDIR3res acl3_getxattrdir_res;
1455 1455
1456 1456 };
1457 1457
1458 1458 static bool_t
1459 1459 auth_tooweak(struct svc_req *req, char *res)
1460 1460 {
1461 1461
1462 1462 if (req->rq_vers == NFS_VERSION && req->rq_proc == RFS_LOOKUP) {
1463 1463 struct nfsdiropres *dr = (struct nfsdiropres *)res;
1464 1464 if ((enum wnfsstat)dr->dr_status == WNFSERR_CLNT_FLAVOR)
1465 1465 return (TRUE);
1466 1466 } else if (req->rq_vers == NFS_V3 && req->rq_proc == NFSPROC3_LOOKUP) {
1467 1467 LOOKUP3res *resp = (LOOKUP3res *)res;
1468 1468 if ((enum wnfsstat)resp->status == WNFSERR_CLNT_FLAVOR)
1469 1469 return (TRUE);
1470 1470 }
1471 1471 return (FALSE);
1472 1472 }
1473 1473
1474 1474
1475 1475 static void
1476 1476 common_dispatch(struct svc_req *req, SVCXPRT *xprt, rpcvers_t min_vers,
1477 1477 rpcvers_t max_vers, char *pgmname,
1478 1478 struct rpc_disptable *disptable)
1479 1479 {
1480 1480 int which;
1481 1481 rpcvers_t vers;
1482 1482 char *args;
1483 1483 union {
1484 1484 union rfs_args ra;
1485 1485 union acl_args aa;
1486 1486 } args_buf;
1487 1487 char *res;
1488 1488 union {
1489 1489 union rfs_res rr;
1490 1490 union acl_res ar;
1491 1491 } res_buf;
1492 1492 struct rpcdisp *disp = NULL;
1493 1493 int dis_flags = 0;
1494 1494 cred_t *cr;
1495 1495 int error = 0;
1496 1496 int anon_ok;
1497 1497 struct exportinfo *exi = NULL;
1498 1498 unsigned int nfslog_rec_id;
1499 1499 int dupstat;
1500 1500 struct dupreq *dr;
1501 1501 int authres;
1502 1502 bool_t publicfh_ok = FALSE;
1503 1503 enum_t auth_flavor;
1504 1504 bool_t dupcached = FALSE;
1505 1505 struct netbuf nb;
1506 1506 bool_t logging_enabled = FALSE;
1507 1507 struct exportinfo *nfslog_exi = NULL;
1508 1508 char **procnames;
1509 1509 char cbuf[INET6_ADDRSTRLEN]; /* to hold both IPv4 and IPv6 addr */
1510 1510 bool_t ro = FALSE;
1511 1511 kstat_t *ksp = NULL;
1512 1512 kstat_t *exi_ksp = NULL;
1513 1513 size_t pos; /* request size */
1514 1514 size_t rlen; /* reply size */
1515 1515 bool_t rsent = FALSE; /* reply was sent successfully */
1516 1516
1517 1517 vers = req->rq_vers;
1518 1518
1519 1519 if (vers < min_vers || vers > max_vers) {
1520 1520 svcerr_progvers(req->rq_xprt, min_vers, max_vers);
1521 1521 error++;
1522 1522 cmn_err(CE_NOTE, "%s: bad version number %u", pgmname, vers);
1523 1523 goto done;
1524 1524 }
1525 1525 vers -= min_vers;
1526 1526
1527 1527 which = req->rq_proc;
1528 1528 if (which < 0 || which >= disptable[(int)vers].dis_nprocs) {
1529 1529 svcerr_noproc(req->rq_xprt);
1530 1530 error++;
1531 1531 goto done;
1532 1532 }
1533 1533
1534 1534 (*(disptable[(int)vers].dis_proccntp))[which].value.ui64++;
1535 1535
1536 1536 ksp = (*(disptable[(int)vers].dis_prociop))[which];
1537 1537 if (ksp != NULL) {
1538 1538 mutex_enter(ksp->ks_lock);
1539 1539 kstat_runq_enter(KSTAT_IO_PTR(ksp));
1540 1540 mutex_exit(ksp->ks_lock);
1541 1541 }
1542 1542 pos = XDR_GETPOS(&xprt->xp_xdrin);
1543 1543
1544 1544 disp = &disptable[(int)vers].dis_table[which];
1545 1545 procnames = disptable[(int)vers].dis_procnames;
1546 1546
1547 1547 auth_flavor = req->rq_cred.oa_flavor;
1548 1548
1549 1549 /*
1550 1550 * Deserialize into the args struct.
1551 1551 */
1552 1552 args = (char *)&args_buf;
1553 1553
1554 1554 #ifdef DEBUG
1555 1555 if (rfs_no_fast_xdrargs || (auth_flavor == RPCSEC_GSS) ||
1556 1556 disp->dis_fastxdrargs == NULL_xdrproc_t ||
1557 1557 !SVC_GETARGS(xprt, disp->dis_fastxdrargs, (char *)&args))
1558 1558 #else
1559 1559 if ((auth_flavor == RPCSEC_GSS) ||
1560 1560 disp->dis_fastxdrargs == NULL_xdrproc_t ||
1561 1561 !SVC_GETARGS(xprt, disp->dis_fastxdrargs, (char *)&args))
1562 1562 #endif
1563 1563 {
1564 1564 bzero(args, disp->dis_argsz);
1565 1565 if (!SVC_GETARGS(xprt, disp->dis_xdrargs, args)) {
1566 1566 error++;
1567 1567 /*
1568 1568 * Check if we are outside our capabilities.
1569 1569 */
1570 1570 if (rfs4_minorvers_mismatch(req, xprt, (void *)args))
1571 1571 goto done;
1572 1572
1573 1573 svcerr_decode(xprt);
1574 1574 cmn_err(CE_NOTE,
1575 1575 "Failed to decode arguments for %s version %u "
1576 1576 "procedure %s client %s%s",
1577 1577 pgmname, vers + min_vers, procnames[which],
1578 1578 client_name(req), client_addr(req, cbuf));
1579 1579 goto done;
1580 1580 }
1581 1581 }
1582 1582
1583 1583 /*
1584 1584 * If Version 4 use that specific dispatch function.
1585 1585 */
1586 1586 if (req->rq_vers == 4) {
1587 1587 error += rfs4_dispatch(disp, req, xprt, args, &rlen);
1588 1588 if (error == 0)
1589 1589 rsent = TRUE;
1590 1590 goto done;
1591 1591 }
1592 1592
1593 1593 dis_flags = disp->dis_flags;
1594 1594
1595 1595 /*
1596 1596 * Find export information and check authentication,
1597 1597 * setting the credential if everything is ok.
1598 1598 */
1599 1599 if (disp->dis_getfh != NULL) {
1600 1600 void *fh;
1601 1601 fsid_t *fsid;
1602 1602 fid_t *fid, *xfid;
1603 1603 fhandle_t *fh2;
1604 1604 nfs_fh3 *fh3;
1605 1605
1606 1606 fh = (*disp->dis_getfh)(args);
1607 1607 switch (req->rq_vers) {
1608 1608 case NFS_VERSION:
1609 1609 fh2 = (fhandle_t *)fh;
1610 1610 fsid = &fh2->fh_fsid;
1611 1611 fid = (fid_t *)&fh2->fh_len;
1612 1612 xfid = (fid_t *)&fh2->fh_xlen;
1613 1613 break;
1614 1614 case NFS_V3:
1615 1615 fh3 = (nfs_fh3 *)fh;
1616 1616 fsid = &fh3->fh3_fsid;
1617 1617 fid = FH3TOFIDP(fh3);
1618 1618 xfid = FH3TOXFIDP(fh3);
1619 1619 break;
1620 1620 }
1621 1621
1622 1622 /*
1623 1623 * Fix for bug 1038302 - corbin
1624 1624 * There is a problem here if anonymous access is
1625 1625 * disallowed. If the current request is part of the
1626 1626 * client's mount process for the requested filesystem,
1627 1627 * then it will carry root (uid 0) credentials on it, and
1628 1628 * will be denied by checkauth if that client does not
1629 1629 * have explicit root=0 permission. This will cause the
1630 1630 * client's mount operation to fail. As a work-around,
1631 1631 * we check here to see if the request is a getattr or
1632 1632 * statfs operation on the exported vnode itself, and
1633 1633 * pass a flag to checkauth with the result of this test.
1634 1634 *
1635 1635 * The filehandle refers to the mountpoint itself if
1636 1636 * the fh_data and fh_xdata portions of the filehandle
1637 1637 * are equal.
1638 1638 *
1639 1639 * Added anon_ok argument to checkauth().
1640 1640 */
1641 1641
1642 1642 if ((dis_flags & RPC_ALLOWANON) && EQFID(fid, xfid))
1643 1643 anon_ok = 1;
1644 1644 else
1645 1645 anon_ok = 0;
1646 1646
1647 1647 cr = xprt->xp_cred;
1648 1648 ASSERT(cr != NULL);
1649 1649 #ifdef DEBUG
1650 1650 if (crgetref(cr) != 1) {
1651 1651 crfree(cr);
1652 1652 cr = crget();
1653 1653 xprt->xp_cred = cr;
1654 1654 cred_misses++;
1655 1655 } else
1656 1656 cred_hits++;
1657 1657 #else
1658 1658 if (crgetref(cr) != 1) {
1659 1659 crfree(cr);
1660 1660 cr = crget();
1661 1661 xprt->xp_cred = cr;
1662 1662 }
1663 1663 #endif
1664 1664
1665 1665 exi = checkexport(fsid, xfid);
1666 1666
1667 1667 if (exi != NULL) {
1668 1668 rw_enter(&exported_lock, RW_READER);
1669 1669
1670 1670 switch (req->rq_vers) {
1671 1671 case NFS_VERSION:
1672 1672 exi_ksp = (disptable == rfs_disptable) ?
1673 1673 exi->exi_kstats->rfsprocio_v2_ptr[which] :
1674 1674 exi->exi_kstats->aclprocio_v2_ptr[which];
1675 1675 break;
1676 1676 case NFS_V3:
1677 1677 exi_ksp = (disptable == rfs_disptable) ?
1678 1678 exi->exi_kstats->rfsprocio_v3_ptr[which] :
1679 1679 exi->exi_kstats->aclprocio_v3_ptr[which];
1680 1680 break;
1681 1681 default:
1682 1682 ASSERT(0);
1683 1683 break;
1684 1684 }
1685 1685
1686 1686 if (exi_ksp != NULL) {
1687 1687 mutex_enter(exi_ksp->ks_lock);
1688 1688 kstat_runq_enter(KSTAT_IO_PTR(exi_ksp));
1689 1689 mutex_exit(exi_ksp->ks_lock);
1690 1690 } else {
1691 1691 rw_exit(&exported_lock);
1692 1692 }
1693 1693
1694 1694 publicfh_ok = PUBLICFH_CHECK(disp, exi, fsid, xfid);
1695 1695
1696 1696 /*
1697 1697 * Don't allow non-V4 clients access
1698 1698 * to pseudo exports
1699 1699 */
1700 1700 if (PSEUDO(exi)) {
1701 1701 svcerr_weakauth(xprt);
1702 1702 error++;
1703 1703 goto done;
1704 1704 }
1705 1705
1706 1706 authres = checkauth(exi, req, cr, anon_ok, publicfh_ok,
1707 1707 &ro);
1708 1708 /*
1709 1709 * authres > 0: authentication OK - proceed
1710 1710 * authres == 0: authentication weak - return error
1711 1711 * authres < 0: authentication timeout - drop
1712 1712 */
1713 1713 if (authres <= 0) {
1714 1714 if (authres == 0) {
1715 1715 svcerr_weakauth(xprt);
1716 1716 error++;
1717 1717 }
1718 1718 goto done;
1719 1719 }
1720 1720 }
1721 1721 } else
1722 1722 cr = NULL;
1723 1723
1724 1724 if ((dis_flags & RPC_MAPRESP) && (auth_flavor != RPCSEC_GSS)) {
1725 1725 res = (char *)SVC_GETRES(xprt, disp->dis_ressz);
1726 1726 if (res == NULL)
1727 1727 res = (char *)&res_buf;
1728 1728 } else
1729 1729 res = (char *)&res_buf;
1730 1730
1731 1731 if (!(dis_flags & RPC_IDEMPOTENT)) {
1732 1732 dupstat = SVC_DUP_EXT(xprt, req, res, disp->dis_ressz, &dr,
1733 1733 &dupcached);
1734 1734
1735 1735 switch (dupstat) {
1736 1736 case DUP_ERROR:
1737 1737 svcerr_systemerr(xprt);
1738 1738 error++;
1739 1739 goto done;
1740 1740 /* NOTREACHED */
1741 1741 case DUP_INPROGRESS:
1742 1742 if (res != (char *)&res_buf)
1743 1743 SVC_FREERES(xprt);
1744 1744 error++;
1745 1745 goto done;
1746 1746 /* NOTREACHED */
1747 1747 case DUP_NEW:
1748 1748 case DUP_DROP:
1749 1749 curthread->t_flag |= T_DONTPEND;
1750 1750
1751 1751 (*disp->dis_proc)(args, res, exi, req, cr, ro);
1752 1752
1753 1753 curthread->t_flag &= ~T_DONTPEND;
1754 1754 if (curthread->t_flag & T_WOULDBLOCK) {
1755 1755 curthread->t_flag &= ~T_WOULDBLOCK;
1756 1756 SVC_DUPDONE_EXT(xprt, dr, res, NULL,
1757 1757 disp->dis_ressz, DUP_DROP);
1758 1758 if (res != (char *)&res_buf)
1759 1759 SVC_FREERES(xprt);
1760 1760 error++;
1761 1761 goto done;
1762 1762 }
1763 1763 if (dis_flags & RPC_AVOIDWORK) {
1764 1764 SVC_DUPDONE_EXT(xprt, dr, res, NULL,
1765 1765 disp->dis_ressz, DUP_DROP);
1766 1766 } else {
1767 1767 SVC_DUPDONE_EXT(xprt, dr, res,
1768 1768 disp->dis_resfree == nullfree ? NULL :
1769 1769 disp->dis_resfree,
1770 1770 disp->dis_ressz, DUP_DONE);
1771 1771 dupcached = TRUE;
1772 1772 }
1773 1773 break;
1774 1774 case DUP_DONE:
1775 1775 break;
1776 1776 }
1777 1777
1778 1778 } else {
1779 1779 curthread->t_flag |= T_DONTPEND;
1780 1780
1781 1781 (*disp->dis_proc)(args, res, exi, req, cr, ro);
1782 1782
1783 1783 curthread->t_flag &= ~T_DONTPEND;
1784 1784 if (curthread->t_flag & T_WOULDBLOCK) {
1785 1785 curthread->t_flag &= ~T_WOULDBLOCK;
1786 1786 if (res != (char *)&res_buf)
1787 1787 SVC_FREERES(xprt);
1788 1788 error++;
1789 1789 goto done;
1790 1790 }
1791 1791 }
1792 1792
1793 1793 if (auth_tooweak(req, res)) {
1794 1794 svcerr_weakauth(xprt);
1795 1795 error++;
1796 1796 goto done;
1797 1797 }
1798 1798
1799 1799 /*
1800 1800 * Check to see if logging has been enabled on the server.
1801 1801 * If so, then obtain the export info struct to be used for
1802 1802 * the later writing of the log record. This is done for
1803 1803 * the case that a lookup is done across a non-logged public
1804 1804 * file system.
1805 1805 */
1806 1806 if (nfslog_buffer_list != NULL) {
1807 1807 nfslog_exi = nfslog_get_exi(exi, req, res, &nfslog_rec_id);
1808 1808 /*
1809 1809 * Is logging enabled?
1810 1810 */
1811 1811 logging_enabled = (nfslog_exi != NULL);
1812 1812
1813 1813 /*
1814 1814 * Copy the netbuf for logging purposes, before it is
1815 1815 * freed by svc_sendreply().
1816 1816 */
1817 1817 if (logging_enabled) {
1818 1818 NFSLOG_COPY_NETBUF(nfslog_exi, xprt, &nb);
1819 1819 /*
1820 1820 * If RPC_MAPRESP flag set (i.e. in V2 ops) the
1821 1821 * res gets copied directly into the mbuf and
1822 1822 * may be freed soon after the sendreply. So we
1823 1823 * must copy it here to a safe place...
1824 1824 */
1825 1825 if (res != (char *)&res_buf) {
1826 1826 bcopy(res, (char *)&res_buf, disp->dis_ressz);
1827 1827 }
1828 1828 }
1829 1829 }
1830 1830
1831 1831 /*
1832 1832 * Serialize and send results struct
1833 1833 */
1834 1834 #ifdef DEBUG
1835 1835 if (rfs_no_fast_xdrres == 0 && res != (char *)&res_buf)
1836 1836 #else
1837 1837 if (res != (char *)&res_buf)
1838 1838 #endif
1839 1839 {
1840 1840 if (!svc_sendreply(xprt, disp->dis_fastxdrres, res)) {
1841 1841 cmn_err(CE_NOTE, "%s: bad sendreply", pgmname);
1842 1842 svcerr_systemerr(xprt);
1843 1843 error++;
1844 1844 } else {
1845 1845 rlen = xdr_sizeof(disp->dis_fastxdrres, res);
1846 1846 rsent = TRUE;
1847 1847 }
1848 1848 } else {
1849 1849 if (!svc_sendreply(xprt, disp->dis_xdrres, res)) {
1850 1850 cmn_err(CE_NOTE, "%s: bad sendreply", pgmname);
1851 1851 svcerr_systemerr(xprt);
1852 1852 error++;
1853 1853 } else {
1854 1854 rlen = xdr_sizeof(disp->dis_xdrres, res);
1855 1855 rsent = TRUE;
1856 1856 }
1857 1857 }
1858 1858
1859 1859 /*
1860 1860 * Log if needed
1861 1861 */
1862 1862 if (logging_enabled) {
1863 1863 nfslog_write_record(nfslog_exi, req, args, (char *)&res_buf,
1864 1864 cr, &nb, nfslog_rec_id, NFSLOG_ONE_BUFFER);
1865 1865 exi_rele(nfslog_exi);
1866 1866 kmem_free((&nb)->buf, (&nb)->len);
1867 1867 }
1868 1868
1869 1869 /*
1870 1870 * Free results struct. With the addition of NFS V4 we can
1871 1871 * have non-idempotent procedures with functions.
1872 1872 */
1873 1873 if (disp->dis_resfree != nullfree && dupcached == FALSE) {
1874 1874 (*disp->dis_resfree)(res);
1875 1875 }
1876 1876
1877 1877 done:
1878 1878 if (ksp != NULL || exi_ksp != NULL) {
1879 1879 pos = XDR_GETPOS(&xprt->xp_xdrin) - pos;
1880 1880 }
1881 1881
1882 1882 /*
1883 1883 * Free arguments struct
1884 1884 */
1885 1885 if (disp) {
1886 1886 if (!SVC_FREEARGS(xprt, disp->dis_xdrargs, args)) {
1887 1887 cmn_err(CE_NOTE, "%s: bad freeargs", pgmname);
1888 1888 error++;
1889 1889 }
1890 1890 } else {
1891 1891 if (!SVC_FREEARGS(xprt, (xdrproc_t)0, (caddr_t)0)) {
1892 1892 cmn_err(CE_NOTE, "%s: bad freeargs", pgmname);
1893 1893 error++;
1894 1894 }
1895 1895 }
1896 1896
1897 1897 if (exi_ksp != NULL) {
1898 1898 mutex_enter(exi_ksp->ks_lock);
1899 1899 KSTAT_IO_PTR(exi_ksp)->nwritten += pos;
1900 1900 KSTAT_IO_PTR(exi_ksp)->writes++;
1901 1901 if (rsent) {
1902 1902 KSTAT_IO_PTR(exi_ksp)->nread += rlen;
1903 1903 KSTAT_IO_PTR(exi_ksp)->reads++;
1904 1904 }
1905 1905 kstat_runq_exit(KSTAT_IO_PTR(exi_ksp));
1906 1906 mutex_exit(exi_ksp->ks_lock);
1907 1907
1908 1908 rw_exit(&exported_lock);
1909 1909 }
1910 1910
1911 1911 if (exi != NULL)
1912 1912 exi_rele(exi);
1913 1913
1914 1914 if (ksp != NULL) {
1915 1915 mutex_enter(ksp->ks_lock);
1916 1916 KSTAT_IO_PTR(ksp)->nwritten += pos;
1917 1917 KSTAT_IO_PTR(ksp)->writes++;
1918 1918 if (rsent) {
1919 1919 KSTAT_IO_PTR(ksp)->nread += rlen;
1920 1920 KSTAT_IO_PTR(ksp)->reads++;
1921 1921 }
1922 1922 kstat_runq_exit(KSTAT_IO_PTR(ksp));
1923 1923 mutex_exit(ksp->ks_lock);
1924 1924 }
1925 1925
1926 1926 global_svstat_ptr[req->rq_vers][NFS_BADCALLS].value.ui64 += error;
1927 1927
1928 1928 global_svstat_ptr[req->rq_vers][NFS_CALLS].value.ui64++;
1929 1929 }
1930 1930
1931 1931 static void
1932 1932 rfs_dispatch(struct svc_req *req, SVCXPRT *xprt)
1933 1933 {
1934 1934 common_dispatch(req, xprt, NFS_VERSMIN, NFS_VERSMAX,
1935 1935 "NFS", rfs_disptable);
1936 1936 }
1937 1937
1938 1938 static char *aclcallnames_v2[] = {
1939 1939 "ACL2_NULL",
1940 1940 "ACL2_GETACL",
1941 1941 "ACL2_SETACL",
1942 1942 "ACL2_GETATTR",
1943 1943 "ACL2_ACCESS",
1944 1944 "ACL2_GETXATTRDIR"
1945 1945 };
1946 1946
1947 1947 static struct rpcdisp acldisptab_v2[] = {
1948 1948 /*
1949 1949 * ACL VERSION 2
1950 1950 */
1951 1951
1952 1952 /* ACL2_NULL = 0 */
1953 1953 {rpc_null,
1954 1954 xdr_void, NULL_xdrproc_t, 0,
1955 1955 xdr_void, NULL_xdrproc_t, 0,
1956 1956 nullfree, RPC_IDEMPOTENT,
1957 1957 0},
1958 1958
1959 1959 /* ACL2_GETACL = 1 */
1960 1960 {acl2_getacl,
1961 1961 xdr_GETACL2args, xdr_fastGETACL2args, sizeof (GETACL2args),
1962 1962 xdr_GETACL2res, NULL_xdrproc_t, sizeof (GETACL2res),
1963 1963 acl2_getacl_free, RPC_IDEMPOTENT,
1964 1964 acl2_getacl_getfh},
1965 1965
1966 1966 /* ACL2_SETACL = 2 */
1967 1967 {acl2_setacl,
1968 1968 xdr_SETACL2args, NULL_xdrproc_t, sizeof (SETACL2args),
1969 1969 #ifdef _LITTLE_ENDIAN
1970 1970 xdr_SETACL2res, xdr_fastSETACL2res, sizeof (SETACL2res),
1971 1971 #else
1972 1972 xdr_SETACL2res, NULL_xdrproc_t, sizeof (SETACL2res),
1973 1973 #endif
1974 1974 nullfree, RPC_MAPRESP,
1975 1975 acl2_setacl_getfh},
1976 1976
1977 1977 /* ACL2_GETATTR = 3 */
1978 1978 {acl2_getattr,
1979 1979 xdr_GETATTR2args, xdr_fastGETATTR2args, sizeof (GETATTR2args),
1980 1980 #ifdef _LITTLE_ENDIAN
1981 1981 xdr_GETATTR2res, xdr_fastGETATTR2res, sizeof (GETATTR2res),
1982 1982 #else
1983 1983 xdr_GETATTR2res, NULL_xdrproc_t, sizeof (GETATTR2res),
1984 1984 #endif
1985 1985 nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
1986 1986 acl2_getattr_getfh},
1987 1987
1988 1988 /* ACL2_ACCESS = 4 */
1989 1989 {acl2_access,
1990 1990 xdr_ACCESS2args, xdr_fastACCESS2args, sizeof (ACCESS2args),
1991 1991 #ifdef _LITTLE_ENDIAN
1992 1992 xdr_ACCESS2res, xdr_fastACCESS2res, sizeof (ACCESS2res),
1993 1993 #else
1994 1994 xdr_ACCESS2res, NULL_xdrproc_t, sizeof (ACCESS2res),
1995 1995 #endif
1996 1996 nullfree, RPC_IDEMPOTENT|RPC_MAPRESP,
1997 1997 acl2_access_getfh},
1998 1998
1999 1999 /* ACL2_GETXATTRDIR = 5 */
2000 2000 {acl2_getxattrdir,
2001 2001 xdr_GETXATTRDIR2args, NULL_xdrproc_t, sizeof (GETXATTRDIR2args),
2002 2002 xdr_GETXATTRDIR2res, NULL_xdrproc_t, sizeof (GETXATTRDIR2res),
2003 2003 nullfree, RPC_IDEMPOTENT,
2004 2004 acl2_getxattrdir_getfh},
2005 2005 };
2006 2006
2007 2007 static char *aclcallnames_v3[] = {
2008 2008 "ACL3_NULL",
2009 2009 "ACL3_GETACL",
2010 2010 "ACL3_SETACL",
2011 2011 "ACL3_GETXATTRDIR"
2012 2012 };
2013 2013
2014 2014 static struct rpcdisp acldisptab_v3[] = {
2015 2015 /*
2016 2016 * ACL VERSION 3
2017 2017 */
2018 2018
2019 2019 /* ACL3_NULL = 0 */
2020 2020 {rpc_null,
2021 2021 xdr_void, NULL_xdrproc_t, 0,
2022 2022 xdr_void, NULL_xdrproc_t, 0,
2023 2023 nullfree, RPC_IDEMPOTENT,
2024 2024 0},
2025 2025
2026 2026 /* ACL3_GETACL = 1 */
2027 2027 {acl3_getacl,
2028 2028 xdr_GETACL3args, NULL_xdrproc_t, sizeof (GETACL3args),
2029 2029 xdr_GETACL3res, NULL_xdrproc_t, sizeof (GETACL3res),
2030 2030 acl3_getacl_free, RPC_IDEMPOTENT,
2031 2031 acl3_getacl_getfh},
2032 2032
2033 2033 /* ACL3_SETACL = 2 */
2034 2034 {acl3_setacl,
2035 2035 xdr_SETACL3args, NULL_xdrproc_t, sizeof (SETACL3args),
2036 2036 xdr_SETACL3res, NULL_xdrproc_t, sizeof (SETACL3res),
2037 2037 nullfree, 0,
2038 2038 acl3_setacl_getfh},
2039 2039
2040 2040 /* ACL3_GETXATTRDIR = 3 */
2041 2041 {acl3_getxattrdir,
2042 2042 xdr_GETXATTRDIR3args, NULL_xdrproc_t, sizeof (GETXATTRDIR3args),
2043 2043 xdr_GETXATTRDIR3res, NULL_xdrproc_t, sizeof (GETXATTRDIR3res),
2044 2044 nullfree, RPC_IDEMPOTENT,
2045 2045 acl3_getxattrdir_getfh},
2046 2046 };
2047 2047
2048 2048 static struct rpc_disptable acl_disptable[] = {
2049 2049 {sizeof (acldisptab_v2) / sizeof (acldisptab_v2[0]),
2050 2050 aclcallnames_v2,
2051 2051 &aclproccnt_v2_ptr, &aclprocio_v2_ptr, acldisptab_v2},
2052 2052 {sizeof (acldisptab_v3) / sizeof (acldisptab_v3[0]),
2053 2053 aclcallnames_v3,
2054 2054 &aclproccnt_v3_ptr, &aclprocio_v3_ptr, acldisptab_v3},
2055 2055 };
2056 2056
2057 2057 static void
2058 2058 acl_dispatch(struct svc_req *req, SVCXPRT *xprt)
2059 2059 {
2060 2060 common_dispatch(req, xprt, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX,
2061 2061 "ACL", acl_disptable);
2062 2062 }
2063 2063
2064 2064 int
2065 2065 checkwin(int flavor, int window, struct svc_req *req)
2066 2066 {
2067 2067 struct authdes_cred *adc;
2068 2068
2069 2069 switch (flavor) {
2070 2070 case AUTH_DES:
2071 2071 adc = (struct authdes_cred *)req->rq_clntcred;
2072 2072 if (adc->adc_fullname.window > window)
2073 2073 return (0);
2074 2074 break;
2075 2075
2076 2076 default:
2077 2077 break;
2078 2078 }
2079 2079 return (1);
2080 2080 }
2081 2081
2082 2082
2083 2083 /*
2084 2084 * checkauth() will check the access permission against the export
2085 2085 * information. Then map root uid/gid to appropriate uid/gid.
2086 2086 *
2087 2087 * This routine is used by NFS V3 and V2 code.
2088 2088 */
2089 2089 static int
2090 2090 checkauth(struct exportinfo *exi, struct svc_req *req, cred_t *cr, int anon_ok,
2091 2091 bool_t publicfh_ok, bool_t *ro)
2092 2092 {
2093 2093 int i, nfsflavor, rpcflavor, stat, access;
2094 2094 struct secinfo *secp;
2095 2095 caddr_t principal;
2096 2096 char buf[INET6_ADDRSTRLEN]; /* to hold both IPv4 and IPv6 addr */
2097 2097 int anon_res = 0;
2098 2098
2099 2099 uid_t uid;
2100 2100 gid_t gid;
2101 2101 uint_t ngids;
2102 2102 gid_t *gids;
2103 2103
2104 2104 /*
2105 2105 * Check for privileged port number
2106 2106 * N.B.: this assumes that we know the format of a netbuf.
2107 2107 */
2108 2108 if (nfs_portmon) {
2109 2109 struct sockaddr *ca;
2110 2110 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2111 2111
2112 2112 if (ca == NULL)
2113 2113 return (0);
2114 2114
2115 2115 if ((ca->sa_family == AF_INET &&
2116 2116 ntohs(((struct sockaddr_in *)ca)->sin_port) >=
2117 2117 IPPORT_RESERVED) ||
2118 2118 (ca->sa_family == AF_INET6 &&
2119 2119 ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
2120 2120 IPPORT_RESERVED)) {
2121 2121 cmn_err(CE_NOTE,
2122 2122 "nfs_server: client %s%ssent NFS request from "
2123 2123 "unprivileged port",
2124 2124 client_name(req), client_addr(req, buf));
2125 2125 return (0);
2126 2126 }
2127 2127 }
2128 2128
2129 2129 /*
2130 2130 * return 1 on success or 0 on failure
2131 2131 */
2132 2132 stat = sec_svc_getcred(req, cr, &principal, &nfsflavor);
2133 2133
2134 2134 /*
2135 2135 * A failed AUTH_UNIX sec_svc_getcred() implies we couldn't set
2136 2136 * the credentials; below we map that to anonymous.
2137 2137 */
2138 2138 if (!stat && nfsflavor != AUTH_UNIX) {
2139 2139 cmn_err(CE_NOTE,
2140 2140 "nfs_server: couldn't get unix cred for %s",
2141 2141 client_name(req));
2142 2142 return (0);
2143 2143 }
2144 2144
2145 2145 /*
2146 2146 * Short circuit checkauth() on operations that support the
2147 2147 * public filehandle, and if the request for that operation
2148 2148 * is using the public filehandle. Note that we must call
2149 2149 * sec_svc_getcred() first so that xp_cookie is set to the
2150 2150 * right value. Normally xp_cookie is just the RPC flavor
2151 2151 * of the the request, but in the case of RPCSEC_GSS it
2152 2152 * could be a pseudo flavor.
2153 2153 */
2154 2154 if (publicfh_ok)
2155 2155 return (1);
2156 2156
2157 2157 rpcflavor = req->rq_cred.oa_flavor;
2158 2158 /*
2159 2159 * Check if the auth flavor is valid for this export
2160 2160 */
2161 2161 access = nfsauth_access(exi, req, cr, &uid, &gid, &ngids, &gids);
2162 2162 if (access & NFSAUTH_DROP)
2163 2163 return (-1); /* drop the request */
2164 2164
2165 2165 if (access & NFSAUTH_RO)
2166 2166 *ro = TRUE;
2167 2167
2168 2168 if (access & NFSAUTH_DENIED) {
2169 2169 /*
2170 2170 * If anon_ok == 1 and we got NFSAUTH_DENIED, it was
2171 2171 * probably due to the flavor not matching during
2172 2172 * the mount attempt. So map the flavor to AUTH_NONE
2173 2173 * so that the credentials get mapped to the anonymous
2174 2174 * user.
2175 2175 */
2176 2176 if (anon_ok == 1)
2177 2177 rpcflavor = AUTH_NONE;
2178 2178 else
2179 2179 return (0); /* deny access */
2180 2180
2181 2181 } else if (access & NFSAUTH_MAPNONE) {
2182 2182 /*
2183 2183 * Access was granted even though the flavor mismatched
2184 2184 * because AUTH_NONE was one of the exported flavors.
2185 2185 */
2186 2186 rpcflavor = AUTH_NONE;
2187 2187
2188 2188 } else if (access & NFSAUTH_WRONGSEC) {
2189 2189 /*
2190 2190 * NFSAUTH_WRONGSEC is used for NFSv4. If we get here,
2191 2191 * it means a client ignored the list of allowed flavors
2192 2192 * returned via the MOUNT protocol. So we just disallow it!
2193 2193 */
2194 2194 return (0);
2195 2195 }
2196 2196
2197 2197 if (rpcflavor != AUTH_SYS)
2198 2198 kmem_free(gids, ngids * sizeof (gid_t));
2199 2199
2200 2200 switch (rpcflavor) {
2201 2201 case AUTH_NONE:
2202 2202 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2203 2203 exi->exi_export.ex_anon);
2204 2204 (void) crsetgroups(cr, 0, NULL);
2205 2205 break;
2206 2206
2207 2207 case AUTH_UNIX:
2208 2208 if (!stat || crgetuid(cr) == 0 && !(access & NFSAUTH_UIDMAP)) {
2209 2209 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2210 2210 exi->exi_export.ex_anon);
2211 2211 (void) crsetgroups(cr, 0, NULL);
2212 2212 } else if (crgetuid(cr) == 0 && access & NFSAUTH_ROOT) {
2213 2213 /*
2214 2214 * It is root, so apply rootid to get real UID
2215 2215 * Find the secinfo structure. We should be able
2216 2216 * to find it by the time we reach here.
2217 2217 * nfsauth_access() has done the checking.
2218 2218 */
2219 2219 secp = NULL;
2220 2220 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2221 2221 struct secinfo *sptr;
2222 2222 sptr = &exi->exi_export.ex_secinfo[i];
2223 2223 if (sptr->s_secinfo.sc_nfsnum == nfsflavor) {
2224 2224 secp = sptr;
2225 2225 break;
2226 2226 }
2227 2227 }
2228 2228 if (secp != NULL) {
2229 2229 (void) crsetugid(cr, secp->s_rootid,
2230 2230 secp->s_rootid);
2231 2231 (void) crsetgroups(cr, 0, NULL);
2232 2232 }
2233 2233 } else if (crgetuid(cr) != uid || crgetgid(cr) != gid) {
2234 2234 if (crsetugid(cr, uid, gid) != 0)
2235 2235 anon_res = crsetugid(cr,
2236 2236 exi->exi_export.ex_anon,
2237 2237 exi->exi_export.ex_anon);
2238 2238 (void) crsetgroups(cr, 0, NULL);
2239 2239 } else if (access & NFSAUTH_GROUPS) {
2240 2240 (void) crsetgroups(cr, ngids, gids);
2241 2241 }
2242 2242
2243 2243 kmem_free(gids, ngids * sizeof (gid_t));
2244 2244
2245 2245 break;
2246 2246
2247 2247 case AUTH_DES:
2248 2248 case RPCSEC_GSS:
2249 2249 /*
2250 2250 * Find the secinfo structure. We should be able
2251 2251 * to find it by the time we reach here.
2252 2252 * nfsauth_access() has done the checking.
2253 2253 */
2254 2254 secp = NULL;
2255 2255 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2256 2256 if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2257 2257 nfsflavor) {
2258 2258 secp = &exi->exi_export.ex_secinfo[i];
2259 2259 break;
2260 2260 }
2261 2261 }
2262 2262
2263 2263 if (!secp) {
2264 2264 cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2265 2265 "no secinfo data for flavor %d",
2266 2266 client_name(req), client_addr(req, buf),
2267 2267 nfsflavor);
2268 2268 return (0);
2269 2269 }
2270 2270
2271 2271 if (!checkwin(rpcflavor, secp->s_window, req)) {
2272 2272 cmn_err(CE_NOTE,
2273 2273 "nfs_server: client %s%sused invalid "
2274 2274 "auth window value",
2275 2275 client_name(req), client_addr(req, buf));
2276 2276 return (0);
2277 2277 }
2278 2278
2279 2279 /*
2280 2280 * Map root principals listed in the share's root= list to root,
2281 2281 * and map any others principals that were mapped to root by RPC
2282 2282 * to anon.
2283 2283 */
2284 2284 if (principal && sec_svc_inrootlist(rpcflavor, principal,
2285 2285 secp->s_rootcnt, secp->s_rootnames)) {
2286 2286 if (crgetuid(cr) == 0 && secp->s_rootid == 0)
2287 2287 return (1);
2288 2288
2289 2289
2290 2290 (void) crsetugid(cr, secp->s_rootid, secp->s_rootid);
2291 2291
2292 2292 /*
2293 2293 * NOTE: If and when kernel-land privilege tracing is
2294 2294 * added this may have to be replaced with code that
2295 2295 * retrieves root's supplementary groups (e.g., using
2296 2296 * kgss_get_group_info(). In the meantime principals
2297 2297 * mapped to uid 0 get all privileges, so setting cr's
2298 2298 * supplementary groups for them does nothing.
2299 2299 */
2300 2300 (void) crsetgroups(cr, 0, NULL);
2301 2301
2302 2302 return (1);
2303 2303 }
2304 2304
2305 2305 /*
2306 2306 * Not a root princ, or not in root list, map UID 0/nobody to
2307 2307 * the anon ID for the share. (RPC sets cr's UIDs and GIDs to
2308 2308 * UID_NOBODY and GID_NOBODY, respectively.)
2309 2309 */
2310 2310 if (crgetuid(cr) != 0 &&
2311 2311 (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2312 2312 return (1);
2313 2313
2314 2314 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2315 2315 exi->exi_export.ex_anon);
2316 2316 (void) crsetgroups(cr, 0, NULL);
2317 2317 break;
2318 2318 default:
2319 2319 return (0);
2320 2320 } /* switch on rpcflavor */
2321 2321
2322 2322 /*
2323 2323 * Even if anon access is disallowed via ex_anon == -1, we allow
2324 2324 * this access if anon_ok is set. So set creds to the default
2325 2325 * "nobody" id.
2326 2326 */
2327 2327 if (anon_res != 0) {
2328 2328 if (anon_ok == 0) {
2329 2329 cmn_err(CE_NOTE,
2330 2330 "nfs_server: client %s%ssent wrong "
2331 2331 "authentication for %s",
2332 2332 client_name(req), client_addr(req, buf),
2333 2333 exi->exi_export.ex_path ?
2334 2334 exi->exi_export.ex_path : "?");
2335 2335 return (0);
2336 2336 }
2337 2337
2338 2338 if (crsetugid(cr, UID_NOBODY, GID_NOBODY) != 0)
2339 2339 return (0);
2340 2340 }
2341 2341
2342 2342 return (1);
2343 2343 }
2344 2344
2345 2345 /*
2346 2346 * returns 0 on failure, -1 on a drop, -2 on wrong security flavor,
2347 2347 * and 1 on success
2348 2348 */
2349 2349 int
2350 2350 checkauth4(struct compound_state *cs, struct svc_req *req)
2351 2351 {
2352 2352 int i, rpcflavor, access;
2353 2353 struct secinfo *secp;
2354 2354 char buf[MAXHOST + 1];
2355 2355 int anon_res = 0, nfsflavor;
2356 2356 struct exportinfo *exi;
2357 2357 cred_t *cr;
2358 2358 caddr_t principal;
2359 2359
2360 2360 uid_t uid;
2361 2361 gid_t gid;
2362 2362 uint_t ngids;
2363 2363 gid_t *gids;
2364 2364
2365 2365 exi = cs->exi;
2366 2366 cr = cs->cr;
2367 2367 principal = cs->principal;
2368 2368 nfsflavor = cs->nfsflavor;
2369 2369
2370 2370 ASSERT(cr != NULL);
2371 2371
2372 2372 rpcflavor = req->rq_cred.oa_flavor;
2373 2373 cs->access &= ~CS_ACCESS_LIMITED;
2374 2374
2375 2375 /*
2376 2376 * Check for privileged port number
2377 2377 * N.B.: this assumes that we know the format of a netbuf.
2378 2378 */
2379 2379 if (nfs_portmon) {
2380 2380 struct sockaddr *ca;
2381 2381 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2382 2382
2383 2383 if (ca == NULL)
2384 2384 return (0);
2385 2385
2386 2386 if ((ca->sa_family == AF_INET &&
2387 2387 ntohs(((struct sockaddr_in *)ca)->sin_port) >=
2388 2388 IPPORT_RESERVED) ||
2389 2389 (ca->sa_family == AF_INET6 &&
2390 2390 ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
2391 2391 IPPORT_RESERVED)) {
2392 2392 cmn_err(CE_NOTE,
2393 2393 "nfs_server: client %s%ssent NFSv4 request from "
2394 2394 "unprivileged port",
2395 2395 client_name(req), client_addr(req, buf));
2396 2396 return (0);
2397 2397 }
2398 2398 }
2399 2399
2400 2400 /*
2401 2401 * Check the access right per auth flavor on the vnode of
2402 2402 * this export for the given request.
2403 2403 */
2404 2404 access = nfsauth4_access(cs->exi, cs->vp, req, cr, &uid, &gid, &ngids,
2405 2405 &gids);
2406 2406
2407 2407 if (access & NFSAUTH_WRONGSEC)
2408 2408 return (-2); /* no access for this security flavor */
2409 2409
2410 2410 if (access & NFSAUTH_DROP)
2411 2411 return (-1); /* drop the request */
2412 2412
2413 2413 if (access & NFSAUTH_DENIED) {
2414 2414
2415 2415 if (exi->exi_export.ex_seccnt > 0)
2416 2416 return (0); /* deny access */
2417 2417
2418 2418 } else if (access & NFSAUTH_LIMITED) {
2419 2419
2420 2420 cs->access |= CS_ACCESS_LIMITED;
2421 2421
2422 2422 } else if (access & NFSAUTH_MAPNONE) {
2423 2423 /*
2424 2424 * Access was granted even though the flavor mismatched
2425 2425 * because AUTH_NONE was one of the exported flavors.
2426 2426 */
2427 2427 rpcflavor = AUTH_NONE;
2428 2428 }
2429 2429
2430 2430 /*
2431 2431 * XXX probably need to redo some of it for nfsv4?
2432 2432 * return 1 on success or 0 on failure
2433 2433 */
2434 2434
2435 2435 if (rpcflavor != AUTH_SYS)
2436 2436 kmem_free(gids, ngids * sizeof (gid_t));
2437 2437
2438 2438 switch (rpcflavor) {
2439 2439 case AUTH_NONE:
2440 2440 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2441 2441 exi->exi_export.ex_anon);
2442 2442 (void) crsetgroups(cr, 0, NULL);
2443 2443 break;
2444 2444
2445 2445 case AUTH_UNIX:
2446 2446 if (crgetuid(cr) == 0 && !(access & NFSAUTH_UIDMAP)) {
2447 2447 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2448 2448 exi->exi_export.ex_anon);
2449 2449 (void) crsetgroups(cr, 0, NULL);
2450 2450 } else if (crgetuid(cr) == 0 && access & NFSAUTH_ROOT) {
2451 2451 /*
2452 2452 * It is root, so apply rootid to get real UID
2453 2453 * Find the secinfo structure. We should be able
2454 2454 * to find it by the time we reach here.
2455 2455 * nfsauth_access() has done the checking.
2456 2456 */
2457 2457 secp = NULL;
2458 2458 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2459 2459 struct secinfo *sptr;
2460 2460 sptr = &exi->exi_export.ex_secinfo[i];
2461 2461 if (sptr->s_secinfo.sc_nfsnum == nfsflavor) {
2462 2462 secp = &exi->exi_export.ex_secinfo[i];
2463 2463 break;
2464 2464 }
2465 2465 }
2466 2466 if (secp != NULL) {
2467 2467 (void) crsetugid(cr, secp->s_rootid,
2468 2468 secp->s_rootid);
2469 2469 (void) crsetgroups(cr, 0, NULL);
2470 2470 }
2471 2471 } else if (crgetuid(cr) != uid || crgetgid(cr) != gid) {
2472 2472 if (crsetugid(cr, uid, gid) != 0)
2473 2473 anon_res = crsetugid(cr,
2474 2474 exi->exi_export.ex_anon,
2475 2475 exi->exi_export.ex_anon);
2476 2476 (void) crsetgroups(cr, 0, NULL);
2477 2477 } if (access & NFSAUTH_GROUPS) {
2478 2478 (void) crsetgroups(cr, ngids, gids);
2479 2479 }
2480 2480
2481 2481 kmem_free(gids, ngids * sizeof (gid_t));
2482 2482
2483 2483 break;
2484 2484
2485 2485 default:
2486 2486 /*
2487 2487 * Find the secinfo structure. We should be able
2488 2488 * to find it by the time we reach here.
2489 2489 * nfsauth_access() has done the checking.
2490 2490 */
2491 2491 secp = NULL;
2492 2492 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2493 2493 if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2494 2494 nfsflavor) {
2495 2495 secp = &exi->exi_export.ex_secinfo[i];
2496 2496 break;
2497 2497 }
2498 2498 }
2499 2499
2500 2500 if (!secp) {
2501 2501 cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2502 2502 "no secinfo data for flavor %d",
2503 2503 client_name(req), client_addr(req, buf),
2504 2504 nfsflavor);
2505 2505 return (0);
2506 2506 }
2507 2507
2508 2508 if (!checkwin(rpcflavor, secp->s_window, req)) {
2509 2509 cmn_err(CE_NOTE,
2510 2510 "nfs_server: client %s%sused invalid "
2511 2511 "auth window value",
2512 2512 client_name(req), client_addr(req, buf));
2513 2513 return (0);
2514 2514 }
2515 2515
2516 2516 /*
2517 2517 * Map root principals listed in the share's root= list to root,
2518 2518 * and map any others principals that were mapped to root by RPC
2519 2519 * to anon. If not going to anon, set to rootid (root_mapping).
2520 2520 */
2521 2521 if (principal && sec_svc_inrootlist(rpcflavor, principal,
2522 2522 secp->s_rootcnt, secp->s_rootnames)) {
2523 2523 if (crgetuid(cr) == 0 && secp->s_rootid == 0)
2524 2524 return (1);
2525 2525
2526 2526 (void) crsetugid(cr, secp->s_rootid, secp->s_rootid);
2527 2527
2528 2528 /*
2529 2529 * NOTE: If and when kernel-land privilege tracing is
2530 2530 * added this may have to be replaced with code that
2531 2531 * retrieves root's supplementary groups (e.g., using
2532 2532 * kgss_get_group_info(). In the meantime principals
2533 2533 * mapped to uid 0 get all privileges, so setting cr's
2534 2534 * supplementary groups for them does nothing.
2535 2535 */
2536 2536 (void) crsetgroups(cr, 0, NULL);
2537 2537
2538 2538 return (1);
2539 2539 }
2540 2540
2541 2541 /*
2542 2542 * Not a root princ, or not in root list, map UID 0/nobody to
2543 2543 * the anon ID for the share. (RPC sets cr's UIDs and GIDs to
2544 2544 * UID_NOBODY and GID_NOBODY, respectively.)
2545 2545 */
2546 2546 if (crgetuid(cr) != 0 &&
2547 2547 (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2548 2548 return (1);
2549 2549
2550 2550 anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2551 2551 exi->exi_export.ex_anon);
2552 2552 (void) crsetgroups(cr, 0, NULL);
2553 2553 break;
2554 2554 } /* switch on rpcflavor */
2555 2555
2556 2556 /*
2557 2557 * Even if anon access is disallowed via ex_anon == -1, we allow
2558 2558 * this access if anon_ok is set. So set creds to the default
2559 2559 * "nobody" id.
2560 2560 */
2561 2561
2562 2562 if (anon_res != 0) {
2563 2563 cmn_err(CE_NOTE,
2564 2564 "nfs_server: client %s%ssent wrong "
2565 2565 "authentication for %s",
2566 2566 client_name(req), client_addr(req, buf),
2567 2567 exi->exi_export.ex_path ?
2568 2568 exi->exi_export.ex_path : "?");
2569 2569 return (0);
2570 2570 }
2571 2571
2572 2572 return (1);
2573 2573 }
2574 2574
2575 2575
2576 2576 static char *
2577 2577 client_name(struct svc_req *req)
2578 2578 {
2579 2579 char *hostname = NULL;
2580 2580
2581 2581 /*
2582 2582 * If it's a Unix cred then use the
2583 2583 * hostname from the credential.
2584 2584 */
2585 2585 if (req->rq_cred.oa_flavor == AUTH_UNIX) {
2586 2586 hostname = ((struct authunix_parms *)
2587 2587 req->rq_clntcred)->aup_machname;
2588 2588 }
2589 2589 if (hostname == NULL)
2590 2590 hostname = "";
2591 2591
2592 2592 return (hostname);
2593 2593 }
2594 2594
2595 2595 static char *
2596 2596 client_addr(struct svc_req *req, char *buf)
2597 2597 {
2598 2598 struct sockaddr *ca;
2599 2599 uchar_t *b;
2600 2600 char *frontspace = "";
2601 2601
2602 2602 /*
2603 2603 * We assume we are called in tandem with client_name and the
2604 2604 * format string looks like "...client %s%sblah blah..."
2605 2605 *
2606 2606 * If it's a Unix cred then client_name returned
2607 2607 * a host name, so we need insert a space between host name
2608 2608 * and IP address.
2609 2609 */
2610 2610 if (req->rq_cred.oa_flavor == AUTH_UNIX)
2611 2611 frontspace = " ";
2612 2612
2613 2613 /*
2614 2614 * Convert the caller's IP address to a dotted string
2615 2615 */
2616 2616 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2617 2617
2618 2618 if (ca->sa_family == AF_INET) {
2619 2619 b = (uchar_t *)&((struct sockaddr_in *)ca)->sin_addr;
2620 2620 (void) sprintf(buf, "%s(%d.%d.%d.%d) ", frontspace,
2621 2621 b[0] & 0xFF, b[1] & 0xFF, b[2] & 0xFF, b[3] & 0xFF);
2622 2622 } else if (ca->sa_family == AF_INET6) {
2623 2623 struct sockaddr_in6 *sin6;
2624 2624 sin6 = (struct sockaddr_in6 *)ca;
2625 2625 (void) kinet_ntop6((uchar_t *)&sin6->sin6_addr,
2626 2626 buf, INET6_ADDRSTRLEN);
2627 2627
2628 2628 } else {
2629 2629
2630 2630 /*
2631 2631 * No IP address to print. If there was a host name
2632 2632 * printed, then we print a space.
2633 2633 */
2634 2634 (void) sprintf(buf, frontspace);
2635 2635 }
2636 2636
2637 2637 return (buf);
2638 2638 }
2639 2639
2640 2640 /*
2641 2641 * NFS Server initialization routine. This routine should only be called
2642 2642 * once. It performs the following tasks:
2643 2643 * - Call sub-initialization routines (localize access to variables)
2644 2644 * - Initialize all locks
2645 2645 * - initialize the version 3 write verifier
2646 2646 */
2647 2647 int
2648 2648 nfs_srvinit(void)
2649 2649 {
2650 2650 int error;
2651 2651
2652 2652 error = nfs_exportinit();
2653 2653 if (error != 0)
2654 2654 return (error);
2655 2655 error = rfs4_srvrinit();
2656 2656 if (error != 0) {
2657 2657 nfs_exportfini();
2658 2658 return (error);
2659 2659 }
2660 2660 rfs_srvrinit();
2661 2661 rfs3_srvrinit();
2662 2662 nfsauth_init();
2663 2663
2664 2664 /* Init the stuff to control start/stop */
2665 2665 nfs_server_upordown = NFS_SERVER_STOPPED;
2666 2666 mutex_init(&nfs_server_upordown_lock, NULL, MUTEX_DEFAULT, NULL);
2667 2667 cv_init(&nfs_server_upordown_cv, NULL, CV_DEFAULT, NULL);
2668 2668 mutex_init(&rdma_wait_mutex, NULL, MUTEX_DEFAULT, NULL);
2669 2669 cv_init(&rdma_wait_cv, NULL, CV_DEFAULT, NULL);
2670 2670
2671 2671 return (0);
2672 2672 }
2673 2673
2674 2674 /*
2675 2675 * NFS Server finalization routine. This routine is called to cleanup the
2676 2676 * initialization work previously performed if the NFS server module could
2677 2677 * not be loaded correctly.
2678 2678 */
2679 2679 void
2680 2680 nfs_srvfini(void)
2681 2681 {
2682 2682 nfsauth_fini();
2683 2683 rfs3_srvrfini();
2684 2684 rfs_srvrfini();
2685 2685 nfs_exportfini();
2686 2686
2687 2687 mutex_destroy(&nfs_server_upordown_lock);
2688 2688 cv_destroy(&nfs_server_upordown_cv);
2689 2689 mutex_destroy(&rdma_wait_mutex);
2690 2690 cv_destroy(&rdma_wait_cv);
2691 2691 }
2692 2692
2693 2693 /*
2694 2694 * Set up an iovec array of up to cnt pointers.
2695 2695 */
2696 2696
2697 2697 void
2698 2698 mblk_to_iov(mblk_t *m, int cnt, struct iovec *iovp)
2699 2699 {
2700 2700 while (m != NULL && cnt-- > 0) {
2701 2701 iovp->iov_base = (caddr_t)m->b_rptr;
2702 2702 iovp->iov_len = (m->b_wptr - m->b_rptr);
2703 2703 iovp++;
2704 2704 m = m->b_cont;
2705 2705 }
2706 2706 }
2707 2707
2708 2708 /*
2709 2709 * Common code between NFS Version 2 and NFS Version 3 for the public
2710 2710 * filehandle multicomponent lookups.
2711 2711 */
2712 2712
2713 2713 /*
2714 2714 * Public filehandle evaluation of a multi-component lookup, following
2715 2715 * symbolic links, if necessary. This may result in a vnode in another
2716 2716 * filesystem, which is OK as long as the other filesystem is exported.
2717 2717 *
2718 2718 * Note that the exi will be set either to NULL or a new reference to the
2719 2719 * exportinfo struct that corresponds to the vnode of the multi-component path.
2720 2720 * It is the callers responsibility to release this reference.
2721 2721 */
2722 2722 int
2723 2723 rfs_publicfh_mclookup(char *p, vnode_t *dvp, cred_t *cr, vnode_t **vpp,
2724 2724 struct exportinfo **exi, struct sec_ol *sec)
2725 2725 {
2726 2726 int pathflag;
2727 2727 vnode_t *mc_dvp = NULL;
2728 2728 vnode_t *realvp;
2729 2729 int error;
2730 2730
2731 2731 *exi = NULL;
2732 2732
2733 2733 /*
2734 2734 * check if the given path is a url or native path. Since p is
2735 2735 * modified by MCLpath(), it may be empty after returning from
2736 2736 * there, and should be checked.
2737 2737 */
2738 2738 if ((pathflag = MCLpath(&p)) == -1)
2739 2739 return (EIO);
2740 2740
2741 2741 /*
2742 2742 * If pathflag is SECURITY_QUERY, turn the SEC_QUERY bit
2743 2743 * on in sec->sec_flags. This bit will later serve as an
2744 2744 * indication in makefh_ol() or makefh3_ol() to overload the
2745 2745 * filehandle to contain the sec modes used by the server for
2746 2746 * the path.
2747 2747 */
2748 2748 if (pathflag == SECURITY_QUERY) {
2749 2749 if ((sec->sec_index = (uint_t)(*p)) > 0) {
2750 2750 sec->sec_flags |= SEC_QUERY;
2751 2751 p++;
2752 2752 if ((pathflag = MCLpath(&p)) == -1)
2753 2753 return (EIO);
2754 2754 } else {
2755 2755 cmn_err(CE_NOTE,
2756 2756 "nfs_server: invalid security index %d, "
2757 2757 "violating WebNFS SNEGO protocol.", sec->sec_index);
2758 2758 return (EIO);
2759 2759 }
2760 2760 }
2761 2761
2762 2762 if (p[0] == '\0') {
2763 2763 error = ENOENT;
2764 2764 goto publicfh_done;
2765 2765 }
2766 2766
2767 2767 error = rfs_pathname(p, &mc_dvp, vpp, dvp, cr, pathflag);
2768 2768
2769 2769 /*
2770 2770 * If name resolves to "/" we get EINVAL since we asked for
2771 2771 * the vnode of the directory that the file is in. Try again
2772 2772 * with NULL directory vnode.
2773 2773 */
2774 2774 if (error == EINVAL) {
2775 2775 error = rfs_pathname(p, NULL, vpp, dvp, cr, pathflag);
2776 2776 if (!error) {
2777 2777 ASSERT(*vpp != NULL);
2778 2778 if ((*vpp)->v_type == VDIR) {
2779 2779 VN_HOLD(*vpp);
2780 2780 mc_dvp = *vpp;
2781 2781 } else {
2782 2782 /*
2783 2783 * This should not happen, the filesystem is
2784 2784 * in an inconsistent state. Fail the lookup
2785 2785 * at this point.
2786 2786 */
2787 2787 VN_RELE(*vpp);
2788 2788 error = EINVAL;
2789 2789 }
2790 2790 }
2791 2791 }
2792 2792
2793 2793 if (error)
2794 2794 goto publicfh_done;
2795 2795
2796 2796 if (*vpp == NULL) {
2797 2797 error = ENOENT;
2798 2798 goto publicfh_done;
2799 2799 }
2800 2800
2801 2801 ASSERT(mc_dvp != NULL);
2802 2802 ASSERT(*vpp != NULL);
2803 2803
2804 2804 if ((*vpp)->v_type == VDIR) {
2805 2805 do {
2806 2806 /*
2807 2807 * *vpp may be an AutoFS node, so we perform
2808 2808 * a VOP_ACCESS() to trigger the mount of the intended
2809 2809 * filesystem, so we can perform the lookup in the
2810 2810 * intended filesystem.
2811 2811 */
2812 2812 (void) VOP_ACCESS(*vpp, 0, 0, cr, NULL);
2813 2813
2814 2814 /*
2815 2815 * If vnode is covered, get the
2816 2816 * the topmost vnode.
2817 2817 */
2818 2818 if (vn_mountedvfs(*vpp) != NULL) {
2819 2819 error = traverse(vpp);
2820 2820 if (error) {
2821 2821 VN_RELE(*vpp);
2822 2822 goto publicfh_done;
2823 2823 }
2824 2824 }
2825 2825
2826 2826 if (VOP_REALVP(*vpp, &realvp, NULL) == 0 &&
2827 2827 realvp != *vpp) {
2828 2828 /*
2829 2829 * If realvp is different from *vpp
2830 2830 * then release our reference on *vpp, so that
2831 2831 * the export access check be performed on the
2832 2832 * real filesystem instead.
2833 2833 */
2834 2834 VN_HOLD(realvp);
2835 2835 VN_RELE(*vpp);
2836 2836 *vpp = realvp;
2837 2837 } else {
2838 2838 break;
2839 2839 }
2840 2840 /* LINTED */
2841 2841 } while (TRUE);
2842 2842
2843 2843 /*
2844 2844 * Let nfs_vptexi() figure what the real parent is.
2845 2845 */
2846 2846 VN_RELE(mc_dvp);
2847 2847 mc_dvp = NULL;
2848 2848
2849 2849 } else {
2850 2850 /*
2851 2851 * If vnode is covered, get the
2852 2852 * the topmost vnode.
2853 2853 */
2854 2854 if (vn_mountedvfs(mc_dvp) != NULL) {
2855 2855 error = traverse(&mc_dvp);
2856 2856 if (error) {
2857 2857 VN_RELE(*vpp);
2858 2858 goto publicfh_done;
2859 2859 }
2860 2860 }
2861 2861
2862 2862 if (VOP_REALVP(mc_dvp, &realvp, NULL) == 0 &&
2863 2863 realvp != mc_dvp) {
2864 2864 /*
2865 2865 * *vpp is a file, obtain realvp of the parent
2866 2866 * directory vnode.
2867 2867 */
2868 2868 VN_HOLD(realvp);
2869 2869 VN_RELE(mc_dvp);
2870 2870 mc_dvp = realvp;
2871 2871 }
2872 2872 }
2873 2873
2874 2874 /*
2875 2875 * The pathname may take us from the public filesystem to another.
2876 2876 * If that's the case then just set the exportinfo to the new export
2877 2877 * and build filehandle for it. Thanks to per-access checking there's
2878 2878 * no security issues with doing this. If the client is not allowed
2879 2879 * access to this new export then it will get an access error when it
2880 2880 * tries to use the filehandle
2881 2881 */
2882 2882 if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2883 2883 VN_RELE(*vpp);
2884 2884 goto publicfh_done;
2885 2885 }
2886 2886
2887 2887 /*
2888 2888 * Not allowed access to pseudo exports.
2889 2889 */
2890 2890 if (PSEUDO(*exi)) {
2891 2891 error = ENOENT;
2892 2892 VN_RELE(*vpp);
2893 2893 goto publicfh_done;
2894 2894 }
2895 2895
2896 2896 /*
2897 2897 * Do a lookup for the index file. We know the index option doesn't
2898 2898 * allow paths through handling in the share command, so mc_dvp will
2899 2899 * be the parent for the index file vnode, if its present. Use
2900 2900 * temporary pointers to preserve and reuse the vnode pointers of the
2901 2901 * original directory in case there's no index file. Note that the
2902 2902 * index file is a native path, and should not be interpreted by
2903 2903 * the URL parser in rfs_pathname()
2904 2904 */
2905 2905 if (((*exi)->exi_export.ex_flags & EX_INDEX) &&
2906 2906 ((*vpp)->v_type == VDIR) && (pathflag == URLPATH)) {
2907 2907 vnode_t *tvp, *tmc_dvp; /* temporary vnode pointers */
2908 2908
2909 2909 tmc_dvp = mc_dvp;
2910 2910 mc_dvp = tvp = *vpp;
2911 2911
2912 2912 error = rfs_pathname((*exi)->exi_export.ex_index, NULL, vpp,
2913 2913 mc_dvp, cr, NATIVEPATH);
2914 2914
2915 2915 if (error == ENOENT) {
2916 2916 *vpp = tvp;
2917 2917 mc_dvp = tmc_dvp;
2918 2918 error = 0;
2919 2919 } else { /* ok or error other than ENOENT */
2920 2920 if (tmc_dvp)
2921 2921 VN_RELE(tmc_dvp);
2922 2922 if (error)
2923 2923 goto publicfh_done;
2924 2924
2925 2925 /*
2926 2926 * Found a valid vp for index "filename". Sanity check
2927 2927 * for odd case where a directory is provided as index
2928 2928 * option argument and leads us to another filesystem
2929 2929 */
2930 2930
2931 2931 /* Release the reference on the old exi value */
2932 2932 ASSERT(*exi != NULL);
2933 2933 exi_rele(*exi);
2934 2934
2935 2935 if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2936 2936 VN_RELE(*vpp);
2937 2937 goto publicfh_done;
2938 2938 }
2939 2939 }
2940 2940 }
2941 2941
2942 2942 publicfh_done:
2943 2943 if (mc_dvp)
2944 2944 VN_RELE(mc_dvp);
2945 2945
2946 2946 return (error);
2947 2947 }
2948 2948
2949 2949 /*
2950 2950 * Evaluate a multi-component path
2951 2951 */
2952 2952 int
2953 2953 rfs_pathname(
2954 2954 char *path, /* pathname to evaluate */
2955 2955 vnode_t **dirvpp, /* ret for ptr to parent dir vnode */
2956 2956 vnode_t **compvpp, /* ret for ptr to component vnode */
2957 2957 vnode_t *startdvp, /* starting vnode */
2958 2958 cred_t *cr, /* user's credential */
2959 2959 int pathflag) /* flag to identify path, e.g. URL */
2960 2960 {
2961 2961 char namebuf[TYPICALMAXPATHLEN];
2962 2962 struct pathname pn;
2963 2963 int error;
2964 2964
2965 2965 /*
2966 2966 * If pathname starts with '/', then set startdvp to root.
2967 2967 */
2968 2968 if (*path == '/') {
2969 2969 while (*path == '/')
2970 2970 path++;
2971 2971
2972 2972 startdvp = rootdir;
2973 2973 }
2974 2974
2975 2975 error = pn_get_buf(path, UIO_SYSSPACE, &pn, namebuf, sizeof (namebuf));
2976 2976 if (error == 0) {
2977 2977 /*
2978 2978 * Call the URL parser for URL paths to modify the original
2979 2979 * string to handle any '%' encoded characters that exist.
2980 2980 * Done here to avoid an extra bcopy in the lookup.
2981 2981 * We need to be careful about pathlen's. We know that
2982 2982 * rfs_pathname() is called with a non-empty path. However,
2983 2983 * it could be emptied due to the path simply being all /'s,
2984 2984 * which is valid to proceed with the lookup, or due to the
2985 2985 * URL parser finding an encoded null character at the
2986 2986 * beginning of path which should not proceed with the lookup.
2987 2987 */
2988 2988 if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
2989 2989 URLparse(pn.pn_path);
2990 2990 if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0)
2991 2991 return (ENOENT);
2992 2992 }
2993 2993 VN_HOLD(startdvp);
2994 2994 error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
2995 2995 rootdir, startdvp, cr);
2996 2996 }
2997 2997 if (error == ENAMETOOLONG) {
2998 2998 /*
2999 2999 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
3000 3000 */
3001 3001 if (error = pn_get(path, UIO_SYSSPACE, &pn))
3002 3002 return (error);
3003 3003 if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
3004 3004 URLparse(pn.pn_path);
3005 3005 if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0) {
3006 3006 pn_free(&pn);
3007 3007 return (ENOENT);
3008 3008 }
3009 3009 }
3010 3010 VN_HOLD(startdvp);
3011 3011 error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
3012 3012 rootdir, startdvp, cr);
3013 3013 pn_free(&pn);
3014 3014 }
3015 3015
3016 3016 return (error);
3017 3017 }
3018 3018
3019 3019 /*
3020 3020 * Adapt the multicomponent lookup path depending on the pathtype
3021 3021 */
3022 3022 static int
3023 3023 MCLpath(char **path)
3024 3024 {
3025 3025 unsigned char c = (unsigned char)**path;
3026 3026
3027 3027 /*
3028 3028 * If the MCL path is between 0x20 and 0x7E (graphic printable
3029 3029 * character of the US-ASCII coded character set), its a URL path,
3030 3030 * per RFC 1738.
3031 3031 */
3032 3032 if (c >= 0x20 && c <= 0x7E)
3033 3033 return (URLPATH);
3034 3034
3035 3035 /*
3036 3036 * If the first octet of the MCL path is not an ASCII character
3037 3037 * then it must be interpreted as a tag value that describes the
3038 3038 * format of the remaining octets of the MCL path.
3039 3039 *
3040 3040 * If the first octet of the MCL path is 0x81 it is a query
3041 3041 * for the security info.
3042 3042 */
3043 3043 switch (c) {
3044 3044 case 0x80: /* native path, i.e. MCL via mount protocol */
3045 3045 (*path)++;
3046 3046 return (NATIVEPATH);
3047 3047 case 0x81: /* security query */
3048 3048 (*path)++;
3049 3049 return (SECURITY_QUERY);
3050 3050 default:
3051 3051 return (-1);
3052 3052 }
3053 3053 }
3054 3054
3055 3055 #define fromhex(c) ((c >= '0' && c <= '9') ? (c - '0') : \
3056 3056 ((c >= 'A' && c <= 'F') ? (c - 'A' + 10) :\
3057 3057 ((c >= 'a' && c <= 'f') ? (c - 'a' + 10) : 0)))
3058 3058
3059 3059 /*
3060 3060 * The implementation of URLparse guarantees that the final string will
3061 3061 * fit in the original one. Replaces '%' occurrences followed by 2 characters
3062 3062 * with its corresponding hexadecimal character.
3063 3063 */
3064 3064 static void
3065 3065 URLparse(char *str)
3066 3066 {
3067 3067 char *p, *q;
3068 3068
3069 3069 p = q = str;
3070 3070 while (*p) {
3071 3071 *q = *p;
3072 3072 if (*p++ == '%') {
3073 3073 if (*p) {
3074 3074 *q = fromhex(*p) * 16;
3075 3075 p++;
3076 3076 if (*p) {
3077 3077 *q += fromhex(*p);
3078 3078 p++;
3079 3079 }
3080 3080 }
3081 3081 }
3082 3082 q++;
3083 3083 }
3084 3084 *q = '\0';
3085 3085 }
3086 3086
3087 3087
3088 3088 /*
3089 3089 * Get the export information for the lookup vnode, and verify its
3090 3090 * useable.
3091 3091 */
3092 3092 int
3093 3093 nfs_check_vpexi(vnode_t *mc_dvp, vnode_t *vp, cred_t *cr,
3094 3094 struct exportinfo **exi)
3095 3095 {
3096 3096 int walk;
3097 3097 int error = 0;
3098 3098
3099 3099 *exi = nfs_vptoexi(mc_dvp, vp, cr, &walk, NULL, FALSE);
3100 3100 if (*exi == NULL)
3101 3101 error = EACCES;
3102 3102 else {
3103 3103 /*
3104 3104 * If nosub is set for this export then
3105 3105 * a lookup relative to the public fh
3106 3106 * must not terminate below the
3107 3107 * exported directory.
3108 3108 */
3109 3109 if ((*exi)->exi_export.ex_flags & EX_NOSUB && walk > 0)
3110 3110 error = EACCES;
3111 3111 }
3112 3112
3113 3113 return (error);
3114 3114 }
3115 3115
3116 3116 /*
3117 3117 * Do the main work of handling HA-NFSv4 Resource Group failover on
3118 3118 * Sun Cluster.
3119 3119 * We need to detect whether any RG admin paths have been added or removed,
3120 3120 * and adjust resources accordingly.
3121 3121 * Currently we're using a very inefficient algorithm, ~ 2 * O(n**2). In
3122 3122 * order to scale, the list and array of paths need to be held in more
3123 3123 * suitable data structures.
3124 3124 */
3125 3125 static void
3126 3126 hanfsv4_failover(void)
3127 3127 {
3128 3128 int i, start_grace, numadded_paths = 0;
3129 3129 char **added_paths = NULL;
3130 3130 rfs4_dss_path_t *dss_path;
3131 3131
3132 3132 /*
3133 3133 * Note: currently, rfs4_dss_pathlist cannot be NULL, since
3134 3134 * it will always include an entry for NFS4_DSS_VAR_DIR. If we
3135 3135 * make the latter dynamically specified too, the following will
3136 3136 * need to be adjusted.
3137 3137 */
3138 3138
3139 3139 /*
3140 3140 * First, look for removed paths: RGs that have been failed-over
3141 3141 * away from this node.
3142 3142 * Walk the "currently-serving" rfs4_dss_pathlist and, for each
3143 3143 * path, check if it is on the "passed-in" rfs4_dss_newpaths array
3144 3144 * from nfsd. If not, that RG path has been removed.
3145 3145 *
3146 3146 * Note that nfsd has sorted rfs4_dss_newpaths for us, and removed
3147 3147 * any duplicates.
3148 3148 */
3149 3149 dss_path = rfs4_dss_pathlist;
3150 3150 do {
3151 3151 int found = 0;
3152 3152 char *path = dss_path->path;
3153 3153
3154 3154 /* used only for non-HA so may not be removed */
3155 3155 if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
3156 3156 dss_path = dss_path->next;
3157 3157 continue;
3158 3158 }
3159 3159
3160 3160 for (i = 0; i < rfs4_dss_numnewpaths; i++) {
3161 3161 int cmpret;
3162 3162 char *newpath = rfs4_dss_newpaths[i];
3163 3163
3164 3164 /*
3165 3165 * Since nfsd has sorted rfs4_dss_newpaths for us,
3166 3166 * once the return from strcmp is negative we know
3167 3167 * we've passed the point where "path" should be,
3168 3168 * and can stop searching: "path" has been removed.
3169 3169 */
3170 3170 cmpret = strcmp(path, newpath);
3171 3171 if (cmpret < 0)
3172 3172 break;
3173 3173 if (cmpret == 0) {
3174 3174 found = 1;
3175 3175 break;
3176 3176 }
3177 3177 }
3178 3178
3179 3179 if (found == 0) {
3180 3180 unsigned index = dss_path->index;
3181 3181 rfs4_servinst_t *sip = dss_path->sip;
3182 3182 rfs4_dss_path_t *path_next = dss_path->next;
3183 3183
3184 3184 /*
3185 3185 * This path has been removed.
3186 3186 * We must clear out the servinst reference to
3187 3187 * it, since it's now owned by another
3188 3188 * node: we should not attempt to touch it.
3189 3189 */
3190 3190 ASSERT(dss_path == sip->dss_paths[index]);
3191 3191 sip->dss_paths[index] = NULL;
3192 3192
3193 3193 /* remove from "currently-serving" list, and destroy */
3194 3194 remque(dss_path);
3195 3195 /* allow for NUL */
3196 3196 kmem_free(dss_path->path, strlen(dss_path->path) + 1);
3197 3197 kmem_free(dss_path, sizeof (rfs4_dss_path_t));
3198 3198
3199 3199 dss_path = path_next;
3200 3200 } else {
3201 3201 /* path was found; not removed */
3202 3202 dss_path = dss_path->next;
3203 3203 }
3204 3204 } while (dss_path != rfs4_dss_pathlist);
3205 3205
3206 3206 /*
3207 3207 * Now, look for added paths: RGs that have been failed-over
3208 3208 * to this node.
3209 3209 * Walk the "passed-in" rfs4_dss_newpaths array from nfsd and,
3210 3210 * for each path, check if it is on the "currently-serving"
3211 3211 * rfs4_dss_pathlist. If not, that RG path has been added.
3212 3212 *
3213 3213 * Note: we don't do duplicate detection here; nfsd does that for us.
3214 3214 *
3215 3215 * Note: numadded_paths <= rfs4_dss_numnewpaths, which gives us
3216 3216 * an upper bound for the size needed for added_paths[numadded_paths].
3217 3217 */
3218 3218
3219 3219 /* probably more space than we need, but guaranteed to be enough */
3220 3220 if (rfs4_dss_numnewpaths > 0) {
3221 3221 size_t sz = rfs4_dss_numnewpaths * sizeof (char *);
3222 3222 added_paths = kmem_zalloc(sz, KM_SLEEP);
3223 3223 }
3224 3224
3225 3225 /* walk the "passed-in" rfs4_dss_newpaths array from nfsd */
3226 3226 for (i = 0; i < rfs4_dss_numnewpaths; i++) {
3227 3227 int found = 0;
3228 3228 char *newpath = rfs4_dss_newpaths[i];
3229 3229
3230 3230 dss_path = rfs4_dss_pathlist;
3231 3231 do {
3232 3232 char *path = dss_path->path;
3233 3233
3234 3234 /* used only for non-HA */
3235 3235 if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
3236 3236 dss_path = dss_path->next;
3237 3237 continue;
3238 3238 }
3239 3239
3240 3240 if (strncmp(path, newpath, strlen(path)) == 0) {
3241 3241 found = 1;
3242 3242 break;
3243 3243 }
3244 3244
3245 3245 dss_path = dss_path->next;
3246 3246 } while (dss_path != rfs4_dss_pathlist);
3247 3247
3248 3248 if (found == 0) {
3249 3249 added_paths[numadded_paths] = newpath;
3250 3250 numadded_paths++;
3251 3251 }
3252 3252 }
3253 3253
3254 3254 /* did we find any added paths? */
3255 3255 if (numadded_paths > 0) {
3256 3256 /* create a new server instance, and start its grace period */
3257 3257 start_grace = 1;
3258 3258 rfs4_servinst_create(start_grace, numadded_paths, added_paths);
3259 3259
3260 3260 /* read in the stable storage state from these paths */
3261 3261 rfs4_dss_readstate(numadded_paths, added_paths);
3262 3262
3263 3263 /*
3264 3264 * Multiple failovers during a grace period will cause
3265 3265 * clients of the same resource group to be partitioned
3266 3266 * into different server instances, with different
3267 3267 * grace periods. Since clients of the same resource
3268 3268 * group must be subject to the same grace period,
3269 3269 * we need to reset all currently active grace periods.
3270 3270 */
3271 3271 rfs4_grace_reset_all();
3272 3272 }
3273 3273
3274 3274 if (rfs4_dss_numnewpaths > 0)
3275 3275 kmem_free(added_paths, rfs4_dss_numnewpaths * sizeof (char *));
3276 3276 }
3277 3277
3278 3278 /*
3279 3279 * Used by NFSv3 and NFSv4 server to query label of
3280 3280 * a pathname component during lookup/access ops.
3281 3281 */
3282 3282 ts_label_t *
3283 3283 nfs_getflabel(vnode_t *vp, struct exportinfo *exi)
3284 3284 {
3285 3285 zone_t *zone;
3286 3286 ts_label_t *zone_label;
3287 3287 char *path;
3288 3288
3289 3289 mutex_enter(&vp->v_lock);
3290 3290 if (vp->v_path != NULL) {
3291 3291 zone = zone_find_by_any_path(vp->v_path, B_FALSE);
3292 3292 mutex_exit(&vp->v_lock);
3293 3293 } else {
3294 3294 /*
3295 3295 * v_path not cached. Fall back on pathname of exported
3296 3296 * file system as we rely on pathname from which we can
3297 3297 * derive a label. The exported file system portion of
3298 3298 * path is sufficient to obtain a label.
3299 3299 */
3300 3300 path = exi->exi_export.ex_path;
3301 3301 if (path == NULL) {
3302 3302 mutex_exit(&vp->v_lock);
3303 3303 return (NULL);
3304 3304 }
3305 3305 zone = zone_find_by_any_path(path, B_FALSE);
3306 3306 mutex_exit(&vp->v_lock);
3307 3307 }
3308 3308 /*
3309 3309 * Caller has verified that the file is either
3310 3310 * exported or visible. So if the path falls in
3311 3311 * global zone, admin_low is returned; otherwise
3312 3312 * the zone's label is returned.
3313 3313 */
3314 3314 zone_label = zone->zone_slabel;
3315 3315 label_hold(zone_label);
3316 3316 zone_rele(zone);
3317 3317 return (zone_label);
3318 3318 }
3319 3319
3320 3320 /*
3321 3321 * TX NFS routine used by NFSv3 and NFSv4 to do label check
3322 3322 * on client label and server's file object lable.
3323 3323 */
3324 3324 boolean_t
3325 3325 do_rfs_label_check(bslabel_t *clabel, vnode_t *vp, int flag,
3326 3326 struct exportinfo *exi)
3327 3327 {
3328 3328 bslabel_t *slabel;
3329 3329 ts_label_t *tslabel;
3330 3330 boolean_t result;
3331 3331
3332 3332 if ((tslabel = nfs_getflabel(vp, exi)) == NULL) {
3333 3333 return (B_FALSE);
3334 3334 }
3335 3335 slabel = label2bslabel(tslabel);
3336 3336 DTRACE_PROBE4(tx__rfs__log__info__labelcheck, char *,
3337 3337 "comparing server's file label(1) with client label(2) (vp(3))",
3338 3338 bslabel_t *, slabel, bslabel_t *, clabel, vnode_t *, vp);
3339 3339
3340 3340 if (flag == EQUALITY_CHECK)
3341 3341 result = blequal(clabel, slabel);
3342 3342 else
3343 3343 result = bldominates(clabel, slabel);
3344 3344 label_rele(tslabel);
3345 3345 return (result);
3346 3346 }
3347 3347
3348 3348 /*
3349 3349 * Callback function to return the loaned buffers.
3350 3350 * Calls VOP_RETZCBUF() only after all uio_iov[]
3351 3351 * buffers are returned. nu_ref maintains the count.
3352 3352 */
3353 3353 void
3354 3354 rfs_free_xuio(void *free_arg)
3355 3355 {
3356 3356 uint_t ref;
3357 3357 nfs_xuio_t *nfsuiop = (nfs_xuio_t *)free_arg;
3358 3358
3359 3359 ref = atomic_dec_uint_nv(&nfsuiop->nu_ref);
3360 3360
3361 3361 /*
3362 3362 * Call VOP_RETZCBUF() only when all the iov buffers
3363 3363 * are sent OTW.
3364 3364 */
3365 3365 if (ref != 0)
3366 3366 return;
3367 3367
3368 3368 if (((uio_t *)nfsuiop)->uio_extflg & UIO_XUIO) {
3369 3369 (void) VOP_RETZCBUF(nfsuiop->nu_vp, (xuio_t *)free_arg, NULL,
3370 3370 NULL);
3371 3371 VN_RELE(nfsuiop->nu_vp);
3372 3372 }
3373 3373
3374 3374 kmem_cache_free(nfs_xuio_cache, free_arg);
3375 3375 }
3376 3376
3377 3377 xuio_t *
3378 3378 rfs_setup_xuio(vnode_t *vp)
3379 3379 {
3380 3380 nfs_xuio_t *nfsuiop;
3381 3381
3382 3382 nfsuiop = kmem_cache_alloc(nfs_xuio_cache, KM_SLEEP);
3383 3383
3384 3384 bzero(nfsuiop, sizeof (nfs_xuio_t));
3385 3385 nfsuiop->nu_vp = vp;
3386 3386
3387 3387 /*
3388 3388 * ref count set to 1. more may be added
3389 3389 * if multiple mblks refer to multiple iov's.
3390 3390 * This is done in uio_to_mblk().
3391 3391 */
3392 3392
3393 3393 nfsuiop->nu_ref = 1;
3394 3394
3395 3395 nfsuiop->nu_frtn.free_func = rfs_free_xuio;
3396 3396 nfsuiop->nu_frtn.free_arg = (char *)nfsuiop;
3397 3397
3398 3398 nfsuiop->nu_uio.xu_type = UIOTYPE_ZEROCOPY;
3399 3399
3400 3400 return (&nfsuiop->nu_uio);
3401 3401 }
3402 3402
3403 3403 mblk_t *
3404 3404 uio_to_mblk(uio_t *uiop)
3405 3405 {
3406 3406 struct iovec *iovp;
3407 3407 int i;
3408 3408 mblk_t *mp, *mp1;
3409 3409 nfs_xuio_t *nfsuiop = (nfs_xuio_t *)uiop;
3410 3410
3411 3411 if (uiop->uio_iovcnt == 0)
3412 3412 return (NULL);
3413 3413
3414 3414 iovp = uiop->uio_iov;
3415 3415 mp = mp1 = esballoca((uchar_t *)iovp->iov_base, iovp->iov_len,
3416 3416 BPRI_MED, &nfsuiop->nu_frtn);
3417 3417 ASSERT(mp != NULL);
3418 3418
3419 3419 mp->b_wptr += iovp->iov_len;
3420 3420 mp->b_datap->db_type = M_DATA;
3421 3421
3422 3422 for (i = 1; i < uiop->uio_iovcnt; i++) {
3423 3423 iovp = (uiop->uio_iov + i);
3424 3424
3425 3425 mp1->b_cont = esballoca(
3426 3426 (uchar_t *)iovp->iov_base, iovp->iov_len, BPRI_MED,
3427 3427 &nfsuiop->nu_frtn);
3428 3428
3429 3429 mp1 = mp1->b_cont;
3430 3430 ASSERT(mp1 != NULL);
3431 3431 mp1->b_wptr += iovp->iov_len;
3432 3432 mp1->b_datap->db_type = M_DATA;
3433 3433 }
3434 3434
3435 3435 nfsuiop->nu_ref = uiop->uio_iovcnt;
3436 3436
3437 3437 return (mp);
3438 3438 }
3439 3439
3440 3440 /*
3441 3441 * Allocate memory to hold data for a read request of len bytes.
3442 3442 *
3443 3443 * We don't allocate buffers greater than kmem_max_cached in size to avoid
3444 3444 * allocating memory from the kmem_oversized arena. If we allocate oversized
3445 3445 * buffers, we incur heavy cross-call activity when freeing these large buffers
3446 3446 * in the TCP receive path. Note that we can't set b_wptr here since the
3447 3447 * length of the data returned may differ from the length requested when
3448 3448 * reading the end of a file; we set b_wptr in rfs_rndup_mblks() once the
3449 3449 * length of the read is known.
3450 3450 */
3451 3451 mblk_t *
3452 3452 rfs_read_alloc(uint_t len, struct iovec **iov, int *iovcnt)
3453 3453 {
3454 3454 struct iovec *iovarr;
3455 3455 mblk_t *mp, **mpp = ∓
3456 3456 size_t mpsize;
3457 3457 uint_t remain = len;
3458 3458 int i, err = 0;
3459 3459
3460 3460 *iovcnt = howmany(len, kmem_max_cached);
3461 3461
3462 3462 iovarr = kmem_alloc(*iovcnt * sizeof (struct iovec), KM_SLEEP);
3463 3463 *iov = iovarr;
3464 3464
3465 3465 for (i = 0; i < *iovcnt; remain -= mpsize, i++) {
3466 3466 ASSERT(remain <= len);
3467 3467 /*
3468 3468 * We roundup the size we allocate to a multiple of
3469 3469 * BYTES_PER_XDR_UNIT (4 bytes) so that the call to
3470 3470 * xdrmblk_putmblk() never fails.
3471 3471 */
3472 3472 ASSERT(kmem_max_cached % BYTES_PER_XDR_UNIT == 0);
3473 3473 mpsize = MIN(kmem_max_cached, remain);
3474 3474 *mpp = allocb_wait(RNDUP(mpsize), BPRI_MED, STR_NOSIG, &err);
3475 3475 ASSERT(*mpp != NULL);
3476 3476 ASSERT(err == 0);
3477 3477
3478 3478 iovarr[i].iov_base = (caddr_t)(*mpp)->b_rptr;
3479 3479 iovarr[i].iov_len = mpsize;
3480 3480 mpp = &(*mpp)->b_cont;
3481 3481 }
3482 3482 return (mp);
3483 3483 }
3484 3484
3485 3485 void
3486 3486 rfs_rndup_mblks(mblk_t *mp, uint_t len, int buf_loaned)
3487 3487 {
3488 3488 int i;
3489 3489 int alloc_err = 0;
3490 3490 mblk_t *rmp;
3491 3491 uint_t mpsize, remainder;
3492 3492
3493 3493 remainder = P2NPHASE(len, BYTES_PER_XDR_UNIT);
3494 3494
3495 3495 /*
3496 3496 * Non copy-reduction case. This function assumes that blocks were
3497 3497 * allocated in multiples of BYTES_PER_XDR_UNIT bytes, which makes this
3498 3498 * padding safe without bounds checking.
3499 3499 */
3500 3500 if (!buf_loaned) {
3501 3501 /*
3502 3502 * Set the size of each mblk in the chain until we've consumed
3503 3503 * the specified length for all but the last one.
3504 3504 */
3505 3505 while ((mpsize = MBLKSIZE(mp)) < len) {
3506 3506 ASSERT(mpsize % BYTES_PER_XDR_UNIT == 0);
3507 3507 mp->b_wptr += mpsize;
3508 3508 len -= mpsize;
3509 3509 mp = mp->b_cont;
3510 3510 ASSERT(mp != NULL);
3511 3511 }
3512 3512
3513 3513 ASSERT(len + remainder <= mpsize);
3514 3514 mp->b_wptr += len;
3515 3515 for (i = 0; i < remainder; i++)
3516 3516 *mp->b_wptr++ = '\0';
3517 3517 return;
3518 3518 }
3519 3519
3520 3520 /*
3521 3521 * No remainder mblk required.
3522 3522 */
3523 3523 if (remainder == 0)
3524 3524 return;
3525 3525
3526 3526 /*
3527 3527 * Get to the last mblk in the chain.
3528 3528 */
3529 3529 while (mp->b_cont != NULL)
3530 3530 mp = mp->b_cont;
3531 3531
3532 3532 /*
3533 3533 * In case of copy-reduction mblks, the size of the mblks are fixed
3534 3534 * and are of the size of the loaned buffers. Allocate a remainder
3535 3535 * mblk and chain it to the data buffers. This is sub-optimal, but not
3536 3536 * expected to happen commonly.
3537 3537 */
3538 3538 rmp = allocb_wait(remainder, BPRI_MED, STR_NOSIG, &alloc_err);
3539 3539 ASSERT(rmp != NULL);
3540 3540 ASSERT(alloc_err == 0);
3541 3541
3542 3542 for (i = 0; i < remainder; i++)
3543 3543 *rmp->b_wptr++ = '\0';
3544 3544
3545 3545 rmp->b_datap->db_type = M_DATA;
3546 3546 mp->b_cont = rmp;
3547 3547 }
↓ open down ↓ |
3429 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX