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